Exemple #1
0
 /**
  * Get object data from backend
  * 
  *  By default, backend to use is given by data object. It is possible to use another backend by giving a
  *  instance of a backend adapter implementing t41_Backend_Adapter_Interface
  *   
  * @param t41_Backend_Adapter_Interface $backend
  */
 public function read(Backend\Adapter\AbstractAdapter $backend = null)
 {
     return Backend::read($this->_dataObject, $backend);
 }
Exemple #2
0
 /**
  * Populate the given collection from the array of identifiers and the uri base
  * 
  * @param array $ids
  * @param \t41\ObjectModel\Collection $collection
  * @param \t41\ObjectModel\ObjectUri $uriBase
  */
 protected function _populateCollection(array $ids, ObjectModel\Collection $collection, ObjectModel\ObjectUri $uriBase)
 {
     if (count($ids) == 0) {
         return array();
     }
     if (count($ids[0]) > 1) {
         $do = clone $collection->getDataObject();
     }
     $class = $collection->getDataObject()->getClass();
     // populate array with relevant objects type
     $array = array();
     /**
      * If data object has been modified (meta property added) we need to use it to populate objects
      */
     if ($collection->getParameter('memberType') != ObjectModel::URI && $collection->getDataObject()->getDna('custom')) {
         $do = clone $collection->getDataObject();
     }
     foreach ($ids as $key => $id) {
         $uri = clone $uriBase;
         if (is_array($id)) {
             $uri->setUrl($uri->getUrl() . $id['id'])->setIdentifier($id['id']);
             unset($id['id']);
         } else {
             $uri->setUrl($uri->getUrl() . $id)->setIdentifier($id);
         }
         if (isset($do)) {
             $obj = clone $do;
             $obj->setUri($uri);
         } else {
             unset($obj);
         }
         switch ($collection->getParameter('memberType')) {
             case ObjectModel::URI:
                 $obj = $uri;
                 break;
             case ObjectModel::MODEL:
                 if (isset($obj) || is_array($ids)) {
                     Backend::read($obj, null, $id);
                 } else {
                     $obj = Core::_($uri, $class);
                 }
                 break;
             case ObjectModel::DATA:
             default:
                 if (isset($obj) || is_array($ids)) {
                     Backend::read($obj, null, $id);
                 } else {
                     $obj = Core::_($uri, $class);
                 }
                 $obj = $obj->getDataObject();
                 break;
         }
         $array[$key] = $obj;
     }
     return $array;
 }
Exemple #3
0
 /**
  * Universal factory for DataObject() and BaseObject() instances with caching capabilities
  * @param ObjectUri $uri
  * @param string $class
  * @param string $type
  * @throws Exception
  * @return \t41\ObjectModel\DataObject|t41\ObjectModel\BaseObject
  */
 public static function _($uri, $class = null, $type = ObjectModel::MODEL)
 {
     if (!$uri instanceof ObjectUri) {
         if (is_null($class)) {
             throw new Exception("Give ObjectUri() instance or specify object class as second argument");
         }
         $uri = new ObjectUri($uri);
         $uri->setClass($class);
     } else {
         $class = $uri->getClass();
     }
     if (self::getEnvData('cache_objects') !== true) {
         $obj = DataObject::factory($class);
         $obj->setUri($uri);
         Backend::read($obj);
         return $type == ObjectModel::MODEL ? new $class($obj) : $obj;
     }
     $def = ObjectModel::getObjectDna($class);
     if ($def && isset($def['unchanging'])) {
         // get cache version
         if (($obj = self::cacheGet($uri->getPermanentUUID())) !== false) {
             self::log(sprintf('[Persistence] Loaded %s object (%s) from cache', $class, $uri));
         } else {
             // done this away to avoid infinite recursion in BaseObject::__construct()
             $obj = DataObject::factory($class);
             $obj->setUri($uri);
             Backend::read($obj);
             $obj = new $class($obj);
             self::cacheSet($obj, $uri->getPermanentUUID(), true, array('tags' => array('permanent')));
             self::log(sprintf('[Persistence] Saved %s object (%s) in cache', $class, $uri));
         }
         return $type == ObjectModel::MODEL ? $obj : $obj->getDataObject();
     } else {
         $obj = DataObject::factory($class);
         $obj->setUri($uri);
         Backend::read($obj);
         return $type == ObjectModel::MODEL ? new $class($obj) : $obj;
     }
 }
