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
1a07485e
Commit
1a07485e
authored
Jun 17, 2014
by
Klimov Paul
Committed by
Carsten Brandt
Jul 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for relation by array attributes
fixes #1249
parent
6a6caf71
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
5 deletions
+33
-5
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
ActiveRelationTrait.php
framework/db/ActiveRelationTrait.php
+13
-0
ExternalActiveRelationTest.php
tests/unit/extensions/sphinx/ExternalActiveRelationTest.php
+19
-5
No files found.
framework/CHANGELOG.md
View file @
1a07485e
...
@@ -75,6 +75,7 @@ Yii Framework 2 Change Log
...
@@ -75,6 +75,7 @@ Yii Framework 2 Change Log
-
Enh #87: Helper
`yii\helpers\Security`
converted into application component, cryptographic strength improved (klimov-paul)
-
Enh #87: Helper
`yii\helpers\Security`
converted into application component, cryptographic strength improved (klimov-paul)
-
Enh #422: Added Support for BIT(M) data type default values in Schema (cebe)
-
Enh #422: Added Support for BIT(M) data type default values in Schema (cebe)
-
Enh #1160: Added $strict parameter to Inflector::camel2id() to handle consecutive uppercase chars (schmunk)
-
Enh #1160: Added $strict parameter to Inflector::camel2id() to handle consecutive uppercase chars (schmunk)
-
Enh #1249: Added support for Active Record relation via array attributes (klimov-paul)
-
Enh #1452: Added
`Module::getInstance()`
to allow accessing the module instance from anywhere within the module (qiangxue)
-
Enh #1452: Added
`Module::getInstance()`
to allow accessing the module instance from anywhere within the module (qiangxue)
-
Enh #2264:
`CookieCollection::has()`
will return false for expired or removed cookies (qiangxue)
-
Enh #2264:
`CookieCollection::has()`
will return false for expired or removed cookies (qiangxue)
-
Enh #2435:
`yii\db\IntegrityException`
is now thrown on database integrity errors instead of general
`yii\db\Exception`
(samdark)
-
Enh #2435:
`yii\db\IntegrityException`
is now thrown on database integrity errors instead of general
`yii\db\Exception`
(samdark)
...
...
framework/db/ActiveRelationTrait.php
View file @
1a07485e
...
@@ -240,8 +240,17 @@ trait ActiveRelationTrait
...
@@ -240,8 +240,17 @@ trait ActiveRelationTrait
$link
=
array_values
(
isset
(
$viaQuery
)
?
$viaQuery
->
link
:
$this
->
link
);
$link
=
array_values
(
isset
(
$viaQuery
)
?
$viaQuery
->
link
:
$this
->
link
);
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
if
(
$this
->
multiple
&&
count
(
$link
)
<
2
&&
is_array
(
$keys
=
$primaryModel
->
{
reset
(
$link
)}))
{
$value
=
[];
foreach
(
$keys
as
$key
)
{
if
(
isset
(
$buckets
[
$key
]))
{
$value
=
array_merge
(
$value
,
$buckets
[
$key
]);
}
}
}
else
{
$key
=
$this
->
getModelKey
(
$primaryModel
,
$link
);
$key
=
$this
->
getModelKey
(
$primaryModel
,
$link
);
$value
=
isset
(
$buckets
[
$key
])
?
$buckets
[
$key
]
:
(
$this
->
multiple
?
[]
:
null
);
$value
=
isset
(
$buckets
[
$key
])
?
$buckets
[
$key
]
:
(
$this
->
multiple
?
[]
:
null
);
}
if
(
$primaryModel
instanceof
ActiveRecordInterface
)
{
if
(
$primaryModel
instanceof
ActiveRecordInterface
)
{
$primaryModel
->
populateRelation
(
$name
,
$value
);
$primaryModel
->
populateRelation
(
$name
,
$value
);
}
else
{
}
else
{
...
@@ -414,9 +423,13 @@ trait ActiveRelationTrait
...
@@ -414,9 +423,13 @@ trait ActiveRelationTrait
$attribute
=
reset
(
$this
->
link
);
$attribute
=
reset
(
$this
->
link
);
foreach
(
$models
as
$model
)
{
foreach
(
$models
as
$model
)
{
if
((
$value
=
$model
[
$attribute
])
!==
null
)
{
if
((
$value
=
$model
[
$attribute
])
!==
null
)
{
if
(
is_array
(
$value
))
{
$values
=
array_merge
(
$values
,
$value
);
}
else
{
$values
[]
=
$value
;
$values
[]
=
$value
;
}
}
}
}
}
}
else
{
}
else
{
// composite keys
// composite keys
foreach
(
$models
as
$model
)
{
foreach
(
$models
as
$model
)
{
...
...
tests/unit/extensions/sphinx/ExternalActiveRelationTest.php
View file @
1a07485e
...
@@ -6,6 +6,7 @@ use yiiunit\data\ar\sphinx\ActiveRecord;
...
@@ -6,6 +6,7 @@ use yiiunit\data\ar\sphinx\ActiveRecord;
use
yiiunit\data\ar\ActiveRecord
as
ActiveRecordDb
;
use
yiiunit\data\ar\ActiveRecord
as
ActiveRecordDb
;
use
yiiunit\data\ar\sphinx\ArticleIndex
;
use
yiiunit\data\ar\sphinx\ArticleIndex
;
use
yiiunit\data\ar\sphinx\ArticleDb
;
use
yiiunit\data\ar\sphinx\ArticleDb
;
use
yiiunit\data\ar\sphinx\TagDb
;
/**
/**
* @group sphinx
* @group sphinx
...
@@ -34,11 +35,14 @@ class ExternalActiveRelationTest extends SphinxTestCase
...
@@ -34,11 +35,14 @@ class ExternalActiveRelationTest extends SphinxTestCase
$this
->
assertEquals
(
1
,
count
(
$article
->
relatedRecords
));
$this
->
assertEquals
(
1
,
count
(
$article
->
relatedRecords
));
// has many :
// has many :
/*
$this->assertFalse($article->isRelationPopulated('tags'));
$this
->
assertFalse
(
$article
->
isRelationPopulated
(
'tags'
));
$tags
=
$article
->
tags
;
$tags
=
$article
->
tags
;
$this
->
assertTrue
(
$article
->
isRelationPopulated
(
'tags'
));
$this
->
assertTrue
(
$article
->
isRelationPopulated
(
'tags'
));
$this->assertEquals(3, count($tags));
$this
->
assertEquals
(
count
(
$article
->
tag
),
count
(
$tags
));
$this->assertTrue($tags[0] instanceof TagDb);*/
$this
->
assertTrue
(
$tags
[
0
]
instanceof
TagDb
);
foreach
(
$tags
as
$tag
)
{
$this
->
assertTrue
(
in_array
(
$tag
->
id
,
$article
->
tag
));
}
}
}
public
function
testFindEager
()
public
function
testFindEager
()
...
@@ -52,10 +56,20 @@ class ExternalActiveRelationTest extends SphinxTestCase
...
@@ -52,10 +56,20 @@ class ExternalActiveRelationTest extends SphinxTestCase
$this
->
assertTrue
(
$articles
[
1
]
->
source
instanceof
ArticleDb
);
$this
->
assertTrue
(
$articles
[
1
]
->
source
instanceof
ArticleDb
);
// has many :
// has many :
/*
$articles = ArticleIndex::find()->with('tags')->all();
$articles
=
ArticleIndex
::
find
()
->
with
(
'tags'
)
->
all
();
$this
->
assertEquals
(
2
,
count
(
$articles
));
$this
->
assertEquals
(
2
,
count
(
$articles
));
$this
->
assertTrue
(
$articles
[
0
]
->
isRelationPopulated
(
'tags'
));
$this
->
assertTrue
(
$articles
[
0
]
->
isRelationPopulated
(
'tags'
));
$this->assertTrue($articles[1]->isRelationPopulated('tags'));*/
$this
->
assertTrue
(
$articles
[
1
]
->
isRelationPopulated
(
'tags'
));
foreach
(
$articles
as
$article
)
{
$this
->
assertTrue
(
$article
->
isRelationPopulated
(
'tags'
));
$tags
=
$article
->
tags
;
$this
->
assertEquals
(
count
(
$article
->
tag
),
count
(
$tags
));
//var_dump($tags);
$this
->
assertTrue
(
$tags
[
0
]
instanceof
TagDb
);
foreach
(
$tags
as
$tag
)
{
$this
->
assertTrue
(
in_array
(
$tag
->
id
,
$article
->
tag
));
}
}
}
}
/**
/**
...
...
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