Access object's properties (and subproperties) using dot path notation. The following are equivalent. echo $obj->property1; echo $obj->getProperty1(); echo $obj['property1']; //$obj may be an array or object echo TPropertyAccess($obj, 'property1'); Setting a property value. $obj1->propert1 = 'hello'; $obj->setProperty('hello'); $obj['property1'] = 'hello'; //$obj may be an array or object TPropertyAccess($obj, 'property1', 'hello'); Subproperties are supported using the dot notation. E.g. echo $obj->property1->property2->property3 echo TPropertyAccess::get($obj, 'property1.property2.property3');
Since: 3.1
Beispiel #1
0
 public function replaceDynamicParameter($sql, $parameter)
 {
     foreach ($this->_mappings as $property) {
         $value = TPropertyAccess::get($parameter, $property);
         $sql = preg_replace('/' . TSimpleDynamicParser::DYNAMIC_TOKEN . '/', str_replace('$', '\\$', $value), $sql, 1);
     }
     return $sql;
 }
Beispiel #2
0
 /**
  * @param mixed object to obtain the property from.
  * @param TParameterProperty parameter property.
  * @return mixed property value.
  * @throws TSqlMapException if property access is invalid.
  */
 protected function getObjectValue($object, $property)
 {
     try {
         return TPropertyAccess::get($object, $property->getProperty());
     } catch (TInvalidPropertyException $e) {
         throw new TSqlMapException('sqlmap_unable_to_get_property_for_parameter', $this->getID(), $property->getProperty(), is_object($object) ? get_class($object) : gettype($object));
     }
 }
Beispiel #3
0
 /**
  * Add nested result property to post select queue.
  * @param string post select statement ID
  * @param TResultMap current result mapping details.
  * @param TResultProperty current result property.
  * @param array a result set row retrieved from the database
  * @param object the result object
  */
 protected function enquequePostSelect($select, $resultMap, $property, $row, $resultObject)
 {
     $statement = $this->getManager()->getMappedStatement($select);
     $key = $this->getPostSelectKeys($resultMap, $property, $row);
     $postSelect = new TPostSelectBinding();
     $postSelect->setStatement($statement);
     $postSelect->setResultObject($resultObject);
     $postSelect->setResultProperty($property);
     $postSelect->setKeys($key);
     if ($property->instanceOfListType($resultObject)) {
         $values = null;
         if ($property->getLazyLoad()) {
             $values = TLazyLoadList::newInstance($statement, $key, $resultObject, $property->getProperty());
             TPropertyAccess::set($resultObject, $property->getProperty(), $values);
         } else {
             $postSelect->setMethod(self::QUERY_FOR_LIST);
         }
     } else {
         if ($property->instanceOfArrayType($resultObject)) {
             $postSelect->setMethod(self::QUERY_FOR_ARRAY);
         } else {
             $postSelect->setMethod(self::QUERY_FOR_OBJECT);
         }
     }
     if (!$property->getLazyLoad()) {
         $this->_selectQueue[] = $postSelect;
     }
 }
