当前位置: 动力学知识库 > 问答 > 编程问答 >

php - save for two table - doctrine and symfony

问题描述:

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 extends 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

分享给朋友:
您可能感兴趣的文章:
随机阅读: