From d905ab6fed2ec52090840f5156a5d94de430ad22 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 5 Nov 2021 16:08:48 +0100 Subject: [PATCH 1/2] Model: Add possibility to initialize the model's query --- src/Model.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Model.php b/src/Model.php index 804a599..87d1d8c 100644 --- a/src/Model.php +++ b/src/Model.php @@ -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; } /** @@ -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 * From bbfcb24c1d21a4a15fee4612bac5931844098bc7 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 5 Nov 2021 16:09:51 +0100 Subject: [PATCH 2/2] `Query::utilize()`: Allow arrays as argument type too --- src/Query.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Query.php b/src/Query.php index 8754a5f..6d610da 100644 --- a/src/Query.php +++ b/src/Query.php @@ -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; }