开发者

Symfony 1.4 embedded form

开发者 https://www.devze.com 2023-02-10 15:16 出处:网络
I have a parent form PageForm and an embedded form ContentForm. Page has a one to many relationship with Content. When the PageForm is being edited I embed only 1 ContentForm - this is working fine. H

I have a parent form PageForm and an embedded form ContentForm. Page has a one to many relationship with Content. When the PageForm is being edited I embed only 1 ContentForm - this is working fine. Here is the configure method of PageForm

$content = new content();
$content->Page = $this->getObject();
if (开发者_开发技巧sfContext::getInstance()->getActionName() == 'new')
{
    $form = new contentForm($content);
} else
{
    $form = new contentForm($this->getObject()->getPublishedVersion());
}
$this->embedForm('content', $form);

method getPublishedVersion() returns a single content object.

Editing and Saving is working fine - saving the Parent form also performs an update for the embedded object into the database ...

My Problem is that I dont want to UPDATE the existing record I want to create a new record and increment the version_number column. I have tried overriding the saveEmbeddedForms() method of the PageForm class but im finding it very difficult - does anyone have any examples or ideas on how I could do this ?

I have tried this ->

if ($forms === null)
{
    $forms = $this->embeddedForms;
}
$newForm = new contentForm($forms['content']);
$newForm->save();
return parent::saveEmbeddedForms($con, $forms);

but its complaining that contentForm() expects and object ... how do i create a new object from the array ?


This is how I solved it ...

public function saveEmbeddedForms($con = null, $forms = null)
{
    if ($forms === null)
    {
        $forms = $this->embeddedForms;
    }
    // create new object as copy
    $newcontent = $forms['content']->getObject()->copy(false);
    // unset the id
    unset($newcontent->id);
    // incerement version
    $version = $newcontent->getVersion();
    $newcontent->setVersion($version +1);
    // save new record
    $newcontent->save();
    return; // dont save the current record
}
0

精彩评论

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