✏️ 正在编辑: Model.php
路径:
/srv/systems_dir/biblioteca_yuppie/library/Yuppie/Model.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php abstract class Yuppie_Model extends Zend_Db_Table_Row_Abstract { use Yuppie_Core_Audity; /** * Formato de data/hora padrão para o banco de dados */ const DATETIME_FORMAT = 'yyyy-MM-dd HH:mm:ss'; /** * Nome da classe Yuppie_DbTable referente a este model * @var String */ protected $_dbTable; public function __construct(array $config = array()) { if (!isset($config['table']) && !$this->_tableClass) { $this->_tableClass = str_replace('_Model_', '_Model_DbTable_', get_class($this)); } parent::__construct($config); } /** * Retorna a classe Yuppie_DbTable referente a este model * * @return Yuppie_DbTable */ public function getDbTable() { if (!$this->_dbTable) { $this->_dbTable = str_replace('Model_', 'Model_DbTable_', get_class($this)); } return new $this->_dbTable; } /** * Ao setar uma atributo diretamente, verifica se existe um método * específico para alterá-lo e, se houver, chama o método * * @param type $columnName * @param type $value * @return type */ public function __set($columnName, $value) { $methodName = 'set' . str_replace(" ", "", ucwords(implode(" ", explode("_", $columnName)))); if (method_exists($this, $methodName)) { return $this->$methodName($value); } return parent::__set($columnName, $value); } public function setAttribute($columnName, $value) { return parent::__set($columnName, $value); } /** * Retorna um campo de data como um objeto Zend_Date * * @param String $columnName * @param String $inputFormat * @return Zend_Date */ public function getDate($columnName, $inputFormat=self::DATETIME_FORMAT) { $date = $this->{$columnName}; return $date ? new Zend_Date($date, $inputFormat) : null; } /** * Altera um campo de data para o formato definido * * @param String $columnName * @param String|Zend_Date $value * @param String $outputFormat * @param String $inputFormat * @return void * @throws Yuppie_Exception */ public function setDate($columnName, $value, $outputFormat=self::DATETIME_FORMAT, $inputFormat=null) { if ($value) { if (!Zend_Date::isDate($value, 'yyyy-MM-dd HH:mm:ss') && !Zend_Date::isDate($value, 'yyyy-MM-dd')) { if (!Zend_Date::isDate($value, $inputFormat)) { throw new Yuppie_Exception("'$value' não é uma data válida"); } $date = new Zend_Date($value, $inputFormat); $value = $date->get($outputFormat); } } return parent::__set($columnName, $value); } public function getFloat($columnName) { $value = $this->$columnName; return Zend_Locale_Format::toFloat($value, array('precision' => 2)); } public function setFloat($columnName, $value) { if( $value != '' ){ $locale = new Zend_Locale('en'); if (!Zend_Locale_Format::isFloat($value, array('precision' => 2, 'locale' => $locale))) { //seta o locale para pt_BR para converter para o padrão brasileiro $locale = new Zend_Locale('pt_BR'); $value = Zend_Locale_Format::getNumber($value, array( 'locale' => $locale, 'precision' => 2 )); } } $this->setAttribute($columnName, $value); } public function setFilterDigits($columnName, $value){ $filter = new Zend_Filter_Digits(); $this->setAttribute($columnName, $filter->filter($value)); } public function findDependentRowset($dependentTable, $ruleKey = null, Zend_Db_Table_Select $select = null) { if (!$select) { $select = $this->getDbTable()->select(); } if (isset($this->_data['excluido'])) { $select->where('excluido = 0'); } return parent::findDependentRowset($dependentTable, $ruleKey, $select); } public function findParentRow($parentTable, $ruleKey = null, \Zend_Db_Table_Select $select = null) { if( is_string($parentTable) ){ $modelParentTable = $this->_getTableFromString($parentTable); $info = $modelParentTable->info(); } if (!$select) { $select = $this->getDbTable()->select(); } if ( in_array('excluido', $info['cols']) ) { $select->where('excluido = 0'); } return parent::findParentRow($parentTable, $ruleKey, $select); } public function setReferencesFromArray(array $data){ $referenceMap = $this->getDbTable()->info('referenceMap'); foreach ($data as $object => $values){ $reference = $referenceMap[ucfirst($object)]; $dbTable = new $reference['refTableClass']; $attrib = "_$object"; if(!empty($this->{$object. '_id'})){ $this->$attrib = $dbTable->findById($this->{$object. '_id'}); $this->$attrib->setFromArray($values); }else{ $this->$attrib = $dbTable->createRow($values); } } } public function saveReferences(){ $referenceMap = $this->getDbTable()->info('referenceMap'); foreach ($referenceMap as $object => $values){ $attrib = '_' . strtolower($object); if(!empty($this->$attrib) && is_object($this->$attrib)){ $this->setAttribute(strtolower($object). '_id', $this->$attrib->save()); } } } public function createDirectory() { $diretorio = $this->getPath(); if( !(is_dir($diretorio)) ){ if( !(mkdir($diretorio, 0777)) ){ throw new Zend_Exception("Alerta: pasta de upload não existe e não pode ser criada"); } } return $diretorio; } public function getPath() { $class = get_class($this); $modelName = strtolower(explode('_', $class)[2]); return UPLOAD_PATH . "/$modelName-$this->id"; } /** * @return array|mixed * @throws Zend_Db_Table_Exception */ // public function save() // { // $oldData = $this->_cleanData; // $id = parent::save(); // $this->audity(new Yuppie_AudityDbTable(), $id, $oldData, $this->_data, $this->_table->info('name')); // return $this->_data['id']; // } }
💾 保存文件
← 返回文件管理器