Commit 8542448f by Carsten Brandt

refactored redis AR to relect the latest changes

- make use of traits - short array - better implementation of query findByPk
parent bc4324c0
...@@ -7,9 +7,8 @@ ...@@ -7,9 +7,8 @@
namespace yii\redis; namespace yii\redis;
use yii\base\InvalidConfigException; use yii\db\ActiveRelationInterface;
use yii\db\ActiveRelationTrait;
// TODO this class is nearly completely duplicated from yii\db\ActiveRelation
/** /**
* ActiveRelation represents a relation between two Active Record classes. * ActiveRelation represents a relation between two Active Record classes.
...@@ -26,76 +25,29 @@ use yii\base\InvalidConfigException; ...@@ -26,76 +25,29 @@ use yii\base\InvalidConfigException;
* @author Carsten Brandt <mail@cebe.cc> * @author Carsten Brandt <mail@cebe.cc>
* @since 2.0 * @since 2.0
*/ */
class ActiveRelation extends ActiveQuery class ActiveRelation extends ActiveQuery implements ActiveRelationInterface
{ {
/** use ActiveRelationTrait;
* @var boolean whether this relation should populate all query results into AR instances.
* If false, only the first row of the results will be retrieved.
*/
public $multiple;
/**
* @var ActiveRecord the primary model that this relation is associated with.
* This is used only in lazy loading with dynamic query options.
*/
public $primaryModel;
/**
* @var array the columns of the primary and foreign tables that establish the relation.
* The array keys must be columns of the table for this relation, and the array values
* must be the corresponding columns from the primary table.
* Do not prefix or quote the column names as this will be done automatically by Yii.
*/
public $link;
/**
* @var array|ActiveRelation the query associated with the pivot table. Please call [[via()]]
* or [[viaTable()]] to set this property instead of directly setting it.
*/
public $via;
/** /**
* Clones internal objects. * Executes a script created by [[LuaScriptBuilder]]
* @param Connection $db the database connection used to execute the query.
* If this parameter is not given, the `db` application component will be used.
* @param string $type the type of the script to generate
* @param null $column
* @return array|bool|null|string
*/ */
public function __clone() protected function executeScript($db, $type, $column=null)
{
if (is_object($this->via)) {
// make a clone of "via" object so that the same query object can be reused multiple times
$this->via = clone $this->via;
}
}
/**
* Specifies the relation associated with the pivot table.
* @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]].
* @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 ActiveRelation the relation object itself.
*/
public function via($relationName, $callable = null)
{
$relation = $this->primaryModel->getRelation($relationName);
$this->via = array($relationName, $relation);
if ($callable !== null) {
call_user_func($callable, $relation);
}
return $this;
}
/**
* Creates a DB command that can be used to execute this query.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return Command the created DB command instance.
*/
protected function executeScript($type, $column=null)
{ {
if ($this->primaryModel !== null) { if ($this->primaryModel !== null) {
// lazy loading // lazy loading
if ($this->via instanceof self) { if ($this->via instanceof self) {
// via pivot table // via pivot table
$viaModels = $this->via->findPivotRows(array($this->primaryModel)); $viaModels = $this->via->findPivotRows([$this->primaryModel]);
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} elseif (is_array($this->via)) { } elseif (is_array($this->via)) {
// via relation // via relation
/** @var $viaQuery ActiveRelation */ /** @var ActiveRelation $viaQuery */
list($viaName, $viaQuery) = $this->via; list($viaName, $viaQuery) = $this->via;
if ($viaQuery->multiple) { if ($viaQuery->multiple) {
$viaModels = $viaQuery->all(); $viaModels = $viaQuery->all();
...@@ -103,187 +55,13 @@ class ActiveRelation extends ActiveQuery ...@@ -103,187 +55,13 @@ class ActiveRelation extends ActiveQuery
} else { } else {
$model = $viaQuery->one(); $model = $viaQuery->one();
$this->primaryModel->populateRelation($viaName, $model); $this->primaryModel->populateRelation($viaName, $model);
$viaModels = $model === null ? array() : array($model); $viaModels = $model === null ? [] : [$model];
}
$this->filterByModels($viaModels);
} else {
$this->filterByModels(array($this->primaryModel));
}
}
return parent::executeScript($type, $column);
}
/**
* Finds the related records and populates them into the primary models.
* This method is internally used by [[ActiveQuery]]. Do not call it directly.
* @param string $name the relation name
* @param array $primaryModels primary models
* @return array the related models
* @throws InvalidConfigException
*/
public function findWith($name, &$primaryModels)
{
if (!is_array($this->link)) {
throw new InvalidConfigException('Invalid link: it must be an array of key-value pairs.');
} }
if ($this->via instanceof self) {
// via pivot table
/** @var $viaQuery ActiveRelation */
$viaQuery = $this->via;
$viaModels = $viaQuery->findPivotRows($primaryModels);
$this->filterByModels($viaModels); $this->filterByModels($viaModels);
} elseif (is_array($this->via)) {
// via relation
/** @var $viaQuery ActiveRelation */
list($viaName, $viaQuery) = $this->via;
$viaQuery->primaryModel = null;
$viaModels = $viaQuery->findWith($viaName, $primaryModels);
$this->filterByModels($viaModels);
} else {
$this->filterByModels($primaryModels);
}
if (count($primaryModels) === 1 && !$this->multiple) {
$model = $this->one();
foreach ($primaryModels as $i => $primaryModel) {
if ($primaryModel instanceof ActiveRecord) {
$primaryModel->populateRelation($name, $model);
} else {
$primaryModels[$i][$name] = $model;
}
}
return array($model);
} else {
$models = $this->all();
if (isset($viaModels, $viaQuery)) {
$buckets = $this->buildBuckets($models, $this->link, $viaModels, $viaQuery->link);
} else {
$buckets = $this->buildBuckets($models, $this->link);
}
$link = array_values(isset($viaQuery) ? $viaQuery->link : $this->link);
foreach ($primaryModels as $i => $primaryModel) {
$key = $this->getModelKey($primaryModel, $link);
$value = isset($buckets[$key]) ? $buckets[$key] : ($this->multiple ? array() : null);
if ($primaryModel instanceof ActiveRecord) {
$primaryModel->populateRelation($name, $value);
} else {
$primaryModels[$i][$name] = $value;
}
}
return $models;
}
}
/**
* @param array $models
* @param array $link
* @param array $viaModels
* @param array $viaLink
* @return array
*/
private function buildBuckets($models, $link, $viaModels = null, $viaLink = null)
{
$buckets = array();
$linkKeys = array_keys($link);
foreach ($models as $i => $model) {
$key = $this->getModelKey($model, $linkKeys);
if ($this->indexBy !== null) {
$buckets[$key][$i] = $model;
} else {
$buckets[$key][] = $model;
}
}
if ($viaModels !== null) {
$viaBuckets = array();
$viaLinkKeys = array_keys($viaLink);
$linkValues = array_values($link);
foreach ($viaModels as $viaModel) {
$key1 = $this->getModelKey($viaModel, $viaLinkKeys);
$key2 = $this->getModelKey($viaModel, $linkValues);
if (isset($buckets[$key2])) {
foreach ($buckets[$key2] as $i => $bucket) {
if ($this->indexBy !== null) {
$viaBuckets[$key1][$i] = $bucket;
} else {
$viaBuckets[$key1][] = $bucket;
}
}
}
}
$buckets = $viaBuckets;
}
if (!$this->multiple) {
foreach ($buckets as $i => $bucket) {
$buckets[$i] = reset($bucket);
}
}
return $buckets;
}
/**
* @param ActiveRecord|array $model
* @param array $attributes
* @return string
*/
private function getModelKey($model, $attributes)
{
if (count($attributes) > 1) {
$key = array();
foreach ($attributes as $attribute) {
$key[] = $model[$attribute];
}
return serialize($key);
} else { } else {
$attribute = reset($attributes); $this->filterByModels([$this->primaryModel]);
return $model[$attribute];
}
} }
/**
* @param array $models
*/
private function filterByModels($models)
{
$attributes = array_keys($this->link);
$values = array();
if (count($attributes) === 1) {
// single key
$attribute = reset($this->link);
foreach ($models as $model) {
if (($value = $model[$attribute]) !== null) {
$values[] = $value;
}
}
} else {
// composite keys
foreach ($models as $model) {
$v = array();
foreach ($this->link as $attribute => $link) {
$v[$attribute] = $model[$link];
}
$values[] = $v;
}
}
$this->andWhere(array('in', $attributes, array_unique($values, SORT_REGULAR)));
}
/**
* @param ActiveRecord[] $primaryModels
* @return array
*/
private function findPivotRows($primaryModels)
{
if (empty($primaryModels)) {
return array();
} }
$this->filterByModels($primaryModels); return parent::executeScript($db, $type, $column);
/** @var $primaryModel ActiveRecord */
$primaryModel = reset($primaryModels);
$db = $primaryModel->getDb(); // TODO use different db in db overlapping relations
return $this->all();
} }
} }
...@@ -129,7 +129,7 @@ class LuaScriptBuilder extends \yii\base\Object ...@@ -129,7 +129,7 @@ class LuaScriptBuilder extends \yii\base\Object
*/ */
private function build($query, $buildResult, $return) private function build($query, $buildResult, $return)
{ {
$columns = array(); $columns = [];
if ($query->where !== null) { if ($query->where !== null) {
$condition = $this->buildCondition($query->where, $columns); $condition = $this->buildCondition($query->where, $columns);
} else { } else {
...@@ -206,7 +206,7 @@ EOF; ...@@ -206,7 +206,7 @@ EOF;
*/ */
public function buildCondition($condition, &$columns) public function buildCondition($condition, &$columns)
{ {
static $builders = array( static $builders = [
'and' => 'buildAndCondition', 'and' => 'buildAndCondition',
'or' => 'buildAndCondition', 'or' => 'buildAndCondition',
'between' => 'buildBetweenCondition', 'between' => 'buildBetweenCondition',
...@@ -217,7 +217,7 @@ EOF; ...@@ -217,7 +217,7 @@ EOF;
'not like' => 'buildLikeCondition', 'not like' => 'buildLikeCondition',
'or like' => 'buildLikeCondition', 'or like' => 'buildLikeCondition',
'or not like' => 'buildLikeCondition', 'or not like' => 'buildLikeCondition',
); ];
if (!is_array($condition)) { if (!is_array($condition)) {
throw new NotSupportedException('Where must be an array.'); throw new NotSupportedException('Where must be an array.');
...@@ -238,10 +238,10 @@ EOF; ...@@ -238,10 +238,10 @@ EOF;
private function buildHashCondition($condition, &$columns) private function buildHashCondition($condition, &$columns)
{ {
$parts = array(); $parts = [];
foreach ($condition as $column => $value) { foreach ($condition as $column => $value) {
if (is_array($value)) { // IN condition if (is_array($value)) { // IN condition
$parts[] = $this->buildInCondition('in', array($column, $value), $columns); $parts[] = $this->buildInCondition('in', [$column, $value], $columns);
} else { } else {
$column = $this->addColumn($column, $columns); $column = $this->addColumn($column, $columns);
if ($value === null) { if ($value === null) {
...@@ -259,7 +259,7 @@ EOF; ...@@ -259,7 +259,7 @@ EOF;
private function buildAndCondition($operator, $operands, &$columns) private function buildAndCondition($operator, $operands, &$columns)
{ {
$parts = array(); $parts = [];
foreach ($operands as $operand) { foreach ($operands as $operand) {
if (is_array($operand)) { if (is_array($operand)) {
$operand = $this->buildCondition($operand, $columns); $operand = $this->buildCondition($operand, $columns);
...@@ -299,7 +299,7 @@ EOF; ...@@ -299,7 +299,7 @@ EOF;
$values = (array)$values; $values = (array)$values;
if (empty($values) || $column === array()) { if (empty($values) || $column === []) {
return $operator === 'in' ? 'false' : 'true'; return $operator === 'in' ? 'false' : 'true';
} }
...@@ -309,7 +309,7 @@ EOF; ...@@ -309,7 +309,7 @@ EOF;
$column = reset($column); $column = reset($column);
} }
$columnAlias = $this->addColumn($column, $columns); $columnAlias = $this->addColumn($column, $columns);
$parts = array(); $parts = [];
foreach ($values as $i => $value) { foreach ($values as $i => $value) {
if (is_array($value)) { if (is_array($value)) {
$value = isset($value[$column]) ? $value[$column] : null; $value = isset($value[$column]) ? $value[$column] : null;
...@@ -329,9 +329,9 @@ EOF; ...@@ -329,9 +329,9 @@ EOF;
protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns) protected function buildCompositeInCondition($operator, $inColumns, $values, &$columns)
{ {
$vss = array(); $vss = [];
foreach ($values as $value) { foreach ($values as $value) {
$vs = array(); $vs = [];
foreach ($inColumns as $column) { foreach ($inColumns as $column) {
$column = $this->addColumn($column, $columns); $column = $this->addColumn($column, $columns);
if (isset($value[$column])) { if (isset($value[$column])) {
...@@ -370,7 +370,7 @@ EOF; ...@@ -370,7 +370,7 @@ EOF;
$column = $this->addColumn($column, $columns); $column = $this->addColumn($column, $columns);
$parts = array(); $parts = [];
foreach ($values as $value) { foreach ($values as $value) {
// TODO implement matching here correctly // TODO implement matching here correctly
$value = $this->quoteValue($value); $value = $this->quoteValue($value);
......
...@@ -41,7 +41,7 @@ class RecordSchema extends TableSchema ...@@ -41,7 +41,7 @@ class RecordSchema extends TableSchema
throw new InvalidConfigException('primaryKey of RecordSchema must not be empty.'); throw new InvalidConfigException('primaryKey of RecordSchema must not be empty.');
} }
if (!is_array($this->primaryKey)) { if (!is_array($this->primaryKey)) {
$this->primaryKey = array($this->primaryKey); $this->primaryKey = [$this->primaryKey];
} }
foreach($this->primaryKey as $pk) { foreach($this->primaryKey as $pk) {
if (!isset($this->columns[$pk])) { if (!isset($this->columns[$pk])) {
......
...@@ -16,12 +16,12 @@ class Customer extends ActiveRecord ...@@ -16,12 +16,12 @@ class Customer extends ActiveRecord
*/ */
public function getOrders() public function getOrders()
{ {
return $this->hasMany('Order', array('customer_id' => 'id')); return $this->hasMany(Order::className(), ['customer_id' => 'id']);
} }
public static function active($query) public static function active($query)
{ {
$query->andWhere(array('status' => 1)); $query->andWhere(['status' => 1]);
} }
public static function getRecordSchema() public static function getRecordSchema()
......
...@@ -8,15 +8,15 @@ class Item extends ActiveRecord ...@@ -8,15 +8,15 @@ class Item extends ActiveRecord
{ {
public static function getRecordSchema() public static function getRecordSchema()
{ {
return new RecordSchema(array( return new RecordSchema([
'name' => 'item', 'name' => 'item',
'primaryKey' => array('id'), 'primaryKey' => ['id'],
'sequenceName' => 'id', 'sequenceName' => 'id',
'columns' => array( 'columns' => [
'id' => 'integer', 'id' => 'integer',
'name' => 'string', 'name' => 'string',
'category_id' => 'integer' 'category_id' => 'integer'
) ]
)); ]);
} }
} }
\ No newline at end of file
...@@ -8,17 +8,17 @@ class Order extends ActiveRecord ...@@ -8,17 +8,17 @@ class Order extends ActiveRecord
{ {
public function getCustomer() public function getCustomer()
{ {
return $this->hasOne('Customer', array('id' => 'customer_id')); return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
} }
public function getOrderItems() public function getOrderItems()
{ {
return $this->hasMany('OrderItem', array('order_id' => 'id')); return $this->hasMany(OrderItem::className(), ['order_id' => 'id']);
} }
public function getItems() public function getItems()
{ {
return $this->hasMany('Item', array('id' => 'item_id')) return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', function($q) { ->via('orderItems', function($q) {
// additional query configuration // additional query configuration
}); });
...@@ -26,9 +26,9 @@ class Order extends ActiveRecord ...@@ -26,9 +26,9 @@ class Order extends ActiveRecord
public function getBooks() public function getBooks()
{ {
return $this->hasMany('Item', array('id' => 'item_id')) return $this->hasMany(Item::className(), ['id' => 'item_id'])
->via('orderItems', array('order_id' => 'id')); ->via('orderItems', ['order_id' => 'id']);
//->where(array('category_id' => 1)); //->where(['category_id' => 1]);
} }
public function beforeSave($insert) public function beforeSave($insert)
...@@ -46,7 +46,7 @@ class Order extends ActiveRecord ...@@ -46,7 +46,7 @@ class Order extends ActiveRecord
{ {
return new RecordSchema(array( return new RecordSchema(array(
'name' => 'orders', 'name' => 'orders',
'primaryKey' => array('id'), 'primaryKey' => ['id'],
'columns' => array( 'columns' => array(
'id' => 'integer', 'id' => 'integer',
'customer_id' => 'integer', 'customer_id' => 'integer',
......
...@@ -8,19 +8,19 @@ class OrderItem extends ActiveRecord ...@@ -8,19 +8,19 @@ class OrderItem extends ActiveRecord
{ {
public function getOrder() public function getOrder()
{ {
return $this->hasOne('Order', array('id' => 'order_id')); return $this->hasOne(Order::className(), ['id' => 'order_id']);
} }
public function getItem() public function getItem()
{ {
return $this->hasOne('Item', array('id' => 'item_id')); return $this->hasOne(Item::className(), ['id' => 'item_id']);
} }
public static function getRecordSchema() public static function getRecordSchema()
{ {
return new RecordSchema(array( return new RecordSchema(array(
'name' => 'order_item', 'name' => 'order_item',
'primaryKey' => array('order_id', 'item_id'), 'primaryKey' => ['order_id', 'item_id'],
'columns' => array( 'columns' => array(
'order_id' => 'integer', 'order_id' => 'integer',
'item_id' => 'integer', 'item_id' => 'integer',
......
...@@ -4,6 +4,9 @@ namespace yiiunit\framework\redis; ...@@ -4,6 +4,9 @@ namespace yiiunit\framework\redis;
use yii\redis\Connection; use yii\redis\Connection;
/**
* @group redis
*/
class RedisConnectionTest extends RedisTestCase class RedisConnectionTest extends RedisTestCase
{ {
/** /**
......
...@@ -8,7 +8,7 @@ use yiiunit\TestCase; ...@@ -8,7 +8,7 @@ use yiiunit\TestCase;
/** /**
* RedisTestCase is the base class for all redis related test cases * RedisTestCase is the base class for all redis related test cases
*/ */
class RedisTestCase extends TestCase abstract class RedisTestCase extends TestCase
{ {
protected function setUp() protected function setUp()
{ {
......
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