/** * @param string $class * @param ORM $orm * @param Loader $loader */ public function __construct(ORM $orm, $class, Loader $loader = null) { $this->class = $class; $this->orm = $orm; $this->columns = $this->dataColumns = []; //We aways need primary loader if (empty($this->loader = $loader)) { //Selector always need primary data loaded to define data structure and perform query //parsing, in most of cases we can easily use RootLoader associated with primary record //schema $this->loader = new RootLoader($this->orm, null, $this->orm->getSchema($class)); } //Every ORM loader has ability to declare it's primary database, we are going to use //primary loader database to initiate selector $database = $this->loader->dbalDatabase(); //AbstractSelect construction parent::__construct($database, $database->driver()->queryCompiler($database->getPrefix())); }
/** * 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(); } }
/** * {@inheritdoc} */ public function __construct(ORM $orm, $container, array $definition = [], LoaderInterface $parent = null) { $this->orm = $orm; //Related record schema $this->schema = $orm->getSchema($definition[static::RELATION_TYPE]); $this->container = $container; $this->definition = $definition; $this->parent = $parent; //Compiling options $this->options['method'] = static::LOAD_METHOD; if (!empty($parent)) { if (!$parent instanceof Loader || $parent->getDatabase() != $this->getDatabase()) { //We have to force post-load (separate query) if parent loader database is different $this->options['method'] = self::POSTLOAD; } } $this->dataColumns = array_keys($this->schema[ORM::M_COLUMNS]); }