예제 #1
0
 public function testSetPrimaryKeyValue()
 {
     $author = new Author(array('name' => "Jakub Vrana", 'web' => "http://www.vrana.cz/"));
     Metadata::getMetadata('App\\Models\\Author')->setPrimaryKeyValue($author, 1);
     $this->assertEquals(new Author(array('id' => 1, 'name' => "Jakub Vrana", 'web' => "http://www.vrana.cz/")), $author);
 }
예제 #2
0
 /**
  * Get target column
  *
  * @return string
  */
 public function getTargetColumn()
 {
     return Metadata::getMetadata($this->targetEntity)->primaryKey;
 }
예제 #3
0
 /**
  * Get modificator
  *
  * @param string $entity entity class name
  * @param string $column entity column name
  * @return string
  * @throws InvalidArgumentException
  */
 protected function getModificator($entity, $column)
 {
     $metadata = Metadata::getMetadata($entity);
     if (!$metadata->hasColumn($column)) {
         throw new \InvalidArgumentException("Entity '{$entity}' has not '{$column}' column");
     }
     $class = $metadata->getColumn($column)->reflection->name;
     if (!in_array($class, array_keys(self::$modificators))) {
         throw new \NotImplementedException("Support for '{$class}' datatype not implemented in '" . get_called_class() . "'");
     }
     return self::$modificators[$class];
 }
예제 #4
0
 /**
  * Method overload for universal getter/setter
  * 
  * @param string $name associtation name
  * @param array $attr
  * @return mixed
  * @throws MemberAccessException
  */
 public function __call($name, $attr)
 {
     try {
         $metadata = Metadata::getMetadata(get_called_class());
         if (strncmp($name, 'get', 3) === 0 && $metadata->hasColumn(lcfirst(substr($name, 3)))) {
             return $this->universalGetValue(lcfirst(substr($name, 3)));
         } elseif (strncmp($name, 'set', 3) === 0 && $metadata->hasColumn(lcfirst(substr($name, 3)))) {
             return $this->universalSetValue(lcfirst(substr($name, 3)), isset($attr[0]) ? $attr[0] : NULL);
         } else {
             return parent::__call($name, $attr);
         }
     } catch (\MemberAccessException $e) {
         return parent::__call($name, $attr);
     }
 }
예제 #5
0
 /**
  * Get entity associations values
  *
  * @param mixed $entity
  * @return array
  */
 public function getAssociationsValues(&$entity)
 {
     $ref = new PropertyReflection($this->entity, '_associations');
     $ref->setAccessible(TRUE);
     $associations = $ref->getValue($entity);
     $ref->setAccessible(FALSE);
     $data = array();
     foreach ($this->associations as $association) {
         if (!array_key_exists($association->name, $associations) || $associations[$association->name] instanceof Associations\LazyLoad) {
             continue;
         }
         if (is_array($associations[$association->name]) || $associations[$association->name] instanceof \ArrayAccess) {
             $data[$association->name] = array_keys($associations[$association->name]);
         } elseif (empty($associations[$association->name])) {
             $data[$association->name] = NULL;
         } else {
             $data[$association->name] = Metadata::getMetadata(get_class($associations[$association->name]))->getPrimaryKeyValue($associations[$association->name]);
         }
     }
     return $data;
 }
예제 #6
0
 /**
  * Get target column
  *
  * @return string
  */
 public function getTargetColumn()
 {
     if (empty($this->mapped)) {
         return Metadata::getMetadata($this->targetEntity)->primaryKey;
     } else {
         return $this->column;
     }
 }
예제 #7
0
 /**
  * Get source column
  *
  * @return string
  */
 public function getSourceColumn()
 {
     return Metadata::getMetadata($this->sourceEntity)->primaryKey;
 }
