Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow models to provide utilize relations by default #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ public function getMetaData()
*/
public static function on(Connection $db)
{
return (new Query())
$self = new static();
$query = (new Query())
->setDb($db)
->setModel(new static());
->setModel($self);

$self->initQuery($query);

return $query;
}

/**
Expand Down Expand Up @@ -122,6 +127,17 @@ public function createRelations(Relations $relations)
{
}

/**
* Initialize this model's Query
*
* If you want to initialize the query right after it's construction, override this method
*
* @param Query $query
*/
public function initQuery(Query $query)
{
}

/**
* Initialize the model
*
Expand Down
10 changes: 6 additions & 4 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,16 @@ public function getUtilize()
/**
* Add a relation to utilize (join)
*
* @param string $path
* @param string|string[] $paths
*
* @return $this
*/
public function utilize($path)
public function utilize($paths)
{
$path = $this->getResolver()->qualifyPath($path, $this->getModel()->getTableName());
$this->utilize[$path] = $this->getResolver()->resolveRelation($path);
foreach ((array) $paths as $path) {
$path = $this->getResolver()->qualifyPath($path, $this->getModel()->getTableName());
$this->utilize[$path] = $this->getResolver()->resolveRelation($path);
}

return $this;
}
Expand Down