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
3701cbd5
Commit
3701cbd5
authored
Nov 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1125 from cebe/842-relation-data
Proposal for accessing populated relations
parents
65d7ffa7
0bc8cbf1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
13 deletions
+30
-13
ActiveRecord.php
framework/yii/db/ActiveRecord.php
+30
-13
No files found.
framework/yii/db/ActiveRecord.php
View file @
3701cbd5
...
@@ -379,13 +379,12 @@ class ActiveRecord extends Model
...
@@ -379,13 +379,12 @@ class ActiveRecord extends Model
}
elseif
(
$this
->
hasAttribute
(
$name
))
{
}
elseif
(
$this
->
hasAttribute
(
$name
))
{
return
null
;
return
null
;
}
else
{
}
else
{
$t
=
strtolower
(
$name
);
if
(
isset
(
$this
->
_related
[
$name
])
||
array_key_exists
(
$name
,
$this
->
_related
))
{
if
(
isset
(
$this
->
_related
[
$t
])
||
array_key_exists
(
$t
,
$this
->
_related
))
{
return
$this
->
_related
[
$name
];
return
$this
->
_related
[
$t
];
}
}
$value
=
parent
::
__get
(
$name
);
$value
=
parent
::
__get
(
$name
);
if
(
$value
instanceof
ActiveRelation
)
{
if
(
$value
instanceof
ActiveRelation
)
{
return
$this
->
_related
[
$
t
]
=
$value
->
multiple
?
$value
->
all
()
:
$value
->
one
();
return
$this
->
_related
[
$
name
]
=
$value
->
multiple
?
$value
->
all
()
:
$value
->
one
();
}
else
{
}
else
{
return
$value
;
return
$value
;
}
}
...
@@ -433,9 +432,8 @@ class ActiveRecord extends Model
...
@@ -433,9 +432,8 @@ class ActiveRecord extends Model
if
(
$this
->
hasAttribute
(
$name
))
{
if
(
$this
->
hasAttribute
(
$name
))
{
unset
(
$this
->
_attributes
[
$name
]);
unset
(
$this
->
_attributes
[
$name
]);
}
else
{
}
else
{
$t
=
strtolower
(
$name
);
if
(
isset
(
$this
->
_related
[
$name
]))
{
if
(
isset
(
$this
->
_related
[
$t
]))
{
unset
(
$this
->
_related
[
$name
]);
unset
(
$this
->
_related
[
$t
]);
}
else
{
}
else
{
parent
::
__unset
(
$name
);
parent
::
__unset
(
$name
);
}
}
...
@@ -523,12 +521,31 @@ class ActiveRecord extends Model
...
@@ -523,12 +521,31 @@ class ActiveRecord extends Model
/**
/**
* Populates the named relation with the related records.
* Populates the named relation with the related records.
* Note that this method does not check if the relation exists or not.
* Note that this method does not check if the relation exists or not.
* @param string $name the relation name (case-
in
sensitive)
* @param string $name the relation name (case-sensitive)
* @param ActiveRecord|array|null the related records to be populated into the relation.
* @param ActiveRecord|array|null the related records to be populated into the relation.
*/
*/
public
function
populateRelation
(
$name
,
$records
)
public
function
populateRelation
(
$name
,
$records
)
{
{
$this
->
_related
[
strtolower
(
$name
)]
=
$records
;
$this
->
_related
[
$name
]
=
$records
;
}
/**
* Check whether the named relation has been populated with records.
* @param string $name the relation name (case-sensitive)
* @return bool whether relation has been populated with records.
*/
public
function
isRelationPopulated
(
$name
)
{
return
array_key_exists
(
$name
,
$this
->
_related
);
}
/**
* Returns all populated relations.
* @return array an array of relation data indexed by relation names.
*/
public
function
getPopulatedRelations
()
{
return
$this
->
_related
;
}
}
/**
/**
...
@@ -1286,7 +1303,7 @@ class ActiveRecord extends Model
...
@@ -1286,7 +1303,7 @@ class ActiveRecord extends Model
*
*
* Note that this method requires that the primary key value is not null.
* Note that this method requires that the primary key value is not null.
*
*
* @param string $name the name of the relationship
* @param string $name the
case sensitive
name of the relationship
* @param ActiveRecord $model the model to be linked with the current one.
* @param ActiveRecord $model the model to be linked with the current one.
* @param array $extraColumns additional column values to be saved into the pivot table.
* @param array $extraColumns additional column values to be saved into the pivot table.
* This parameter is only meaningful for a relationship involving a pivot table
* This parameter is only meaningful for a relationship involving a pivot table
...
@@ -1308,7 +1325,7 @@ class ActiveRecord extends Model
...
@@ -1308,7 +1325,7 @@ class ActiveRecord extends Model
$viaClass
=
$viaRelation
->
modelClass
;
$viaClass
=
$viaRelation
->
modelClass
;
$viaTable
=
$viaClass
::
tableName
();
$viaTable
=
$viaClass
::
tableName
();
// unset $viaName so that it can be reloaded to reflect the change
// unset $viaName so that it can be reloaded to reflect the change
unset
(
$this
->
_related
[
strtolower
(
$viaName
)
]);
unset
(
$this
->
_related
[
$viaName
]);
}
else
{
}
else
{
$viaRelation
=
$relation
->
via
;
$viaRelation
=
$relation
->
via
;
$viaTable
=
reset
(
$relation
->
via
->
from
);
$viaTable
=
reset
(
$relation
->
via
->
from
);
...
@@ -1364,7 +1381,7 @@ class ActiveRecord extends Model
...
@@ -1364,7 +1381,7 @@ class ActiveRecord extends Model
* The model with the foreign key of the relationship will be deleted if `$delete` is true.
* The model with the foreign key of the relationship will be deleted if `$delete` is true.
* Otherwise, the foreign key will be set null and the model will be saved without validation.
* Otherwise, the foreign key will be set null and the model will be saved without validation.
*
*
* @param string $name the name of the relationship.
* @param string $name the
case sensitive
name of the relationship.
* @param ActiveRecord $model the model to be unlinked from the current one.
* @param ActiveRecord $model the model to be unlinked from the current one.
* @param boolean $delete whether to delete the model that contains the foreign key.
* @param boolean $delete whether to delete the model that contains the foreign key.
* If false, the model's foreign key will be set null and saved.
* If false, the model's foreign key will be set null and saved.
...
@@ -1382,7 +1399,7 @@ class ActiveRecord extends Model
...
@@ -1382,7 +1399,7 @@ class ActiveRecord extends Model
/** @var $viaClass ActiveRecord */
/** @var $viaClass ActiveRecord */
$viaClass
=
$viaRelation
->
modelClass
;
$viaClass
=
$viaRelation
->
modelClass
;
$viaTable
=
$viaClass
::
tableName
();
$viaTable
=
$viaClass
::
tableName
();
unset
(
$this
->
_related
[
strtolower
(
$viaName
)
]);
unset
(
$this
->
_related
[
$viaName
]);
}
else
{
}
else
{
$viaRelation
=
$relation
->
via
;
$viaRelation
=
$relation
->
via
;
$viaTable
=
reset
(
$relation
->
via
->
from
);
$viaTable
=
reset
(
$relation
->
via
->
from
);
...
...
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