开发者

How to edit multiple records with associations?

I have 4 tables: - albums - images - tags - images_tags (id, image_id, tag_id)

I would like to edit multiple records. Everything works fine until I added tags table. Now my $this->data looks li开发者_开发百科ke this: (124 is image id)

Array(
     [124] => Array(
           [Image] => Array
           (
                 [id] => 124
                 ...
           )

           [Album] => Array
           (
                 [id] => 2
                 ...
           )

           [Tag] => Array
           (
                  [0] => Array
                  (
                       [id] => 2
                       ....
                  )
           )
      [125] => Array( ...
)

This is my view edit file:

<?php foreach($this->data as $key => $value) {

echo $this->Form->input('Image.'.$key.'.id');
echo $this->Form->input('Image.'.$key.'.title');
echo $this->Form->input('Image.'.$key.'.Tag'); // multi select for tags
...

And edit action in Images controller:

$result = $this->Image->find('all', array(
        'conditions' => array('Image.id' => $img_ids))
);

$this->data['Image'] = Set::combine($result, '{n}.Image.id', '{n}');

I don't know how can I bind $this->data array with multiple edit form. Previously I had only Image data:

 Array(
         [124] => Array(
                  [id] => 2,
                  ...
         ),
         [125] => Array(
                  ...
  );

But now I need also information about tags. I am using saveAll() function.


You will need to transform the [Tag] index. The proper format for saving hasAndBelongsToMany is [Tag][Tag] => array(id1,id2,id3,...). For example:

Array(
 [124] => Array(
       [Image] => Array
       (
             [id] => 124
             ...
       )

       [Album] => Array
       (
             [id] => 2
             ...
       )

       [Tag] => Array
       (
              [Tag] => Array
              (
                  [0] => 2,
                  [1] => 16,
                  ...
              )
       )
  [125] => Array( ...
)

Where 2 and 16 are ids of associated tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