From 7c7ed48c4c95b353f9968e27bf5274f9087991aa Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 25 Nov 2015 13:36:12 +0100 Subject: [PATCH] cleanup docs and duplicate code in query --- framework/db/ActiveQuery.php | 20 +++++--------------- framework/db/Query.php | 32 ++++++++++++++++---------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/framework/db/ActiveQuery.php b/framework/db/ActiveQuery.php index 098031925b..2b7b075418 100644 --- a/framework/db/ActiveQuery.php +++ b/framework/db/ActiveQuery.php @@ -39,6 +39,7 @@ use yii\base\InvalidConfigException; * ActiveQuery also provides the following additional query options: * * - [[with()]]: list of relations that this query should be performed with. + * - [[joinWith()]]: reuse a relation query definition to add a join to a query. * - [[indexBy()]]: the name of the column by which the query result should be indexed. * - [[asArray()]]: whether to return each record as an array. * @@ -154,19 +155,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface } if (empty($this->select) && !empty($this->join)) { - foreach ((array) $this->from as $alias => $table) { - if (is_string($alias)) { - $this->select = ["$alias.*"]; - } elseif (is_string($table)) { - if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $table, $matches)) { - $alias = $matches[2]; - } else { - $alias = $table; - } - $this->select = ["$alias.*"]; - } - break; - } + list(, $alias) = $this->getQueryTableName($this); + $this->select = ["$alias.*"]; } if ($this->primaryModel === null) { @@ -442,8 +432,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface * Inner joins with the specified relations. * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". * Please refer to [[joinWith()]] for detailed usage of this method. - * @param string|array $with the relations to be joined with - * @param boolean|array $eagerLoading whether to eager loading the relations + * @param string|array $with the relations to be joined with. + * @param boolean|array $eagerLoading whether to eager loading the relations. * @return $this the query object itself * @see joinWith() */ diff --git a/framework/db/Query.php b/framework/db/Query.php index cc3ec4ab22..6cf1fedee2 100644 --- a/framework/db/Query.php +++ b/framework/db/Query.php @@ -583,13 +583,13 @@ class Query extends Component implements QueryInterface * @param string $type the type of join, such as INNER JOIN, LEFT JOIN. * @param string|array $table the table to be joined. * - * Use string to represent the name of the table to be joined. - * Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). + * Use a string to represent the name of the table to be joined. + * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). * The method will automatically quote the table name unless it contains some parenthesis * (which means the table is given as a sub-query or DB expression). * - * Use array to represent joining with a sub-query. The array must contain only one element. - * The value must be a Query object representing the sub-query while the corresponding key + * Use an array to represent joining with a sub-query. The array must contain only one element. + * The value must be a [[Query]] object representing the sub-query while the corresponding key * represents the alias for the sub-query. * * @param string|array $on the join condition that should appear in the ON part. @@ -607,13 +607,13 @@ class Query extends Component implements QueryInterface * Appends an INNER JOIN part to the query. * @param string|array $table the table to be joined. * - * Use string to represent the name of the table to be joined. - * Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). + * Use a string to represent the name of the table to be joined. + * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). * The method will automatically quote the table name unless it contains some parenthesis * (which means the table is given as a sub-query or DB expression). * - * Use array to represent joining with a sub-query. The array must contain only one element. - * The value must be a Query object representing the sub-query while the corresponding key + * Use an array to represent joining with a sub-query. The array must contain only one element. + * The value must be a [[Query]] object representing the sub-query while the corresponding key * represents the alias for the sub-query. * * @param string|array $on the join condition that should appear in the ON part. @@ -631,13 +631,13 @@ class Query extends Component implements QueryInterface * Appends a LEFT OUTER JOIN part to the query. * @param string|array $table the table to be joined. * - * Use string to represent the name of the table to be joined. - * Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). + * Use a string to represent the name of the table to be joined. + * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). * The method will automatically quote the table name unless it contains some parenthesis * (which means the table is given as a sub-query or DB expression). * - * Use array to represent joining with a sub-query. The array must contain only one element. - * The value must be a Query object representing the sub-query while the corresponding key + * Use an array to represent joining with a sub-query. The array must contain only one element. + * The value must be a [[Query]] object representing the sub-query while the corresponding key * represents the alias for the sub-query. * * @param string|array $on the join condition that should appear in the ON part. @@ -655,13 +655,13 @@ class Query extends Component implements QueryInterface * Appends a RIGHT OUTER JOIN part to the query. * @param string|array $table the table to be joined. * - * Use string to represent the name of the table to be joined. - * Table name can contain schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). + * Use a string to represent the name of the table to be joined. + * The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). * The method will automatically quote the table name unless it contains some parenthesis * (which means the table is given as a sub-query or DB expression). * - * Use array to represent joining with a sub-query. The array must contain only one element. - * The value must be a Query object representing the sub-query while the corresponding key + * Use an array to represent joining with a sub-query. The array must contain only one element. + * The value must be a [[Query]] object representing the sub-query while the corresponding key * represents the alias for the sub-query. * * @param string|array $on the join condition that should appear in the ON part.