/** * * 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; }
/** * Populate the given data object * * @param t41_Data_Object $do data object instance * @return boolean */ public function read(t41_Data_Object $do) { $subDn = $this->_mapper ? $this->_mapper->getDatastore($do->getClass()) : null; // get data from backend try { if (!$this->_ressource) { $this->_connect($subDn); } //$data = $this->_ressource->getEntry($do->getUri()->getIdentifier()); $data = $this->_ressource->search('(objectClass=*)', $do->getUri()->getIdentifier()); } catch (Exception $e) { throw new Exception($e->getMessage); } if (empty($data)) { return false; } // Normalize array before mapping // Almost each record in a LDAP result array is an array $data = $this->_flattenArray($data); $do->populate($data, $this->_mapper); return true; }
/** * Return a key/value array of all backend keys needed to build a query for a unique record * * @param t41_Data_Object $do * @return array */ protected function _preparePrimaryKeyClauses(t41_Data_Object $do) { /* no mapper or no pkey definition in mapper */ if (!$this->_mapper || !$this->_mapper->getPrimaryKey($do->getUri()->getClass())) { return array(Backend::DEFAULT_PKEY => $do->getUri()->getIdentifier()); } $array = array(); // example of mapper definition: <mapper id="myid" pkey="key1:string,key2:integer">...</mapper> $pkeyVals = explode(\t41\Mapper::VALUES_SEPARATOR, $do->getUri()->getIdentifier()); /* @var $obj t41_Backend_Key */ foreach ($this->_mapper->getPrimaryKey($do->getClass()) as $key => $obj) { if (!isset($pkeyVals[$key])) { continue; } $array[$obj->getName()] = $obj->castValue($pkeyVals[$key]); } return $array; }