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
c303ffd6
Commit
c303ffd6
authored
Oct 04, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added ActiveRecord::arrayAttributes to elasticsearch
fixes #3381
parent
1b4d894c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
35 additions
and
5 deletions
+35
-5
ActiveQuery.php
extensions/elasticsearch/ActiveQuery.php
+3
-1
ActiveRecord.php
extensions/elasticsearch/ActiveRecord.php
+24
-2
CHANGELOG.md
extensions/elasticsearch/CHANGELOG.md
+1
-1
Query.php
extensions/elasticsearch/Query.php
+7
-1
No files found.
extensions/elasticsearch/ActiveQuery.php
View file @
c303ffd6
...
@@ -64,11 +64,13 @@ use yii\db\ActiveRelationTrait;
...
@@ -64,11 +64,13 @@ use yii\db\ActiveRelationTrait;
* This methods may only be called in a relational context. Same is true for [[inverseOf()]], which
* This methods may only be called in a relational context. Same is true for [[inverseOf()]], which
* marks a relation as inverse of another relation.
* marks a relation as inverse of another relation.
*
*
* > N
OTE
: elasticsearch limits the number of records returned by any query to 10 records by default.
* > N
ote
: elasticsearch limits the number of records returned by any query to 10 records by default.
* > If you expect to get more records you should specify limit explicitly in relation definition.
* > If you expect to get more records you should specify limit explicitly in relation definition.
* > This is also important for relations that use [[via()]] so that if via records are limited to 10
* > This is also important for relations that use [[via()]] so that if via records are limited to 10
* > the relations records can also not be more than 10.
* > the relations records can also not be more than 10.
*
*
* > Note: Currently [[with]] is not supported in combination with [[asArray]].
*
* @author Carsten Brandt <mail@cebe.cc>
* @author Carsten Brandt <mail@cebe.cc>
* @since 2.0
* @since 2.0
*/
*/
...
...
extensions/elasticsearch/ActiveRecord.php
View file @
c303ffd6
...
@@ -270,6 +270,7 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -270,6 +270,7 @@ class ActiveRecord extends BaseActiveRecord
* for the `_id` field so that it is part of the `_source` fields and thus becomes part of the attributes.
* for the `_id` field so that it is part of the `_source` fields and thus becomes part of the attributes.
*
*
* @return string[] list of attribute names.
* @return string[] list of attribute names.
* @throws \yii\base\InvalidConfigException if not overridden in a child class.
*/
*/
public
function
attributes
()
public
function
attributes
()
{
{
...
@@ -277,6 +278,19 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -277,6 +278,19 @@ class ActiveRecord extends BaseActiveRecord
}
}
/**
/**
* A list of attributes that should be treated as array valued when retrieved through [[ActiveQuery::fields]].
*
* If not listed by this method, attributes retrieved through [[ActiveQuery::fields]] will converted to a scalar value
* when the result array contains only one value.
*
* @return string[] list of attribute names. Must be a subset of [[attributes()]].
*/
public
function
arrayAttributes
()
{
return
[];
}
/**
* @return string the name of the index this record is stored in.
* @return string the name of the index this record is stored in.
*/
*/
public
static
function
index
()
public
static
function
index
()
...
@@ -294,6 +308,10 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -294,6 +308,10 @@ class ActiveRecord extends BaseActiveRecord
/**
/**
* @inheritdoc
* @inheritdoc
*
* @param ActiveRecord $record the record to be populated. In most cases this will be an instance
* created by [[instantiate()]] beforehand.
* @param array $row attribute values (name => value)
*/
*/
public
static
function
populateRecord
(
$record
,
$row
)
public
static
function
populateRecord
(
$record
,
$row
)
{
{
...
@@ -302,9 +320,10 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -302,9 +320,10 @@ class ActiveRecord extends BaseActiveRecord
$attributes
=
$row
[
'_source'
];
$attributes
=
$row
[
'_source'
];
}
}
if
(
isset
(
$row
[
'fields'
]))
{
if
(
isset
(
$row
[
'fields'
]))
{
// reset fields in case it is scalar value TODO use field metadata for this
// reset fields in case it is scalar value
$arrayAttributes
=
$record
->
arrayAttributes
();
foreach
(
$row
[
'fields'
]
as
$key
=>
$value
)
{
foreach
(
$row
[
'fields'
]
as
$key
=>
$value
)
{
if
(
count
(
$value
)
==
1
)
{
if
(
!
isset
(
$arrayAttributes
[
$key
])
&&
count
(
$value
)
==
1
)
{
$row
[
'fields'
][
$key
]
=
reset
(
$value
);
$row
[
'fields'
][
$key
]
=
reset
(
$value
);
}
}
}
}
...
@@ -439,6 +458,7 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -439,6 +458,7 @@ class ActiveRecord extends BaseActiveRecord
* @param array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* @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.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @return integer the number of rows updated
* @return integer the number of rows updated
* @throws Exception on error.
*/
*/
public
static
function
updateAll
(
$attributes
,
$condition
=
[])
public
static
function
updateAll
(
$attributes
,
$condition
=
[])
{
{
...
@@ -498,6 +518,7 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -498,6 +518,7 @@ class ActiveRecord extends BaseActiveRecord
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
* Please refer to [[Query::where()]] on how to specify this parameter.
* @return integer the number of rows updated
* @return integer the number of rows updated
* @throws Exception on error.
*/
*/
public
static
function
updateAllCounters
(
$counters
,
$condition
=
[])
public
static
function
updateAllCounters
(
$counters
,
$condition
=
[])
{
{
...
@@ -562,6 +583,7 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -562,6 +583,7 @@ class ActiveRecord extends BaseActiveRecord
* @param array $condition the conditions that will be put in the WHERE part of the DELETE SQL.
* @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.
* Please refer to [[ActiveQuery::where()]] on how to specify this parameter.
* @return integer the number of rows deleted
* @return integer the number of rows deleted
* @throws Exception on error.
*/
*/
public
static
function
deleteAll
(
$condition
=
[])
public
static
function
deleteAll
(
$condition
=
[])
{
{
...
...
extensions/elasticsearch/CHANGELOG.md
View file @
c303ffd6
...
@@ -4,7 +4,7 @@ Yii Framework 2 elasticsearch extension Change Log
...
@@ -4,7 +4,7 @@ Yii Framework 2 elasticsearch extension Change Log
2.
0.0 under development
2.
0.0 under development
-----------------------
-----------------------
-
no changes in this release.
-
Enh #3381: Added ActiveRecord::arrayAttributes() to define attributes that should be treated as array when retrieved via
`fields`
(cebe)
2.0.0-rc September 27, 2014
2.0.0-rc September 27, 2014
...
...
extensions/elasticsearch/Query.php
View file @
c303ffd6
...
@@ -320,7 +320,13 @@ class Query extends Component implements QueryInterface
...
@@ -320,7 +320,13 @@ class Query extends Component implements QueryInterface
}
}
$column
=
[];
$column
=
[];
foreach
(
$result
[
'hits'
][
'hits'
]
as
$row
)
{
foreach
(
$result
[
'hits'
][
'hits'
]
as
$row
)
{
$column
[]
=
isset
(
$row
[
'_source'
][
$field
])
?
$row
[
'_source'
][
$field
]
:
null
;
if
(
isset
(
$row
[
'fields'
][
$field
]))
{
$column
[]
=
$row
[
'fields'
][
$field
];
}
elseif
(
isset
(
$row
[
'_source'
][
$field
]))
{
$column
[]
=
$row
[
'_source'
][
$field
];
}
else
{
$column
[]
=
null
;
}
}
}
return
$column
;
return
$column
;
}
}
...
...
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