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
b85c87bc
Commit
b85c87bc
authored
May 25, 2013
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AR relations copy&paste
parent
b030326d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
25 deletions
+39
-25
ActiveRecord.php
framework/yii/db/redis/ActiveRecord.php
+39
-25
No files found.
framework/yii/db/redis/ActiveRecord.php
View file @
b85c87bc
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
namespace
yii\db\redis
;
namespace
yii\db\redis
;
use
yii\base\InvalidCallException
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidParamException
;
use
yii\base\InvalidParamException
;
use
yii\base\NotSupportedException
;
use
yii\base\NotSupportedException
;
...
@@ -420,34 +421,12 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
...
@@ -420,34 +421,12 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
*/
*/
public
function
link
(
$name
,
$model
,
$extraColumns
=
array
())
public
function
link
(
$name
,
$model
,
$extraColumns
=
array
())
{
{
// TODO
$relation
=
$this
->
getRelation
(
$name
);
$relation
=
$this
->
getRelation
(
$name
);
if
(
$relation
->
via
!==
null
)
{
if
(
$relation
->
via
!==
null
)
{
if
(
is_array
(
$relation
->
via
))
{
// TODO
/** @var $viaRelation ActiveRelation */
list
(
$viaName
,
$viaRelation
)
=
$relation
->
via
;
/** @var $viaClass ActiveRecord */
$viaClass
=
$viaRelation
->
modelClass
;
$viaTable
=
$viaClass
::
tableName
();
// unset $viaName so that it can be reloaded to reflect the change
unset
(
$this
->
_related
[
strtolower
(
$viaName
)]);
}
else
{
$viaRelation
=
$relation
->
via
;
$viaTable
=
reset
(
$relation
->
via
->
from
);
}
$columns
=
array
();
foreach
(
$viaRelation
->
link
as
$a
=>
$b
)
{
$columns
[
$a
]
=
$this
->
$b
;
}
foreach
(
$relation
->
link
as
$a
=>
$b
)
{
$columns
[
$b
]
=
$model
->
$a
;
}
foreach
(
$extraColumns
as
$k
=>
$v
)
{
$columns
[
$k
]
=
$v
;
}
static
::
getDb
()
->
createCommand
()
->
insert
(
$viaTable
,
$columns
)
->
execute
();
}
else
{
}
else
{
$p1
=
$model
->
isPrimaryKey
(
array_keys
(
$relation
->
link
));
$p1
=
$model
->
isPrimaryKey
(
array_keys
(
$relation
->
link
));
$p2
=
$this
->
isPrimaryKey
(
array_values
(
$relation
->
link
));
$p2
=
$this
->
isPrimaryKey
(
array_values
(
$relation
->
link
));
...
@@ -482,6 +461,24 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
...
@@ -482,6 +461,24 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
}
}
/**
/**
* @param array $link
* @param ActiveRecord $foreignModel
* @param ActiveRecord $primaryModel
* @throws InvalidCallException
*/
private
function
bindModels
(
$link
,
$foreignModel
,
$primaryModel
)
{
foreach
(
$link
as
$fk
=>
$pk
)
{
$value
=
$primaryModel
->
$pk
;
if
(
$value
===
null
)
{
throw
new
InvalidCallException
(
'Unable to link models: the primary key of '
.
get_class
(
$primaryModel
)
.
' is null.'
);
}
$foreignModel
->
$fk
=
$value
;
}
$foreignModel
->
save
(
false
);
}
/**
* Destroys the relationship between two models.
* Destroys the relationship between two models.
*
*
* 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.
...
@@ -558,6 +555,23 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
...
@@ -558,6 +555,23 @@ abstract class ActiveRecord extends \yii\db\ActiveRecord
}
}
}
}
/**
* TODO duplicate code, refactor
* @param array $keys
* @return boolean
*/
private
function
isPrimaryKey
(
$keys
)
{
$pks
=
$this
->
primaryKey
();
foreach
(
$keys
as
$key
)
{
if
(
!
in_array
(
$key
,
$pks
,
true
))
{
return
false
;
}
}
return
true
;
}
// TODO implement link and unlink
// TODO implement link and unlink
}
}
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