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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
e1065ef1
Commit
e1065ef1
authored
Nov 17, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"snippetByModel" option added to Sphinx Active Query.
parent
386b58b2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
8 deletions
+72
-8
ActiveQuery.php
extensions/sphinx/ActiveQuery.php
+41
-3
Query.php
extensions/sphinx/Query.php
+11
-5
ArticleIndex.php
tests/unit/data/sphinx/ar/ArticleIndex.php
+6
-0
ActiveRelationTest.php
tests/unit/extensions/sphinx/ActiveRelationTest.php
+14
-0
No files found.
extensions/sphinx/ActiveQuery.php
View file @
e1065ef1
...
...
@@ -27,6 +27,19 @@ class ActiveQuery extends Query implements ActiveQueryInterface
public
$sql
;
/**
* Sets the [[snippetCallback]] to [[fetchSnippetSourceFromModels]], which allows to
* fetch the snippet source strings from the Active Record models, using method
* [[ActiveRecord::getSnippetSource()]].
* Warning: this option should NOT be used with [[asArray]] at the same time!
* @return static the query object itself
*/
public
function
snippetByModel
()
{
$this
->
snippetCallback
(
array
(
$this
,
'fetchSnippetSourceFromModels'
));
return
$this
;
}
/**
* Executes query and returns all results as an array.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
...
...
@@ -41,6 +54,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
if
(
!
empty
(
$this
->
with
))
{
$this
->
findWith
(
$this
->
with
,
$models
);
}
$models
=
$this
->
fillUpSnippets
(
$models
);
return
$models
;
}
else
{
return
[];
...
...
@@ -72,6 +86,7 @@ class ActiveQuery extends Query implements ActiveQueryInterface
$this
->
findWith
(
$this
->
with
,
$models
);
$model
=
$models
[
0
];
}
list
(
$model
)
=
$this
->
fillUpSnippets
([
$model
]);
return
$model
;
}
else
{
return
null
;
...
...
@@ -88,9 +103,8 @@ class ActiveQuery extends Query implements ActiveQueryInterface
{
/** @var $modelClass ActiveRecord */
$modelClass
=
$this
->
modelClass
;
if
(
$db
===
null
)
{
$db
=
$modelClass
::
getDb
();
}
$this
->
setConnection
(
$db
);
$db
=
$this
->
getConnection
();
$params
=
$this
->
params
;
if
(
$this
->
sql
===
null
)
{
...
...
@@ -105,4 +119,27 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
return
$db
->
createCommand
(
$this
->
sql
,
$params
);
}
/**
* @inheritdoc
*/
protected
function
defaultConnection
()
{
$modelClass
=
$this
->
modelClass
;
return
$modelClass
::
getDb
();
}
/**
* Fetches the source for the snippets using [[ActiveRecord::getSnippetSource()]] method.
* @param ActiveRecord[] $models raw query result rows.
* @return array snippet source strings
*/
protected
function
fetchSnippetSourceFromModels
(
$models
)
{
$result
=
[];
foreach
(
$models
as
$model
)
{
$result
[]
=
$model
->
getSnippetSource
();
}
return
$result
;
}
}
\ No newline at end of file
extensions/sphinx/Query.php
View file @
e1065ef1
...
...
@@ -9,6 +9,7 @@ namespace yii\sphinx;
use
Yii
;
use
yii\base\Component
;
use
yii\base\InvalidCallException
;
use
yii\db\Expression
;
/**
...
...
@@ -235,11 +236,11 @@ class Query extends Component
*/
public
function
one
(
$db
=
null
)
{
$r
esult
=
$this
->
createCommand
(
$db
)
->
queryOne
();
if
(
$r
esult
)
{
list
(
$r
esult
)
=
$this
->
fillUpSnippets
([
$result
]);
$r
ow
=
$this
->
createCommand
(
$db
)
->
queryOne
();
if
(
$r
ow
!==
false
)
{
list
(
$r
ow
)
=
$this
->
fillUpSnippets
([
$row
]);
}
return
$r
esult
;
return
$r
ow
;
}
/**
...
...
@@ -797,8 +798,12 @@ class Query extends Component
protected
function
callSnippets
(
array
$source
)
{
$connection
=
$this
->
getConnection
();
$match
=
$this
->
match
;
if
(
$match
===
null
)
{
throw
new
InvalidCallException
(
'Unable to call snippets: "'
.
$this
->
className
()
.
'::match" should be specified.'
);
}
return
$connection
->
createCommand
()
->
callSnippets
(
$this
->
from
[
0
],
$source
,
$
this
->
match
,
$this
->
snippetOptions
)
->
callSnippets
(
$this
->
from
[
0
],
$source
,
$match
,
$this
->
snippetOptions
)
->
queryColumn
();
}
}
\ No newline at end of file
tests/unit/data/sphinx/ar/ArticleIndex.php
View file @
e1065ef1
...
...
@@ -28,4 +28,9 @@ class ArticleIndex extends ActiveRecord
];
return
new
ActiveRelation
(
$config
);
}
public
function
getSnippetSource
()
{
return
$this
->
source
->
content
;
}
}
\ No newline at end of file
tests/unit/extensions/sphinx/ActiveRelationTest.php
View file @
e1065ef1
...
...
@@ -39,4 +39,17 @@ class ActiveRelationTest extends SphinxTestCase
$this
->
assertTrue
(
$articles
[
0
]
->
source
instanceof
ArticleDb
);
$this
->
assertTrue
(
$articles
[
1
]
->
source
instanceof
ArticleDb
);
}
/**
* @depends testFindEager
*/
public
function
testFindWithSnippets
()
{
$articles
=
ArticleIndex
::
find
()
->
match
(
'about'
)
->
with
(
'source'
)
->
snippetByModel
()
->
all
();
$this
->
assertEquals
(
2
,
count
(
$articles
));
}
}
\ No newline at end of file
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