/** * Set up model with given value or with the current timestamp. * * @param Zend_Date|Opus_Date|string $value (Optional) Some sort of date representation. * @return void */ public function __construct($value = null) { parent::__construct(); if ($value instanceof Zend_Date) { $this->setZendDate($value); } else { if ($value instanceof DateTime) { $this->setDateTime($value); } else { if (is_string($value) and preg_match(self::TIMEDATE_REGEXP, $value)) { $this->setFromString($value); } else { if (is_string($value) and preg_match(self::DATEONLY_REGEXP, $value)) { $this->setFromString($value); } else { if ($value instanceof Opus_Date) { $this->updateFrom($value); } else { // set all fields to 0 $this->setYear(0)->setMonth(0)->setDay(0)->setHour(NULL)->setMinute(NULL)->setSecond(NULL)->setTimezone(NULL)->setUnixTimestamp(0); } } } } } }
/** * Construct a new model instance and connect it a database table's row. * Pass an id to immediately fetch model data from the database. If not id is given * a new persistent intance gets created wich got its id set as soon as it is stored * via a call to _store(). * * @param integer|Zend_Db_Table_Row $id (Optional) (Id of) Existing database row. * @param Zend_Db_Table_Abstract $tableGatewayModel (Optional) Opus_Db model to fetch table row from. * @throws Opus_Model_Exception Thrown if passed id is invalid. */ public function __construct($id = null, Zend_Db_Table_Abstract $tableGatewayModel = null) { $gatewayClass = self::getTableGatewayClass(); // Ensure that a default table gateway class is set if (is_null($gatewayClass) === true and is_null($tableGatewayModel) === true) { throw new Opus_Model_Exception('No table gateway model passed or specified by $_tableGatewayClass for class: ' . get_class($this)); } if ($tableGatewayModel === null) { // Try to query table gateway from internal attribute $tableGatewayModel = Opus_Db_TableGateway::getInstance($gatewayClass); } if ($id === null) { $this->_primaryTableRow = $tableGatewayModel->createRow(); } else { if ($id instanceof Zend_Db_Table_Row) { if ($id->getTableClass() !== $gatewayClass) { throw new Opus_Model_Exception('Mistyped table row passed. Expected row from ' . $gatewayClass . ', got row from ' . $id->getTableClass() . '.'); } $this->_primaryTableRow = $id; $this->_isNewRecord = false; } else { $id_tupel = is_array($id) ? $id : array($id); $id_string = is_array($id) ? "(" . implode(",", $id) . ")" : $id; // This is needed, because find takes as many parameters as // primary keys. It *does* *not* accept arrays with all primary // key columns. $rowset = call_user_func_array(array(&$tableGatewayModel, 'find'), $id_tupel); if (false == $rowset->count() > 0) { throw new Opus_Model_NotFoundException('No ' . get_class($tableGatewayModel) . " with id {$id_string} in database."); } $this->_primaryTableRow = $rowset->getRow(0); $this->_isNewRecord = false; } } // Paranoid programming, sorry! Check if proper row has been created. if (!$this->_primaryTableRow instanceof Zend_Db_Table_Row) { throw new Opus_Model_Exception("Invalid row object for class " . get_class($this)); } parent::__construct(); // initialize plugins $this->_loadPlugins(); $this->_fetchValues(); $this->_clearFieldsModifiedFlag(); }