Commit cb8237c1 by Carsten Brandt

merged ActiveQueryInterface and ActiveRelatioInterface

parent e76fb238
......@@ -9,7 +9,6 @@ namespace yii\elasticsearch;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
......@@ -73,7 +72,7 @@ use yii\db\ActiveRelationTrait;
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
......
......@@ -9,7 +9,6 @@ namespace yii\mongodb;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
......@@ -60,7 +59,7 @@ use yii\db\ActiveRelationTrait;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
......
......@@ -9,7 +9,6 @@ namespace yii\mongodb\file;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
......@@ -36,7 +35,7 @@ use yii\db\ActiveRelationTrait;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
......
......@@ -11,7 +11,6 @@ use yii\base\InvalidParamException;
use yii\base\NotSupportedException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
use yii\db\QueryTrait;
......@@ -71,7 +70,7 @@ use yii\db\QueryTrait;
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends \yii\base\Component implements ActiveQueryInterface
{
use QueryTrait;
use ActiveQueryTrait;
......
......@@ -10,7 +10,6 @@ namespace yii\sphinx;
use yii\base\InvalidCallException;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
/**
......@@ -81,7 +80,7 @@ use yii\db\ActiveRelationTrait;
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
......
......@@ -9,7 +9,6 @@ namespace yii\sphinx;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\db\ActiveRelationInterface;
use yii\db\BaseActiveRecord;
use yii\db\StaleObjectException;
use yii\helpers\Inflector;
......
......@@ -175,8 +175,9 @@ Yii Framework 2 Change Log
- Chg: Advanced app template: moved database connection DSN, login and password to `-local` config not to expose it to VCS (samdark)
- Chg: Renamed `yii\web\Request::acceptedLanguages` to `acceptableLanguages` (qiangxue)
- Chg: Removed implementation of `Arrayable` from `yii\Object` (qiangxue)
- Chg #2146: Removed `ActiveRelation` class and moved the functionality to `ActiveQuery`. All relational queries are
now directly served by `ActiveQuery` allowing to use custom scopes in relations.
- Chg #2146: Removed `ActiveRelation` class and `ActiveRelationInterface`, moved the functionality to `ActiveQuery`.
All relational queries are now directly served by `ActiveQuery` allowing to use custom scopes in relations
and also to declare arbitrary queries as relations.
Also removed `ActiveRecordInterface::createActiveRelation()` (cebe)
- Chg: The scripts in asset bundles are now registered in `View` at the end of `endBody()`. It was done in `endPage()` previously (qiangxue)
- Chg: Renamed `csrf-var` to `csrf-param` for CSRF header name (Dilip)
......
......@@ -7,7 +7,6 @@
*/
namespace yii\db;
use yii\base\Object;
/**
* ActiveQuery represents a DB query associated with an Active Record class.
......@@ -69,7 +68,7 @@ use yii\base\Object;
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationInterface
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
......@@ -454,14 +453,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface, ActiveRelationI
* Joins a parent query with a child query.
* The current query object will be modified accordingly.
* @param ActiveQuery $parent
* @param ActiveRelationInterface $child
* @param ActiveQuery $child
* @param string $joinType
*/
private function joinWithRelation($parent, $child, $joinType)
{
$via = $child->via;
$child->via = null;
if ($via instanceof ActiveRelationInterface) {
if ($via instanceof ActiveQuery) {
// via table
$this->joinWithRelation($parent, $via, $joinType);
$this->joinWithRelation($via, $child, $joinType);
......
......@@ -10,7 +10,11 @@ namespace yii\db;
/**
* ActiveQueryInterface defines the common interface to be implemented by active record query classes.
*
* A class implementing this interface should also use [[ActiveQueryTrait]].
* That are methods for either normal queries that return active records but also relational queries
* in which the query represents a relation between two active record classes and will return related
* records only.
*
* A class implementing this interface should also use [[ActiveQueryTrait]] and [[ActiveRelationTrait]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
......@@ -74,4 +78,22 @@ interface ActiveQueryInterface extends QueryInterface
* @return static the query object itself
*/
public function with();
/**
* Specifies the relation associated with the pivot table for use in relational query.
* @param string $relationName the relation name. This refers to a relation declared in the [[ActiveRelationTrait::primaryModel|primaryModel]] of the relation.
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return static the relation object itself.
*/
public function via($relationName, $callable = null);
/**
* Finds the related records for the specified primary record.
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
* @param string $name the relation name
* @param ActiveRecordInterface $model the primary model
* @return mixed the related record(s)
*/
public function findFor($name, $model);
}
......@@ -169,7 +169,7 @@ trait ActiveQueryTrait
/**
* @param ActiveRecord $model
* @param array $with
* @return ActiveRelationInterface[]
* @return ActiveQueryInterface[]
*/
private function normalizeRelations($model, $with)
{
......
......@@ -261,11 +261,11 @@ interface ActiveRecordInterface
/**
* Returns the relation object with the specified name.
* A relation is defined by a getter method which returns an object implementing the [[ActiveRelationInterface]]
* (normally this would be an [[ActiveQuery]] object).
* A relation is defined by a getter method which returns an object implementing the [[ActiveQueryInterface]]
* (normally this would be a relational [[ActiveQuery]] object).
* It can be declared in either the ActiveRecord class itself or one of its behaviors.
* @param string $name the relation name
* @return ActiveRelationInterface the relation object
* @return ActiveQueryInterface the relational query object
*/
public function getRelation($name);
......@@ -285,7 +285,7 @@ interface ActiveRecordInterface
* @param static $model the record to be linked with the current one.
* @param array $extraColumns additional column values to be saved into the pivot table.
* This parameter is only meaningful for a relationship involving a pivot table
* (i.e., a relation set with `[[ActiveRelationInterface::via()]]`.)
* (i.e., a relation set with `[[ActiveQueryInterface::via()]]`.)
*/
public function link($name, $model, $extraColumns = []);
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\db;
use yii\base\InvalidParamException;
/**
* ActiveRelationInterface defines the common interface to be implemented by relational active record query classes.
*
* A class implementing this interface should also use [[ActiveRelationTrait]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
*/
interface ActiveRelationInterface extends ActiveQueryInterface
{
/**
* Specifies the relation associated with the pivot table.
* @param string $relationName the relation name. This refers to a relation declared in the [[ActiveRelationTrait::primaryModel|primaryModel]] of the relation.
* @param callable $callable a PHP callback for customizing the relation associated with the pivot table.
* Its signature should be `function($query)`, where `$query` is the query to be customized.
* @return static the relation object itself.
*/
public function via($relationName, $callable = null);
/**
* Finds the related records for the specified primary record.
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
* @param string $name the relation name
* @param ActiveRecordInterface $model the primary model
* @return mixed the related record(s)
* @throws InvalidParamException if the relation is invalid
*/
public function findFor($name, $model);
}
......@@ -230,7 +230,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
return $this->_related[$name];
}
$value = parent::__get($name);
if ($value instanceof ActiveRelationInterface) {
if ($value instanceof ActiveQueryInterface) {
return $this->_related[$name] = $value->findFor($name, $this);
} else {
return $value;
......@@ -315,7 +315,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* @param array $link the primary-foreign key constraint. The keys of the array refer to
* the attributes of the record associated with the `$class` model, while the values of the
* array refer to the corresponding attributes in **this** AR class.
* @return ActiveRelationInterface the relation object.
* @return ActiveQueryInterface the relational query object.
*/
public function hasOne($class, $link)
{
......@@ -356,7 +356,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* @param array $link the primary-foreign key constraint. The keys of the array refer to
* the attributes of the record associated with the `$class` model, while the values of the
* array refer to the corresponding attributes in **this** AR class.
* @return ActiveRelationInterface the relation object.
* @return ActiveQueryInterface the relational query object.
*/
public function hasMany($class, $link)
{
......@@ -1037,10 +1037,10 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
/**
* Returns the relation object with the specified name.
* A relation is defined by a getter method which returns an [[ActiveRelationInterface]] object.
* A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object.
* It can be declared in either the Active Record class itself or one of its behaviors.
* @param string $name the relation name
* @return ActiveRelationInterface|ActiveQuery the relation object
* @return ActiveQueryInterface|ActiveQuery the relational query object
* @throws InvalidParamException if the named relation does not exist.
*/
public function getRelation($name)
......@@ -1052,7 +1052,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
} catch (UnknownMethodException $e) {
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
}
if (!$relation instanceof ActiveRelationInterface) {
if (!$relation instanceof ActiveQueryInterface) {
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment