protected function _applyHas() { $tree = Set::expand(array_fill_keys(array_keys($this->has()), false)); $this->_applyJoins($this->model(), $tree, '', $this->alias()); foreach ($this->has() as $path => $conditions) { $this->where($conditions, $this->alias($path)); } }
/** * Returns a nested tree representation of `'embed'` option. * * @return array The corresponding nested tree representation. */ public function treeify($embed) { if (!$embed) { return []; } if ($embed === true) { $embed = $this->relations(); } $embed = Set::expand(array_fill_keys(array_keys(Set::normalize((array) $embed)), null)); $result = []; foreach ($embed as $relName => $value) { if (!isset($this->_relations[$relName])) { continue; } if ($this->_relations[$relName]['relation'] === 'hasManyThrough') { $rel = $this->relation($relName); $result[$rel->through()] = [$rel->using() => $value]; } $result[$relName] = $value; } return $result; }
/** * Instantiates a new record or document object, initialized with any data passed in. For example: * * ```php * $post = Posts::create(['title' => 'New post']); * echo $post->title; // echoes 'New post' * $success = $post->save(); * ``` * * Note that while this method creates a new object, there is no effect on the database until * the `save()` method is called. * * In addition, this method can be used to simulate loading a pre-existing object from the * database, without actually querying the database: * * ```php * $post = Posts::create(['id' => $id, 'moreData' => 'foo'], ['exists' => true]); * $post->title = 'New title'; * $success = $post->save(); * ``` * * This will create an update query against the object with an ID matching `$id`. Also note that * only the `title` field will be updated. * * @param array $data Any data that this object should be populated with initially. * @param array $options Options to be passed to item. * - `'type'` _string_ : can be `'entity'` or `'set'`. `'set'` is used if the passed data represent a collection * of entities. Default to `'entity'`. * - `'exists'` _mixed_ : corresponds whether the entity is present in the datastore or not. * - `'autoreload'` _boolean_: sets the specific behavior when exists is `null`. A '`true`' value will perform a * reload of the entity from the datasource. Default to `'true'`. * - `'defaults'` _boolean_: indicates whether the entity needs to be populated with their defaults values on creation. * - `'model'` _string_ : the model to use for instantiating the entity. Can be useful for implementing * som Single Table Inheritance. * @return object Returns a new, un-saved record or document object. In addition to * the values passed to `$data`, the object will also contain any values * assigned to the `'default'` key of each field defined in the schema. */ public static function create($data = [], $options = []) { $defaults = ['type' => 'entity', 'exists' => false, 'model' => static::class]; $options += $defaults; $options['defaults'] = !$options['exists']; if ($options['defaults'] && $options['type'] === 'entity') { $data = Set::merge(Set::expand(static::schema()->defaults()), $data); } $type = $options['type']; $class = $type === 'entity' ? $options['model'] : static::$_classes[$options['type']]; $options = ['data' => $data] + $options; return new $class($options); }