Beispiel #4
0
 /**
  * Returns true if the result property {@link Type getType()} is of \ArrayAccess
  * or that the actual result object is an array or implements \ArrayAccess
  * @param object result object
  * @return boolean true if the result object is an instance of \ArrayAccess or is an array.
  */
 public function instanceOfArrayType($target)
 {
     if ($this->getType() === null) {
         $prop = TPropertyAccess::get($target, $this->getProperty());
         if (is_object($prop)) {
             return $prop instanceof \ArrayAccess;
         }
         return is_array($prop);
     }
     return $this->getPropertyValueType() == self::ARRAY_TYPE;
 }
 /**
  * Load cache models from xml mapping.
  * @param SimpleXmlElement cache node.
  */
 protected function loadCacheModel($node)
 {
     $cacheModel = new TSqlMapCacheModel();
     $properties = array('id', 'implementation');
     foreach ($node->attributes() as $name => $value) {
         if (in_array(strtolower($name), $properties)) {
             $cacheModel->{'set' . $name}((string) $value);
         }
     }
     $cache = Prado::createComponent($cacheModel->getImplementationClass(), $cacheModel);
     $this->setObjectPropFromNode($cache, $node, $properties);
     foreach ($node->xpath('property') as $propertyNode) {
         $name = $propertyNode->attributes()->name;
         if ($name === null || $name === '') {
             continue;
         }
         $value = $propertyNode->attributes()->value;
         if ($value === null || $value === '') {
             continue;
         }
         if (!TPropertyAccess::has($cache, $name)) {
             continue;
         }
         TPropertyAccess::set($cache, $name, $value);
     }
     $this->loadFlushInterval($cacheModel, $node);
     $cacheModel->initialize($cache);
     $this->_manager->addCacheModel($cacheModel);
     foreach ($node->xpath('flushOnExecute') as $flush) {
         $this->loadFlushOnCache($cacheModel, $node, $flush);
     }
 }
 public function testArrayAccess()
 {
     $thingamajig = array('a' => 'foo', 'b' => 'bar', 'c' => new _PropertyAccessTestHelperPublicVar(), 'd' => new _PropertyAccessTestHelperStaticProperties(), 'e' => new _PropertyAccessTestHelperDynamicProperties());
     $testobj = new _PropertyAccessTestHelperPublicVar();
     TPropertyAccess::set($testobj, 'a', $thingamajig);
     $tmp = TPropertyAccess::get($testobj, 'a');
     self::assertTrue(is_array($tmp));
     self::assertEquals($thingamajig, $tmp);
     self::assertEquals('foo', TPropertyAccess::get($testobj, 'a.a'));
     self::assertEquals('bar', TPropertyAccess::get($testobj, 'a.b'));
     self::assertTrue(TPropertyAccess::get($testobj, 'a.c') instanceof _PropertyAccessTestHelperPublicVar);
     self::assertTrue(TPropertyAccess::get($testobj, 'a.d') instanceof _PropertyAccessTestHelperStaticProperties);
     self::assertTrue(TPropertyAccess::get($testobj, 'a.e') instanceof _PropertyAccessTestHelperDynamicProperties);
     TPropertyAccess::set($testobj, 'a.c.a', 10);
     TPropertyAccess::set($testobj, 'a.d.a', 10);
     TPropertyAccess::set($testobj, 'a.e.a', 10);
     self::assertEquals(10, TPropertyAccess::get($testobj, 'a.c.a'));
     self::assertEquals(10, TPropertyAccess::get($testobj, 'a.d.a'));
     self::assertEquals(10, TPropertyAccess::get($testobj, 'a.e.a'));
     TPropertyAccess::set($testobj, 'a.c.c', 30);
     TPropertyAccess::set($testobj, 'a.d.c', 30);
     TPropertyAccess::set($testobj, 'a.e.c', 30);
     self::assertEquals(30, TPropertyAccess::get($testobj, 'a.c.c'));
     self::assertEquals(30, TPropertyAccess::get($testobj, 'a.d.c'));
     self::assertNull(TPropertyAccess::get($testobj, 'a.e.c'));
     self::assertNull(TPropertyAccess::get($testobj, 'a.e.C'));
     self::setExpectedException('Prado\\Data\\SqlMap\\DataMapper\\TInvalidPropertyException');
     TPropertyAccess::get($testobj, 'a.c.C');
     self::setExpectedException('Prado\\Data\\SqlMap\\DataMapper\\TInvalidPropertyException');
     TPropertyAccess::get($testobj, 'a.d.C');
 }
 /**
  * Set the object properties for all the child nodes visited.
  * @param string parent node id
  * @param array list of child nodes visited.
  */
 protected function onChildNodesVisited($parent, $nodes)
 {
     if (empty($parent) || empty($this->_entries[$parent])) {
         return;
     }
     $parentObject = $this->_entries[$parent]['object'];
     $property = $this->_entries[$nodes[0]]['property'];
     $list = TPropertyAccess::get($parentObject, $property);
     foreach ($nodes as $node) {
         if ($list instanceof TList) {
             $parentObject->{$property}[] = $this->_entries[$node]['object'];
         } else {
             if (is_array($list)) {
                 $list[] = $this->_entries[$node]['object'];
             } else {
                 throw new TSqlMapExecutionException('sqlmap_property_must_be_list');
             }
         }
     }
     if (is_array($list)) {
         TPropertyAccess::set($parentObject, $property, $list);
     }
     if ($this->_entries[$parent]['property'] === null) {
         $this->_list[] = $parentObject;
     }
 }