コード例 #1
0
 /**
  * Check if value can be assigned to current attribute<br>
  *
  * If Entity has an ID and attribute 'once' flag is set and attribute has a value assigned to it
  * then a new value should not be assigned.
  *
  * @return bool
  */
 protected function canAssign()
 {
     if ($this->entity->getId()->getValue() && $this->getOnce() && $this->value !== null) {
         return false;
     }
     return true;
 }
コード例 #2
0
 /**
  * Extract EntityAbstract data to array using specified list of attributes.
  * If no attributes are specified, only simple and Many2One attributes will be extracted.
  * If you need to get One2Many and Many2Many attributes, you need to explicitly specify a list of attributes.
  *
  * @param array $attributes Ex: 'title,author.name,comments.id,comments.text'
  *
  * @return array
  */
 public function extractData($attributes = [])
 {
     $checkKey = get_class($this->entity) . '-' . $this->entity->getId();
     if (self::$loadedEntities->keyExists($checkKey)) {
         return ['__reference__' => true, 'class' => get_class($this->entity), 'id' => $this->entity->getId()->getValue()];
     } else {
         self::$loadedEntities->key($checkKey, true);
     }
     if ($this->isEmpty($attributes)) {
         $attributes = $this->getDefaultAttributes();
     }
     $data = [];
     $attributes = $this->buildEntityFields($attributes);
     foreach ($attributes as $attr => $subAttributes) {
         $entityAttribute = $this->entity->getAttribute($attr);
         $entityAttributeValue = $entityAttribute->getValue();
         $isOne2Many = $this->isInstanceOf($entityAttribute, AttributeType::ONE2MANY);
         $isMany2Many = $this->isInstanceOf($entityAttribute, AttributeType::MANY2MANY);
         $isMany2One = $this->isInstanceOf($entityAttribute, AttributeType::MANY2ONE);
         if ($isMany2One) {
             if ($this->isNull($entityAttributeValue)) {
                 $data[$attr] = null;
                 continue;
             }
             if (self::$currentLevel < $this->nestedLevel) {
                 self::$currentLevel++;
                 $attrDataExtractor = new EntityDataExtractor($entityAttributeValue, $this->nestedLevel);
                 $data[$attr] = $attrDataExtractor->extractData($subAttributes);
                 self::$currentLevel--;
             }
         } elseif ($isOne2Many || $isMany2Many) {
             $data[$attr] = [];
             foreach ($entityAttributeValue as $item) {
                 if (self::$currentLevel < $this->nestedLevel) {
                     self::$currentLevel++;
                     $attrDataExtractor = new EntityDataExtractor($item, $this->nestedLevel);
                     $data[$attr][] = $attrDataExtractor->extractData($subAttributes);
                     self::$currentLevel--;
                 }
             }
         } else {
             $data[$attr] = $entityAttribute->getToArrayValue();
         }
     }
     self::$loadedEntities->removeKey($checkKey);
     return $data;
 }
コード例 #3
0
 /**
  * Set or get attribute value
  *
  * @return bool|null|\Webiny\Component\Entity\EntityAbstract
  */
 public function getValue()
 {
     if ($this->isNull($this->value)) {
         $query = [$this->relatedAttribute => $this->entity->getId()->getValue()];
         $query = array_merge($query, $this->filter);
         $callable = [$this->entityClass, 'find'];
         $this->value = call_user_func_array($callable, [$query]);
     }
     return $this->value;
 }