Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
130b6346
Commit
130b6346
authored
Sep 24, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis WIP
- relation support - completed and refactored lua script builder
parent
e62e8487
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
62 deletions
+113
-62
ActiveQuery.php
framework/yii/redis/ActiveQuery.php
+32
-44
ActiveRelation.php
framework/yii/redis/ActiveRelation.php
+70
-7
LuaScriptBuilder.php
framework/yii/redis/LuaScriptBuilder.php
+0
-0
ActiveRecordTest.php
tests/unit/framework/redis/ActiveRecordTest.php
+11
-11
No files found.
framework/yii/redis/ActiveQuery.php
View file @
130b6346
...
...
@@ -9,6 +9,7 @@
*/
namespace
yii\redis
;
use
yii\base\NotSupportedException
;
/**
* ActiveQuery represents a DB query associated with an Active Record class.
...
...
@@ -89,18 +90,30 @@ class ActiveQuery extends \yii\base\Component
public
$indexBy
;
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
* Executes a script created by [[LuaScriptBuilder]]
* @param $type
* @param null $column
* @return array|bool|null|string
*/
p
ublic
function
all
(
)
p
rivate
function
executeScript
(
$type
,
$column
=
null
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildAll
(
$this
);
$data
=
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
$method
=
'build'
.
$type
;
$script
=
$db
->
getLuaScriptBuilder
()
->
$method
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
}
/**
* Executes query and returns all results as an array.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public
function
all
()
{
// TODO add support for orderBy
$data
=
$this
->
executeScript
(
'All'
);
$rows
=
array
();
foreach
(
$data
as
$dataRow
)
{
$row
=
array
();
...
...
@@ -130,13 +143,8 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
one
()
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildOne
(
$this
);
$data
=
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
// TODO add support for orderBy
$data
=
$this
->
executeScript
(
'One'
);
if
(
$data
===
array
())
{
return
null
;
}
...
...
@@ -168,11 +176,7 @@ class ActiveQuery extends \yii\base\Component
public
function
column
(
$column
)
{
// TODO add support for indexBy and orderBy
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildColumn
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Column'
,
$column
);
}
/**
...
...
@@ -183,14 +187,13 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
count
()
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
if
(
$this
->
offset
===
null
&&
$this
->
limit
===
null
&&
$this
->
where
===
null
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
return
$db
->
executeCommand
(
'LLEN'
,
array
(
$modelClass
::
tableName
()));
}
else
{
$script
=
$db
->
luaScriptBuilder
->
buildCount
(
$this
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Count'
);
}
}
...
...
@@ -201,11 +204,7 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
sum
(
$column
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildSum
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Sum'
,
$column
);
}
/**
...
...
@@ -216,11 +215,7 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
average
(
$column
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildAverage
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Average'
,
$column
);
}
/**
...
...
@@ -231,11 +226,7 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
min
(
$column
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildMin
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Min'
,
$column
);
}
/**
...
...
@@ -246,11 +237,7 @@ class ActiveQuery extends \yii\base\Component
*/
public
function
max
(
$column
)
{
$modelClass
=
$this
->
modelClass
;
/** @var Connection $db */
$db
=
$modelClass
::
getDb
();
$script
=
$db
->
luaScriptBuilder
->
buildMax
(
$this
,
$column
);
return
$db
->
executeCommand
(
'EVAL'
,
array
(
$script
,
0
));
return
$this
->
executeScript
(
'Max'
,
$column
);
}
/**
...
...
@@ -294,7 +281,7 @@ class ActiveQuery extends \yii\base\Component
* (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return Query the query object itself
* @return
Active
Query the query object itself
* @see addOrderBy()
*/
public
function
orderBy
(
$columns
)
...
...
@@ -310,7 +297,7 @@ class ActiveQuery extends \yii\base\Component
* (e.g. `array('id' => Query::SORT_ASC, 'name' => Query::SORT_DESC)`).
* The method will automatically quote the column names unless a column contains some parenthesis
* (which means the column contains a DB expression).
* @return Query the query object itself
* @return
Active
Query the query object itself
* @see orderBy()
*/
public
function
addOrderBy
(
$columns
)
...
...
@@ -326,6 +313,7 @@ class ActiveQuery extends \yii\base\Component
protected
function
normalizeOrderBy
(
$columns
)
{
throw
new
NotSupportedException
(
'orderBy is currently not supported'
);
if
(
is_array
(
$columns
))
{
return
$columns
;
}
else
{
...
...
framework/yii/redis/ActiveRelation.php
View file @
130b6346
...
...
@@ -9,6 +9,7 @@
*/
namespace
yii\redis
;
use
yii\base\InvalidConfigException
;
/**
* ActiveRecord is the base class for classes representing relational data in terms of objects.
...
...
@@ -42,20 +43,70 @@ class ActiveRelation extends \yii\redis\ActiveQuery
public
$via
;
/**
* Clones internal objects.
*/
public
function
__clone
()
{
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
)
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
)
{
// lazy loading
if
(
$this
->
via
instanceof
self
)
{
// via pivot table
$viaModels
=
$this
->
via
->
findPivotRows
(
array
(
$this
->
primaryModel
));
$this
->
filterByModels
(
$viaModels
);
}
elseif
(
is_array
(
$this
->
via
))
{
// via relation
/** @var $viaQuery ActiveRelation */
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
if
(
$viaQuery
->
multiple
)
{
$viaModels
=
$viaQuery
->
all
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$viaModels
);
}
else
{
$model
=
$viaQuery
->
one
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$model
);
$viaModels
=
$model
===
null
?
array
()
:
array
(
$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 by [[ActiveQuery]]. Do not call it directly.
* 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
...
...
@@ -68,14 +119,12 @@ class ActiveRelation extends \yii\redis\ActiveQuery
}
if
(
$this
->
via
instanceof
self
)
{
// TODO
// via pivot table
/** @var $viaQuery ActiveRelation */
$viaQuery
=
$this
->
via
;
$viaModels
=
$viaQuery
->
findPivotRows
(
$primaryModels
);
$this
->
filterByModels
(
$viaModels
);
}
elseif
(
is_array
(
$this
->
via
))
{
// TODO
// via relation
/** @var $viaQuery ActiveRelation */
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
...
...
@@ -185,7 +234,6 @@ class ActiveRelation extends \yii\redis\ActiveQuery
}
}
/**
* @param array $models
*/
...
...
@@ -193,7 +241,7 @@ class ActiveRelation extends \yii\redis\ActiveQuery
{
$attributes
=
array_keys
(
$this
->
link
);
$values
=
array
();
if
(
count
(
$attributes
)
===
1
)
{
if
(
count
(
$attributes
)
===
1
)
{
// single key
$attribute
=
reset
(
$this
->
link
);
foreach
(
$models
as
$model
)
{
...
...
@@ -209,7 +257,22 @@ class ActiveRelation extends \yii\redis\ActiveQuery
$values
[]
=
$v
;
}
}
$this
->
primaryKeys
(
$values
);
$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
);
/** @var $primaryModel ActiveRecord */
$primaryModel
=
reset
(
$primaryModels
);
$db
=
$primaryModel
->
getDb
();
// TODO use different db in db overlapping relations
return
$this
->
all
();
}
}
framework/yii/redis/LuaScriptBuilder.php
View file @
130b6346
This diff is collapsed.
Click to expand it.
tests/unit/framework/redis/ActiveRecordTest.php
View file @
130b6346
...
...
@@ -239,17 +239,17 @@ class ActiveRecordTest extends RedisTestCase
$this
->
assertFalse
(
Customer
::
find
()
->
where
(
array
(
'id'
=>
5
))
->
exists
());
}
//
public function testFindLazy()
//
{
//
/** @var $customer Customer */
//
$customer = Customer::find(2);
//
$orders = $customer->orders;
//
$this->assertEquals(2, count($orders));
//
// $orders = $customer->getOrders()->primaryKeys(array(
3))->all();
//
$this->assertEquals(1, count($orders));
//
$this->assertEquals(3, $orders[0]->id);
//
}
public
function
testFindLazy
()
{
/** @var $customer Customer */
$customer
=
Customer
::
find
(
2
);
$orders
=
$customer
->
orders
;
$this
->
assertEquals
(
2
,
count
(
$orders
));
$orders
=
$customer
->
getOrders
()
->
where
(
array
(
'id'
=>
3
))
->
all
();
$this
->
assertEquals
(
1
,
count
(
$orders
));
$this
->
assertEquals
(
3
,
$orders
[
0
]
->
id
);
}
// public function testFindEager()
// {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment