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
9a45a043
Commit
9a45a043
authored
Nov 19, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create relation methods added to Sphinx Active Record.
parent
8bec8c5f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
7 deletions
+109
-7
ActiveRecord.php
extensions/sphinx/ActiveRecord.php
+108
-0
ArticleIndex.php
tests/unit/data/sphinx/ar/ArticleIndex.php
+1
-7
No files found.
extensions/sphinx/ActiveRecord.php
View file @
9a45a043
...
...
@@ -18,6 +18,7 @@ use yii\db\Expression;
use
yii\db\StaleObjectException
;
use
yii\helpers\Inflector
;
use
yii\helpers\StringHelper
;
use
Yii
;
/**
* Class ActiveRecord
...
...
@@ -472,6 +473,113 @@ class ActiveRecord extends Model
}
/**
* Declares a `has-one` relation.
* The declaration is returned in terms of an [[ActiveRelationInterface]] instance
* through which the related record can be queried and retrieved back.
*
* A `has-one` relation means that there is at most one related record matching
* the criteria set by this relation, e.g., a particular index has one source.
*
* For example, to declare the `source` relation for `ArticleIndex` class, we can write
* the following code in the `ArticleIndex` class:
*
* ~~~
* public function getSource()
* {
* return $this->hasOne('db', ArticleContent::className(), ['article_id' => 'id']);
* }
* ~~~
*
* Note that in the above, the 'article_id' key in the `$link` parameter refers to an attribute name
* in the related class `ArticleContent`, while the 'id' value refers to an attribute name
* in the current AR class.
*
* @param string $type relation type or class name.
* - if value contains backslash ("\"), it is treated as full active relation class name,
* for example: "app\mydb\ActiveRelation"
* - if value does not contain backslash ("\"), the active relation class name will be composed
* by pattern: "yii\{type}\ActiveRelation", for example: type "db" refers "yii\db\ActiveRelation",
* type "sphinx" - "yii\sphinx\ActiveRelation"
* @param string $class the class name of the related record
* @param array $link the primary-foreign key constraint. The keys of the array refer to
* the attributes in the `$class` model, while the values of the array refer to the corresponding
* attributes in the index associated with this AR class.
* @return ActiveRelationInterface the relation object.
*/
public
function
hasOne
(
$type
,
$class
,
$link
)
{
return
$this
->
createActiveRelation
(
$type
,
[
'modelClass'
=>
$class
,
'primaryModel'
=>
$this
,
'link'
=>
$link
,
'multiple'
=>
false
,
]);
}
/**
* Declares a `has-many` relation.
* The declaration is returned in terms of an [[ActiveRelationInterface]] instance
* through which the related record can be queried and retrieved back.
*
* A `has-many` relation means that there are multiple related records matching
* the criteria set by this relation, e.g., an article has many tags.
*
* For example, to declare the `tags` relation for `ArticleIndex` class, we can write
* the following code in the `ArticleIndex` class:
*
* ~~~
* public function getOrders()
* {
* return $this->hasMany('db', Tag::className(), ['id' => 'tag_id']);
* }
* ~~~
*
* Note that in the above, the 'id' key in the `$link` parameter refers to
* an attribute name in the related class `Tag`, while the 'tag_id' value refers to
* a multi value attribute name in the current AR class.
*
* @param string $type relation type or class name.
* - if value contains backslash ("\"), it is treated as full active relation class name,
* for example: "app\mydb\ActiveRelation"
* - if value does not contain backslash ("\"), the active relation class name will be composed
* by pattern: "yii\{type}\ActiveRelation", for example: type "db" refers "yii\db\ActiveRelation",
* type "sphinx" - "yii\sphinx\ActiveRelation"
* @param string $class the class name of the related record
* @param array $link the primary-foreign key constraint. The keys of the array refer to
* the columns in the table associated with the `$class` model, while the values of the
* array refer to the corresponding columns in the table associated with this AR class.
* @return ActiveRelationInterface the relation object.
*/
public
function
hasMany
(
$type
,
$class
,
$link
)
{
return
$this
->
createActiveRelation
(
$type
,
[
'modelClass'
=>
$class
,
'primaryModel'
=>
$this
,
'link'
=>
$link
,
'multiple'
=>
true
,
]);
}
/**
* Creates an [[ActiveRelationInterface]] instance.
* This method is called by [[hasOne()]] and [[hasMany()]] to create a relation instance.
* You may override this method to return a customized relation.
* @param string $type relation type or class name.
* @param array $config the configuration passed to the ActiveRelation class.
* @return ActiveRelationInterface the newly created [[ActiveRelation]] instance.
*/
protected
function
createActiveRelation
(
$type
,
$config
=
[])
{
if
(
strpos
(
$type
,
'\\'
)
===
false
)
{
$class
=
"yii
\\
{
$type
}
\\
ActiveRelation"
;
}
else
{
$class
=
$type
;
}
$config
[
'class'
]
=
$class
;
return
Yii
::
createObject
(
$config
);
}
/**
* Populates the named relation with the related records.
* Note that this method does not check if the relation exists or not.
* @param string $name the relation name (case-sensitive)
...
...
tests/unit/data/sphinx/ar/ArticleIndex.php
View file @
9a45a043
...
...
@@ -20,13 +20,7 @@ class ArticleIndex extends ActiveRecord
public
function
getSource
()
{
$config
=
[
'modelClass'
=>
ArticleDb
::
className
(),
'primaryModel'
=>
$this
,
'link'
=>
[
'id'
=>
'id'
],
'multiple'
=>
false
,
];
return
new
ActiveRelation
(
$config
);
return
$this
->
hasOne
(
'db'
,
ArticleDb
::
className
(),
[
'id'
=>
'id'
]);
}
public
function
getSnippetSource
()
...
...
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