Spot\Entity::set PHP Method

set() public method

public set ( string $field, mixed $value, boolean $modified = true )
$field string
$value mixed
$modified boolean
    public function set($field, $value, $modified = true)
    {
        // Custom setter method
        $camelCaseField = str_replace(' ', '', ucwords(str_replace('_', ' ', $field)));
        $setterMethod = 'set' . $camelCaseField;
        if (!in_array($field, $this->_inSetter) && method_exists($this, $setterMethod)) {
            $this->_inSetter[$field] = true;
            $value = call_user_func([$this, $setterMethod], $value);
            unset($this->_inSetter[$field]);
        }
        if (array_key_exists($field, $this->_data) || !in_array($field, self::$relationFields[get_class($this)])) {
            if ($modified) {
                $this->_dataModified[$field] = $value;
            } else {
                $this->_data[$field] = $value;
            }
        } elseif (in_array($field, self::$relationFields[get_class($this)])) {
            // Set relation
            $this->relation($field, $value);
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Load an entity relation object into the entity object
  *
  * @param \Spot\Entity $entity
  * @param string $field
  * @param \Spot\Entity\Relation\AbstractRelation
  * @param \Spot\Mapper $mapper
  * @param bool $reload
  * @return \Spot\Entity\EntityInterface
  * @throws \InvalidArgumentException
  */
 protected function loadRelationObject($entity, $field, $relation, Mapper $mapper, $reload = false)
 {
     $entityName = $entity instanceof ResultsetInterface ? $entity->getEntityName() : $entity->toString();
     if (empty($entityName)) {
         throw new \InvalidArgumentException("Cannot load relation with a null \$entityName");
     }
     if (isset($entity->{$field}) && !$reload) {
         return $entity->{$field};
     }
     $relationEntity = isset($relation['entity']) ? $relation['entity'] : false;
     if (!$relationEntity) {
         throw new \InvalidArgumentException("Entity for '" . $field . "' relation has not been defined.");
     }
     // Self-referencing entity relationship?
     $relationEntity == ':self' && ($relationEntity = $entityName);
     // Load relation class to lazy-loading relations on demand
     $relationClass = '\\Spot\\Entity\\Relation\\' . $relation['type'];
     // Set field equal to relation class instance
     $relation = new $relationClass($mapper, $this->entityManager, $entity, $relation);
     // Inject relation object into entity property
     if (!$entity instanceof ResultsetInterface) {
         $entity->set($field, $relation);
     }
     return $relation;
 }