开发者

save for two table - doctrine and symfony

开发者 https://www.devze.com 2023-03-31 18:54 出处:网络
i have two table: News: id title body and NewsCopy: id title body if i add new News i would like also add this same all data for table NewsCopy?

i have two table:

News:
id
title
body

and

NewsCopy:
id
title
body

if i add new News i would like also add this same all data for table NewsCopy?

class News e开发者_如何学编程xtends BaseNews
{
    public function save(Doctrine_Connection $conn = null)
    {

      return parent::save($conn);
    }
}

how can i make this simply?


Well, one possible way is to hook up into the Doctrine saving mechanism:

class News{
    //..other declarations//

    //executed after Save
    public function postSave(){
        $newsCopy = new NewsCopy(); 
        //set the parameters manually
        $newsCopy->id = $this->id;
        $newsCopy->title = $this->title;
        $newsCopy->body = $this->body;

        //OR, even better, create a "cast constructor" the same idea
        //$newsCopy = new NewsCopy($this);

        $newsCopy->save();
    }
}

See "Event Listeners" chapter for more detailed explanation


You can utilize the toArray() method of the existing and populated "News" record object and populate a separate CopyNews object. With the now newly configured object you can do the save with.

I assume doctrine 1.2 - and I do not have a testing environment - so no code :).

You could probably also play with the clone() method and set a new table name ...

All untested - sorry.


The best you can do is to use triggers

0

精彩评论

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

关注公众号