示例#1
0
 /**
  * Due setContext() method and entity cache of ORM any custom initiation code in constructor
  * must not depends on database data.
  *
  * @see Component::staticContainer()
  * @see setContext
  * @param array      $data
  * @param bool|false $loaded
  * @param ORM|null   $orm
  * @param array      $ormSchema
  */
 public function __construct(array $data = [], $loaded = false, ORM $orm = null, array $ormSchema = [])
 {
     $this->loaded = $loaded;
     //We can use global container as fallback if no default values were provided
     $this->orm = $this->saturate($orm, ORM::class);
     $this->ormSchema = !empty($ormSchema) ? $ormSchema : $this->orm->getSchema(static::class);
     parent::__construct($this->ormSchema);
     static::initialize();
     if (isset($data[ORM::PIVOT_DATA])) {
         $this->pivotData = $data[ORM::PIVOT_DATA];
         unset($data[ORM::PIVOT_DATA]);
     }
     foreach (array_intersect_key($data, $this->ormSchema[ORM::M_RELATIONS]) as $name => $relation) {
         $this->relations[$name] = $relation;
         unset($data[$name]);
     }
     //Merging with default values
     $this->fields = $data + $this->ormSchema[ORM::M_COLUMNS];
     if (!$this->isLoaded()) {
         //Non loaded records should be in solid state by default and require initial validation
         $this->solidState(true)->invalidate();
     }
 }
 /**
  * @param array           $fields
  * @param EntityInterface $parent
  * @param ODM             $odm
  * @param array           $odmSchema
  * @throws SugarException
  */
 public function __construct($fields = [], EntityInterface $parent = null, ODM $odm = null, $odmSchema = null)
 {
     $this->parent = $parent;
     //We can use global container as fallback if no default values were provided
     $this->odm = $this->saturate($odm, ODM::class);
     $this->odmSchema = !empty($odmSchema) ? $odmSchema : $this->odm->schema(static::class);
     if (empty($fields)) {
         $this->invalidate();
     }
     $fields = is_array($fields) ? $fields : [];
     if (!empty($this->odmSchema[ODM::D_DEFAULTS])) {
         //Merging with default values
         $fields = array_replace_recursive($this->odmSchema[ODM::D_DEFAULTS], $fields);
     }
     parent::__construct($fields, $this->odmSchema);
 }