Home > PHP > ExtendAssociations Behavior for CakePHP 2.0.x

ExtendAssociations Behavior for CakePHP 2.0.x

  • 2012-01-10 (火)
  • PHP
  • このエントリーを含むはてなブックマーク

CakePHP における HABTM association table を更新する際に便利な Behavior がこの ExtendAssociationsBehavior:

HABTM Add & Delete Behavior - The Bakery, Everything CakePHP

ただこの Behavior, CakePHP 1.2 時代に作られたもので現行版の CakePHP 2.0.x だと動作しなかったので、パッチコードを書いてみた。以下:

Updated Version of ExtendAssociationsBehavior for CakePHP 2.0.x

Download

Patch

$ diff -u old new
--- Vendor/Model/Behavior/ExtendAssociationsBehavior.php	2012-01-10 23:31:14.000000000 +0900
+++ Vendor/Model/Behavior/ExtendAssociations2Behavior.php	2012-01-10 23:33:57.000000000 +0900
@@ -1,5 +1,6 @@
 <?php 
-class ExtendAssociationsBehavior extends ModelBehavior { 
+App::uses('ModelBehavior', 'Model');
+class ExtendAssociations2Behavior extends ModelBehavior { 
     /** 
      * Model-specific settings 
      * @var array 
@@ -44,7 +45,7 @@
             // important to use array_unique() since merging will add  
             // non-unique values to the array. 
             $data[$assoc][$assoc] = array_unique(am($data[$assoc][$assoc], $assoc_ids)); 
-            return $model->save($data); 
+            return $model->save($data, array('fieldList'=>array($assoc))); 
         } 
          
         // association doesn't exist, return false 
@@ -82,7 +83,7 @@
                 // which is the ones we want to re-save. 
                 $data[$assoc][$assoc] = array_diff($data[$assoc][$assoc], $assoc_ids); 
             } 
-            return $model->save($data); 
+            return $model->save($data, array('fieldList'=>array($assoc))); 
         } 
          
         // association doesn't exist, return false         
@@ -122,7 +123,7 @@
          
         // unbind all models except the habtm association 
         $this->unbindAll($model, array('hasAndBelongsToMany' => array($assoc))); 
-        $data = $model->find(array($model->name.'.'.$model->primaryKey => $id)); 
+        $data = $model->findById($id);
              
         $model->recursive = $tmp_recursive; 
         $model->cacheQueries = $tmp_cacheQueries; 
@@ -149,8 +150,8 @@
      */ 
     function unbindAll(&$model, $exceptions = array()) { 
         $unbind = array(); 
-        foreach($model->__associations as $type) { 
-            foreach($model->{$type} as $assoc=>$assocData) { 
+        foreach($model->_associations as $type) { 
+            foreach($model->$type as $assoc=>$assocData) { 
                 // if the assoc is NOT in the exceptions list then 
                 // add it to the list of models to be unbound. 
                 if(@!in_array($assoc, $exceptions[$type])) { 
@@ -164,3 +165,4 @@
         } 
     } 
 } 
+

Usage

使い方は旧版から変更無し:

## add 
$model->habtmAdd($assoc_name, $from_id, array($to_id1, $to_id2, ...));
e.g. $this->Post->habtmAdd('PostsTag', 1, array(2, 3));

## delete
$model->habtmDelete($assoc_name, $from_id, array($to_id1, $to_id2, ...));
e.g. $this->Post->habtmAdd('PostsTag', 1, array(3));

## delete all
$model->habtmDeleteAll($assoc_name, $from_id);

Comments:0

Comment Form

コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。

Remember personal info

Home > PHP > ExtendAssociations Behavior for CakePHP 2.0.x

Search
Feeds

Page Top