/** * Accessor to children relations. * * @param {?AbstractModel|int|string|array} $children If provided, add them as children in relation. * @param {?string} $collection Collection identifier for the relation, defaults to the model's collection name. * * @return {array} Existing children before modification. */ protected function children($collection = null, $children = null, $replace = false) { if ($collection === null) { $collection = $this->collectionName(); } $return = Relation::getChildren($this->identity(), $collection); if ($children) { if ($replace) { $this->deleteDescendants($collection); } if (!is_array($children)) { $children = array($children); } foreach ($children as $child) { if ($child instanceof AbstractModel) { $child = $child->identity(); } Relation::set($this->identity(), $child, $collection); } } return $return; }