【问题标题】:symfony 1.4 updating some object while (or after) saving another one related to itsymfony 1.4 更新某个对象,同时(或之后)保存另一个与之相关的对象
【发布时间】:2011-03-07 23:31:14
【问题描述】:

我在 Symfony 中有一个学说形式,它将某些对象保存在数据库的表中。 通过这样做,我还希望更新另一个与第一个对象有关系的对象,因此对象在保存第一个对象时会更新关系(一对多)。

我真的不知道这是否可能......

我会发布我的代码,作为参考:

架构:

Entry:
  columns:
    pic: { type: integer, notnull: false, default: null }
  relations:
    Pic:
      local:   pic
      foreign: id
      onDelete: SET NULL
    Pics:
      type:    many
      class:   Pic
      local:   id
      foreign: entry_id


Pic:
  columns:
    id:         { type: integer, notnull: true, primary: true, autoincrement: true }
    entry_id:   { type: integer, notnull: true, primary: true }
    pic:        { type: string(255), notnull: true }
  relations:
    Entry:
      local:    entry_id
      foreign:  id
      onDelete: CASCADE

我在 Pic 和 Entry 之间还有另一个多对多关系,因为一个 Entry 可能有很多 Pics,但我创建的第一张 Pic 也必须定义为我在 Entry 中建立的“默认”Pic。图片字段...

(我之前正确地创建了一个条目,所以现在我必须获得一个新的图片,这将是该条目的“默认”图片)...在图片表的创建操作中,我希望更新条目,因此它知道我刚刚添加的全新图片,并且应该通过“默认”链接到它...

图片actions.class.php:

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new PicForm();
    $this->form->bind(
                $request->getParameter($form->getName()),
                $request->getFiles($form->getName())
                );

    if ($form->isValid())
      {
        $this->pic = $form->save();
        $entry = $this->pic->getEntry();
        $entry->updateSetPic($this->pic);
        $this->redirect('entry_show', $entry);
      }
  }

最后,我在 lib/model/doctrine/Entry.class.php 文件中有以下 'updateSetPic' 方法:

  public function updateSetPic(Pic $pic)
  {
    $this->setPic($pic);
    $this->save();
  }

我现在遇到的问题是,看起来,upateSetPic 方法接收最近保存的 Pic 对象(在 Pic 操作 create 方法中),什么也没收到,就好像 $form->save( ) 方法中没有任何内容(尽管它确实保存了,但也许在这个阶段它没有提交或其他什么?)

所以我的问题是,我这样做好吗?或者我哪里错了?有没有办法完成我所需要的?如果是这样,那是什么方法?也许我需要融入 Pic 保存过程的另一部分,但我现在有点迷路了......

有什么想法吗?

谢谢!

【问题讨论】:

    标签: symfony1 symfony-forms symfony-plugins


    【解决方案1】:

    在您的“$form->isValid()”if 语句中,您调用 updateSetPic 的 $entrada 似乎没有设置。

    但要回答您的问题,您可能需要查看可以添加到对象 (http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/en) 的 postSave() 函数。

    【讨论】:

    • 对不起,$entrada 不正确,应该是 $entry ... 我会检查你给我的链接。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2012-03-07
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多