Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
+ through option support
Browse files Browse the repository at this point in the history
* fixed complex foreign key config
* some doc refreshing
  • Loading branch information
velosipedist committed Oct 28, 2013
1 parent a6eec7e commit e7fc332
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions DeletableBehavior.php
Expand Up @@ -67,6 +67,7 @@ public function afterBatchDeleteHandler(CModelEvent $event)
/**
* Before Batch Delete Handler
* @param CModelEvent $event
* @return bool
*/
public function beforeBatchDeleteHandler(CModelEvent $event)
{
Expand Down Expand Up @@ -128,15 +129,36 @@ public function getRelativesIds(array $ids, $modelName, $attribute)
* Delete relatives of models
*
* @param array $ids primary keys of models
* @throws RestrictException
*/
public function batchDeleteRelatives($ids)
{
foreach ($this->relations as $relation => $type) {
$rels = $this->owner->relations();
$modelName = $rels[$relation][1];
$attribute = $rels[$relation][2];

$relativesIds = $this->getRelativesIds($ids, $modelName, $attribute);
$relationConfig = $rels[$relation];

$modelName = $relationConfig[1];

if(isset($relationConfig['through'])){
$throughLinkRelation = $rels[$relationConfig['through']];
$copy = array_keys($throughLinkRelation[2]);
$linkKey = array_shift($copy);
$copy = array_keys($relationConfig[2]);
$relatedKey = array_shift($copy);
$linkIds = $this->getRelativesIds($ids, $throughLinkRelation[1], $linkKey);
$relativesIds = array();
foreach ($linkIds as $id) {
$relativesIds[] = is_array($id) ? $id[$relatedKey] : $id;
}

}else{
$attribute = $relationConfig[2];
if(is_array($attribute)){
$attribute = array_shift($attribute);
}
$relativesIds = $this->getRelativesIds($ids, $modelName, $attribute);
}

if (!empty($relativesIds) && $type == self::RESTRICT) {
throw new RestrictException("Can not delete because of restrict \"$modelName\" data");
Expand All @@ -150,10 +172,11 @@ public function batchDeleteRelatives($ids)
/**
* Delete models & relatives
*
* @param array $ids models primary keys for deleting
* @param bool $deleteRelatives delete or not relatives
* @param bool $first need this param for control transaction
* @param array $ids models primary keys for deleting
* @param bool $deleteRelatives delete or not relatives
* @param bool $first need this param for control transaction
*
* @throws Exception
* @return int numbers of rows that deleted.
*/
public function batchDelete(array $ids, $deleteRelatives = true, $first = true)
Expand Down Expand Up @@ -295,4 +318,4 @@ private function _convertIdsForDeleteMethod(array $ids)
* If type is RESTRICT and relatives exists, then throw this exception
*
*/
class RestrictException extends CException {}
class RestrictException extends CException {}

0 comments on commit e7fc332

Please sign in to comment.