/** * * Object constructor * @param t41_Data_Object|t41_Object_Uri|string $val * @param array $params */ public function __construct($val = null, array $params = null) { $this->_setParameterObjects(); if (is_array($params)) { $this->_setParameters($params); } /* build data object and populate it if possible */ if ($val instanceof DataObject) { if ($val->getClass() != get_class($this)) { throw new Exception("Provided Data Object is not build on class definition"); } $this->_dataObject = $val; /* get object rules from config */ $this->setRules(); } else { $this->_dataObject = DataObject::factory(get_class($this)); /* get object rules from config */ $this->setRules(); /* get object rules from config */ //$this->_rules = t41_Object::getRules(get_class($this), $this->_dataObject); if (!is_null($val)) { if (!$val instanceof ObjectUri) { $val = new ObjectUri($val); $val->setClass(get_class($this)); } $this->_dataObject->setUri($val); $this->read(); } } }
/** * Save new set of data from a t41_Data_Object object using INSERT * * @param t41_Data_Object $do * @return boolean * @throws t41_Backend_Exception */ public function create(t41_Data_Object $do) { $table = $this->_getTableFromClass($do->getClass()); if (!$table) { throw new Exception('BACKEND_MISSING_DBTABLE_PARAM'); } // get a valid data array passing mapper if any if ($this->_mapper) { $recordSet = $do->map($this->_mapper, $this); } else { $recordSet = $do->toArray($this); } $this->_setLastQuery('insert', $recordSet); try { $this->_ressource->insert(isset($table) ? $table : $do->getClass(), $recordSet['data']); } catch (\Exception $e) { if (true) { throw new Exception("Error Creating Record: " . $e->getMessage); } else { return false; } } // inject new t41_Object_Uri object in data object // @todo provide support for primary keys that are not generated by DB (not AUTO INCREMENTED INTEGER) $id = $this->_ressource->lastInsertId(); $uri = $id; if (!$this->_mapper instanceof Backend\Mapper) { $uri = $table . '/' . $uri; } $uri = new ObjectModel\ObjectUri($uri); $do->setUri($uri); /* get collection handling properties (if any) and process them */ foreach ($do->getProperties() as $property) { if (!$property instanceof Property\CollectionProperty) { continue; } $collection = $property->getValue(); //var_dump($collection->getMembers()); /* @var $member t41_Object_Model */ foreach ($collection->getMembers() as $member) { $member->setProperty($property->getParameter('keyprop'), $uri); $member->save(); } } return true; }