Saving Data In Related Table


(Assd18) #1

Hi. I have 2 tables (page and tags), and one related table page_tags. I want save data on table page_tags. But data doesn’t save.

Thank’s)




            $genreIds = array(1, 2); 

            $page = Page::model()->findByPk(16); 

            $tags = Tags::model()->findAllByPk($genreIds); 

            $page->tags = $tags;

            $page->save();




model Page

public function relations()

    {

return array(

                'tags' => array(self::MANY_MANY, 'Tags', 'page_tags(page_id, tag_id)'),

            

        );

}




model Tags

public function relations()

    {

return array(

                'tags' => array(self::MANY_MANY, 'Page', 'page_tags(tag_id, page_id)'),

            

        );

}




(Ziggi) #2

This is the answer:

http://www.yiiframework.com/extension/save-relations-ar-behavior/


(Assd18) #3

Thank’s, but still doesn’t work. I past in Page model function behaviors and write in controller:




$page = Page::model()->findByPk(16);

$data = array(array('tag_id'=>2,'page_id'=>2),array('tag_id'=>5,'page_id'=>3));

$page->setRelationRecords('tags',$data);

$page->save();



Please help.


(Assd18) #4

Who have same problem?