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
983b2286
Commit
983b2286
authored
Nov 24, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
elasticsearch AR relations + null values
parent
58b1538b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
106 additions
and
41 deletions
+106
-41
ActiveQuery.php
framework/yii/elasticsearch/ActiveQuery.php
+34
-0
ActiveRecord.php
framework/yii/elasticsearch/ActiveRecord.php
+16
-6
QueryBuilder.php
framework/yii/elasticsearch/QueryBuilder.php
+42
-19
Customer.php
tests/unit/data/ar/elasticsearch/Customer.php
+1
-1
Order.php
tests/unit/data/ar/elasticsearch/Order.php
+11
-13
OrderItem.php
tests/unit/data/ar/elasticsearch/OrderItem.php
+2
-2
ActiveRecordTest.php
tests/unit/framework/elasticsearch/ActiveRecordTest.php
+0
-0
No files found.
framework/yii/elasticsearch/ActiveQuery.php
View file @
983b2286
...
...
@@ -129,4 +129,38 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
return
$model
;
}
/**
* @inheritDocs
*/
public
function
scalar
(
$field
,
$db
=
null
)
{
$record
=
parent
::
one
(
$db
);
if
(
$record
!==
false
)
{
if
(
$field
==
'primaryKey'
)
{
return
$record
[
'_id'
];
}
elseif
(
isset
(
$record
[
'_source'
][
$field
]))
{
return
$record
[
'_source'
][
$field
];
}
}
return
null
;
}
/**
* @inheritDocs
*/
public
function
column
(
$field
,
$db
=
null
)
{
if
(
$field
==
'primaryKey'
)
{
$command
=
$this
->
createCommand
(
$db
);
$command
->
queryParts
[
'fields'
]
=
[];
$rows
=
$command
->
queryAll
()[
'hits'
];
$result
=
[];
foreach
(
$rows
as
$row
)
{
$result
[]
=
$row
[
'_id'
];
}
return
$result
;
}
return
parent
::
column
(
$field
,
$db
);
}
}
framework/yii/elasticsearch/ActiveRecord.php
View file @
983b2286
...
...
@@ -312,16 +312,21 @@ class ActiveRecord extends \yii\db\ActiveRecord
* @param array $attributes attribute values (name-value pairs) to be saved into the table
* @param array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @param array $params this parameter is ignored in
redis
implementation.
* @param array $params this parameter is ignored in
elasticsearch
implementation.
* @return integer the number of rows updated
*/
public
static
function
updateAll
(
$attributes
,
$condition
=
[],
$params
=
[])
{
if
(
empty
(
$condition
))
{
if
(
count
(
$condition
)
==
1
&&
isset
(
$condition
[
'primaryKey'
]))
{
$primaryKeys
=
(
array
)
$condition
[
'primaryKey'
];
}
else
{
$primaryKeys
=
static
::
find
()
->
where
(
$condition
)
->
column
(
'primaryKey'
);
}
if
(
empty
(
$primaryKeys
))
{
return
0
;
}
$bulk
=
''
;
foreach
((
array
)
$
condition
as
$pk
)
{
foreach
((
array
)
$
primaryKeys
as
$pk
)
{
$action
=
Json
::
encode
([
"update"
=>
[
"_id"
=>
$pk
,
...
...
@@ -362,16 +367,21 @@ class ActiveRecord extends \yii\db\ActiveRecord
*
* @param array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @param array $params this parameter is ignored in
redis
implementation.
* @param array $params this parameter is ignored in
elasticsearch
implementation.
* @return integer the number of rows deleted
*/
public
static
function
deleteAll
(
$condition
=
[],
$params
=
[])
{
if
(
empty
(
$condition
))
{
if
(
count
(
$condition
)
==
1
&&
isset
(
$condition
[
'primaryKey'
]))
{
$primaryKeys
=
(
array
)
$condition
[
'primaryKey'
];
}
else
{
$primaryKeys
=
static
::
find
()
->
where
(
$condition
)
->
column
(
'primaryKey'
);
}
if
(
empty
(
$primaryKeys
))
{
return
0
;
}
$bulk
=
''
;
foreach
((
array
)
$
condition
as
$pk
)
{
foreach
((
array
)
$
primaryKeys
as
$pk
)
{
$bulk
.=
Json
::
encode
([
"delete"
=>
[
"_id"
=>
$pk
,
...
...
framework/yii/elasticsearch/QueryBuilder.php
View file @
983b2286
...
...
@@ -152,13 +152,21 @@ class QueryBuilder extends \yii\base\Object
{
$parts
=
[];
foreach
(
$condition
as
$attribute
=>
$value
)
{
if
(
is_array
(
$value
))
{
// IN condition
$parts
[]
=
[
'in'
=>
[
$attribute
=>
$value
]];
if
(
$attribute
==
'primaryKey'
)
{
if
(
$value
==
null
)
{
// there is no null pk
$parts
[]
=
[
'script'
=>
[
'script'
=>
'0==1'
]];
}
else
{
$parts
[]
=
[
'ids'
=>
[
'values'
=>
is_array
(
$value
)
?
$value
:
[
$value
]]];
}
}
else
{
if
(
$value
===
null
)
{
$parts
[]
=
[
'
missing'
=>
[
'field'
=>
$attribute
,
'existence'
=>
true
,
'null_value'
=>
tr
ue
]];
if
(
is_array
(
$value
))
{
// IN condition
$parts
[]
=
[
'
in'
=>
[
$attribute
=>
$val
ue
]];
}
else
{
$parts
[]
=
[
'term'
=>
[
$attribute
=>
$value
]];
if
(
$value
===
null
)
{
$parts
[]
=
[
'missing'
=>
[
'field'
=>
$attribute
,
'existence'
=>
true
,
'null_value'
=>
true
]];
}
else
{
$parts
[]
=
[
'term'
=>
[
$attribute
=>
$value
]];
}
}
}
}
...
...
@@ -190,6 +198,9 @@ class QueryBuilder extends \yii\base\Object
}
list
(
$column
,
$value1
,
$value2
)
=
$operands
;
if
(
$column
==
'primaryKey'
)
{
throw
new
NotSupportedException
(
'Between condition is not supported for primaryKey.'
);
}
$filter
=
[
'range'
=>
[
$column
=>
[
'gte'
=>
$value1
,
'lte'
=>
$value2
]]];
if
(
$operator
==
'not between'
)
{
$filter
=
[
'not'
=>
$filter
];
...
...
@@ -197,7 +208,7 @@ class QueryBuilder extends \yii\base\Object
return
$filter
;
}
private
function
buildInCondition
(
$operator
,
$operands
,
&
$params
)
private
function
buildInCondition
(
$operator
,
$operands
)
{
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
throw
new
InvalidParamException
(
"Operator '
$operator
' requires two operands."
);
...
...
@@ -208,7 +219,7 @@ class QueryBuilder extends \yii\base\Object
$values
=
(
array
)
$values
;
if
(
empty
(
$values
)
||
$column
===
[])
{
return
$operator
===
'in'
?
[
'script'
=>
[
'script'
=>
'0=1'
]]
:
[];
return
$operator
===
'in'
?
[
'script'
=>
[
'script'
=>
'0=
=
1'
]]
:
[];
}
if
(
count
(
$column
)
>
1
)
{
...
...
@@ -226,21 +237,32 @@ class QueryBuilder extends \yii\base\Object
unset
(
$values
[
$i
]);
}
}
if
(
empty
(
$values
)
&&
$canBeNull
)
{
return
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]];
}
else
{
$filter
=
[
'in'
=>
[
$column
=>
$values
]];
if
(
$canBeNull
)
{
$filter
=
[
'or'
=>
[
$filter
,
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]]]];
if
(
$column
==
'primaryKey'
)
{
if
(
empty
(
$values
)
&&
$canBeNull
)
{
// there is no null pk
$filter
=
[
'script'
=>
[
'script'
=>
'0==1'
]];
}
else
{
$filter
=
[
'ids'
=>
[
'values'
=>
array_values
(
$values
)]];
if
(
$canBeNull
)
{
$filter
=
[
'or'
=>
[
$filter
,
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]]]];
}
}
if
(
$operator
==
'not in'
)
{
$filter
=
[
'not'
=>
$filter
];
}
else
{
if
(
empty
(
$values
)
&&
$canBeNull
)
{
$filter
=
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]];
}
else
{
$filter
=
[
'in'
=>
[
$column
=>
array_values
(
$values
)]];
if
(
$canBeNull
)
{
$filter
=
[
'or'
=>
[
$filter
,
[
'missing'
=>
[
'field'
=>
$column
,
'existence'
=>
true
,
'null_value'
=>
true
]]]];
}
}
return
$filter
;
}
if
(
$operator
==
'not in'
)
{
$filter
=
[
'not'
=>
$filter
];
}
return
$filter
;
}
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
,
&
$params
)
protected
function
buildCompositeInCondition
(
$operator
,
$columns
,
$values
)
{
throw
new
NotSupportedException
(
'composite in is not supported by elasticsearch.'
);
$vss
=
array
();
...
...
@@ -265,8 +287,9 @@ class QueryBuilder extends \yii\base\Object
return
'('
.
implode
(
', '
,
$columns
)
.
")
$operator
("
.
implode
(
', '
,
$vss
)
.
')'
;
}
private
function
buildLikeCondition
(
$operator
,
$operands
,
&
$params
)
private
function
buildLikeCondition
(
$operator
,
$operands
)
{
throw
new
NotSupportedException
(
'like conditions is not supported by elasticsearch.'
);
if
(
!
isset
(
$operands
[
0
],
$operands
[
1
]))
{
throw
new
Exception
(
"Operator '
$operator
' requires two operands."
);
}
...
...
@@ -276,7 +299,7 @@ class QueryBuilder extends \yii\base\Object
$values
=
(
array
)
$values
;
if
(
empty
(
$values
))
{
return
$operator
===
'LIKE'
||
$operator
===
'OR LIKE'
?
'0=1'
:
''
;
return
$operator
===
'LIKE'
||
$operator
===
'OR LIKE'
?
'0=
=
1'
:
''
;
}
if
(
$operator
===
'LIKE'
||
$operator
===
'NOT LIKE'
)
{
...
...
tests/unit/data/ar/elasticsearch/Customer.php
View file @
983b2286
...
...
@@ -24,7 +24,7 @@ class Customer extends ActiveRecord
public
function
getOrders
()
{
return
$this
->
hasMany
(
'Order'
,
array
(
'customer_id'
=>
'id'
))
->
orderBy
(
'id
'
);
return
$this
->
hasMany
(
Order
::
className
(),
array
(
'customer_id'
=>
'primaryKey'
))
->
orderBy
(
'create_time
'
);
}
public
static
function
active
(
$query
)
...
...
tests/unit/data/ar/elasticsearch/Order.php
View file @
983b2286
...
...
@@ -19,33 +19,31 @@ class Order extends ActiveRecord
public
function
getCustomer
()
{
return
$this
->
hasOne
(
'Customer'
,
[
'id
'
=>
'customer_id'
]);
return
$this
->
hasOne
(
Customer
::
className
(),
[
'primaryKey
'
=>
'customer_id'
]);
}
public
function
getOrderItems
()
{
return
$this
->
hasMany
(
'OrderItem'
,
[
'order_id'
=>
'id
'
]);
return
$this
->
hasMany
(
OrderItem
::
className
(),
[
'order_id'
=>
'primaryKey
'
]);
}
public
function
getItems
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
,
function
(
$q
)
{
// additional query configuration
})
->
orderBy
(
'id'
);
return
$this
->
hasMany
(
Item
::
className
(),
[
'primaryKey'
=>
'item_id'
])
->
via
(
'orderItems'
)
->
orderBy
(
'name'
);
}
public
function
getBooks
()
{
return
$this
->
hasMany
(
'Item'
,
[
'id
'
=>
'item_id'
])
->
viaTable
(
'tbl_order_item'
,
[
'order_id'
=>
'id
'
])
->
where
([
'category_id'
=>
1
]);
}
//
public function getBooks()
//
{
// return $this->hasMany('Item', ['primaryKey
' => 'item_id'])
// ->viaTable('tbl_order_item', ['order_id' => 'primaryKey
'])
//
->where(['category_id' => 1]);
//
}
public
function
beforeSave
(
$insert
)
{
if
(
parent
::
beforeSave
(
$insert
))
{
$this
->
create_time
=
time
();
//
$this->create_time = time();
return
true
;
}
else
{
return
false
;
...
...
tests/unit/data/ar/elasticsearch/OrderItem.php
View file @
983b2286
...
...
@@ -19,11 +19,11 @@ class OrderItem extends ActiveRecord
public
function
getOrder
()
{
return
$this
->
hasOne
(
'Order'
,
[
'id
'
=>
'order_id'
]);
return
$this
->
hasOne
(
Order
::
className
(),
[
'primaryKey
'
=>
'order_id'
]);
}
public
function
getItem
()
{
return
$this
->
hasOne
(
'Item'
,
[
'id
'
=>
'item_id'
]);
return
$this
->
hasOne
(
Item
::
className
(),
[
'primaryKey
'
=>
'item_id'
]);
}
}
tests/unit/framework/elasticsearch/ActiveRecordTest.php
View file @
983b2286
This diff is collapsed.
Click to expand it.
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