/** * $rel->includes('posts'); * $rel->includes('posts', function($posts) { $posts->limit(5); }); * $rel->includes(['posts', 'comments']); * $rel->includes([ * 'posts' => function($posts) { $posts->limit(5); }, * 'comments' => function($cs) { $cs->includes('user'); } * ]); */ public function includes($assocNames, \Closure $modifier = null) { $rel = $this->currentOrClone(); $modelClass = $rel->modelClass; $assocs = Associations::forClass($rel->modelClass); if ($assocNames && $modifier) { $assocNames = [$assocNames => $modifier]; } if (!is_array($assocNames)) { $assocNames = [$assocNames]; } foreach ($assocNames as $assocName => $modifier) { if (is_int($assocName)) { $assocName = $modifier; $modifier = null; } if (!isset($this->includes[$assocName])) { $options = $assocs->get($assocName); if ($options === false) { throw new \Exception(sprintf("Association '%s' for class %s doesn't exist", $assocName, $modelClass)); } $relation = isset($options['className']) ? new self($options['className']) : null; $this->includes[$assocName] = ['options' => $options, 'modifier' => $modifier, 'relation' => $relation]; } if ($modifier && $relation) { $modifier($this->includes[$assocName]['relation']); } } return $rel; }
/** * Returns the Associations object that holds the associations * data for the called class. * * @return Associations */ public function getAssociations() { return Associations::forClass(get_called_class()); }