这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tmp/*
yii

ar.komodoproject
55 changes: 54 additions & 1 deletion EActiveRecordRelationBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class EActiveRecordRelationBehavior extends CActiveRecordBehavior
public $useTransaction=true;
/** @var CDbTransaction */
private $_transaction;

/**
* Holds relations to be used
*/
protected $_enabledRelations=null;

/**
* Declares events and the corresponding event handler methods.
Expand Down Expand Up @@ -118,7 +123,7 @@ public function afterSave($event)
/** @var CDbCommandBuilder $commandBuilder */
$commandBuilder=$this->owner->dbConnection->commandBuilder;

foreach($this->owner->relations() as $name => $relation)
foreach($this->getRelations() as $name => $relation)
{
switch($relation[0]) // relation type such as BELONGS_TO, HAS_ONE, HAS_MANY, MANY_MANY
{
Expand Down Expand Up @@ -356,6 +361,54 @@ protected function parseManyManyFk($name, $relation)

return array($joinTable, $fks);
}


/**
* Scope: Resets the enabled relations to default (Owner's)
* @return CActiveRecord $owner
*/
public function resetRelations()
{
$this->_enabledRelations=null;
return $this->owner;
}

/**
* Scope: Sets the enabled relations to be used
* @param String relation names
* @param String ...
*
* @return CActiveRecord $owner
*/
public function withRelations()
{
$this->_enabledRelations=func_get_args();
return $this->owner;
}

/**
* Scope: Exclude relations from owner's to be used
* @param String relation name
* @param String ...
*
* @return CActiveRecord $owner
*/
public function withoutRelations()
{
$this->_enabledRelations=array_diff(array_keys($this->owner->relations()),func_get_args());
return $this->owner;
}

/**
* Return the enabled relations
* @return Array filtered owner relations
*/
protected function getRelations()
{
if (is_null($this->_enabledRelations)) return $this->owner->relations();

return array_intersect_key($this->owner->relations(), array_flip($this->_enabledRelations));
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function behaviors()
## Let the magic begin...

We have two ActiveRecord classes (the ones from [Yii definitive guide](http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship)):


```php
<?php
class Post extends CActiveRecord
Expand Down Expand Up @@ -103,6 +105,21 @@ Somewhere in our application code we can do:
$user->save();
```

## You can also…

Enable/Disable/Reset the relations to save. To do this you can use:

`$model->withoutRelations('relation name 1', 'relation name 2',…)->save()`

…will save all `$model` relations except the ones passed to the scope.

`$model->withRelations('relation name 1', 'relation name 2',…)->save()`

…will save only the `$model` relations passed to the scope

`$model->resetRelations()->save()`

…will clear the former scopes, and save all the relations of `$model`

## Some things you should care about...

Expand Down