예제 #8
0
파일: Map.php 프로젝트: nella/ActiveMapper
 /**
  * Map entity or entities
  *
  * @param string $entity
  * @param string $name
  * @param mixed $key
  * @param mixed $targetPK
  * @throws InvalidArgumentException
  */
 public function map($entity, $name, $key, $targetPK)
 {
     $association = Metadata::getMetadata($entity)->associations[$name];
     if ($association instanceof OneToMany && !is_array($targetPK) && !$targetPK instanceof \ArrayAccess && $targetPK != NULL) {
         throw new \InvalidArgumentException("For association '{$name}' of '{$entity}' entity targetKey must be array because is OneToMany");
     }
     if ($association instanceof ManyToMany && !is_array($targetPK) && !$targetPK instanceof \ArrayAccess && $targetPK != NULL) {
         throw new \InvalidArgumentException("For association '{$name}' of '{$entity}' entity targetKey must be array because is ManyToMany");
     }
     if ($association instanceof OneToOne && empty($association->mapped)) {
         if (!isset($this->data[$association->targetEntity])) {
             $this->data[$association->targetEntity] = array();
         }
         if (!isset($this->data[$association->targetEntity][$entity])) {
             $this->data[$association->targetEntity][$entity] = array();
         }
         if (empty($targetPK)) {
             unset($this->data[$association->targetEntity][$entity][$key]);
         } else {
             $this->data[$association->targetEntity][$entity][$key] = $targetPK;
         }
     } elseif ($association instanceof OneToMany) {
         if (!isset($this->data[$entity])) {
             $this->data[$entity] = array();
         }
         if (!isset($this->data[$entity][$association->targetEntity])) {
             $this->data[$entity][$association->targetEntity] = array();
         }
         if (empty($targetPK)) {
             $keys = $this->data[$entity][$association->targetEntity][$key];
             unset($this->data[$entity][$association->targetEntity][$key]);
         } else {
             $keys = $this->data[$entity][$association->targetEntity][$key] = $targetPK;
         }
         foreach ($keys as $value) {
             if (!isset($this->data[$association->targetEntity])) {
                 $this->data[$association->targetEntity] = array();
             }
             if (!isset($this->data[$association->targetEntity][$entity])) {
                 $this->data[$association->targetEntity][$entity] = array();
             }
             if (empty($targetPK)) {
                 unset($this->data[$association->targetEntity][$entity][$value]);
             } else {
                 $this->data[$association->targetEntity][$entity][$value] = $key;
             }
         }
     } elseif ($association instanceof ManyToOne) {
         if (!isset($this->data[$entity])) {
             $this->data[$entity] = array();
         }
         if (!isset($this->data[$entity][$association->targetEntity])) {
             $this->data[$entity][$association->targetEntity] = array();
         }
         if (empty($targetPK)) {
             if (isset($this->data[$association->targetEntity]) && isset($this->data[$association->targetEntity][$entity])) {
                 $value = $this->data[$entity][$association->targetEntity][$key];
                 if (isset($this->data[$association->targetEntity][$entity][$value])) {
                     $values =& $this->data[$association->targetEntity][$entity][$value];
                     $pos = array_search($value, $values);
                     if ($pos) {
                         unset($values[$pos]);
                     }
                 }
             }
             unset($this->data[$entity][$association->targetEntity][$key]);
         } else {
             $this->data[$entity][$association->targetEntity][$key] = $targetPK;
             if (isset($this->data[$association->targetEntity]) && isset($this->data[$association->targetEntity][$entity])) {
                 $value = $this->data[$entity][$association->targetEntity][$key];
                 if (isset($this->data[$association->targetEntity][$entity][$value])) {
                     $values =& $this->data[$association->targetEntity][$entity][$value];
                     if (!in_array($value, $values)) {
                         $values[] = $value;
                     }
                 }
             }
         }
     } else {
         if (!isset($this->data[$entity])) {
             $this->data[$entity] = array();
         }
         if (!isset($this->data[$entity][$association->targetEntity])) {
             $this->data[$entity][$association->targetEntity] = array();
         }
         if (empty($targetPK)) {
             unset($this->data[$entity][$association->targetEntity][$key]);
         } else {
             $this->data[$entity][$association->targetEntity][$key] = $targetPK;
         }
     }
 }