Exemple #4
0
 public function formatValue($key = null)
 {
     if ($key == null) {
         return '';
     }
     // value is already available (foreign key with no specific constraint in it
     if (isset($this->_enumValues[$key])) {
         return $this->_enumValues[$key];
     }
     // value no more available to select, though we need to display it !
     $uri = new ObjectModel\ObjectUri($key);
     $uri->setClass($this->getCollection()->getClass());
     $_do = clone $this->_collection->getDataObject();
     $_do->setUri($uri);
     Backend::read($_do);
     $this->_displayProps = explode(',', $this->getParameter('display'));
     $str = array();
     foreach ($this->_displayProps as $disProp) {
         $property = $_do->getProperty($disProp);
         if (!$property instanceof Property\PropertyAbstract) {
             continue;
         }
         $str[] = $property->getValue();
     }
     $str = implode(' ', $str);
     $this->_enumValues[$key] = $str;
     return $str;
 }
Exemple #5
0
 public function formatValue($key = null)
 {
     if ($key == null) {
         return '';
     }
     // value is already available (foreign key with no specific constraint in it
     if (isset($this->_enumValues[$key])) {
         return $this->_enumValues[$key];
     }
     // value no more available to select, though we need to display it !
     if (is_string($key)) {
         $uri = new ObjectModel\ObjectUri($key);
         $uri->setClass($this->getCollection()->getClass());
         $_do = clone $this->_collection->getDataObject();
         $_do->setUri($uri);
         Backend::read($_do);
     } else {
         $_do = $key->getDataObject();
     }
     return Property::parseDisplayProperty($_do, $this->getParameter('display'));
 }
Exemple #6
0
 public function populate(array $dataset, ObjectUri $uriBase)
 {
     if (count($dataset) == 0) {
         return array();
     }
     $do = clone $this->getDataObject();
     $class = $do->getClass();
     // populate array with relevant objects type
     $array = array();
     foreach ($dataset as $key => $data) {
         $uri = clone $uriBase;
         $uri->setUrl($uri->getUrl() . $data[Backend::DEFAULT_PKEY])->setIdentifier($data[Backend::DEFAULT_PKEY]);
         unset($data[Backend::DEFAULT_PKEY]);
         if (isset($do)) {
             $obj = clone $do;
             $obj->setUri($uri);
         } else {
             unset($obj);
         }
         switch ($this->getParameter('memberType')) {
             case ObjectModel::URI:
                 $obj = $uri;
                 break;
             case ObjectModel::MODEL:
                 $obj = clone $do;
                 $obj->setUri($uri);
                 Backend::read($obj, null, $data);
                 $obj = new $class($obj);
                 break;
             case ObjectModel::DATA:
             default:
                 $obj = clone $do;
                 $obj->setUri($uri);
                 Backend::read($obj, null, $data);
                 break;
         }
         $array[] = $obj;
     }
     return $array;
 }
Exemple #7
0
 /**
  * Returns the property matching the pattern in $name, recursively if needed
  * @param string $name
  * @return t41\ObjectModel\Property\AbstractProperty
  */
 public function getRecursiveProperty($name)
 {
     if ($name == ObjectUri::IDENTIFIER) {
         return new IdentifierProperty('id');
     }
     if (strpos($name, '.') === false) {
         return $this->getProperty($name);
     }
     $parts = explode('.', $name);
     $data = $this;
     foreach ($parts as $part) {
         $property = $data->getProperty($part);
         // stop recursion if property is an ArrayProperty because recursion is not possible in do's property
         if ($property instanceof ArrayProperty) {
             return $property;
         }
         if ($property instanceof Property\ObjectProperty) {
             if ($property->getValue() instanceof ObjectModel\DataObject) {
                 $data = $property->getValue();
             } else {
                 if ($property->getValue() instanceof BaseObject) {
                     $data = $property->getValue()->getDataObject();
                 } else {
                     if ($property->getValue() instanceof ObjectUri) {
                         $data = DataObject::factory($property->getParameter('instanceof'));
                         $data->setUri($property->getValue());
                         Backend::read($data);
                     } else {
                         $data = DataObject::factory($property->getParameter('instanceof'));
                     }
                 }
             }
         }
     }
     return $data->getProperty($part) ? $data->getProperty($part) : $property;
 }