symfony 1.4 在保存另一个与之相关的对象时(或之后)更新某个对象

发布于 2024-10-20 12:38:21 字数 1995 浏览 4 评论 0原文

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

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

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

Schema:

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 可能有很多图片,但我创建的第一个图片也必须定义为我在 Entry.pic 字段中建立的“默认”图片...

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

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 actions create 方法中),只是接收不到任何内容,就好像 $form->save() 方法中的 Pic 对象中没有任何内容(尽管它确实保存了,但也许在这个阶段还没有承诺什么?)

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

有什么想法吗?

谢谢你!

I have a doctrine form in Symfony, which saves certain object in a table in the DB.
By doing this, I wish to also update another object, which has a relation to the first one, so the objects get the relation (one-to-many) updated as the first object gets saved.

I really don't know if this is possible at all...

I'll post my code, as a reference:

Schema:

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

I have another many-to-many relation between Pic and Entry, because an Entry may have a lot of Pics, but the first Pic I create must also be defined as the 'default' Pic which I stablish in the Entry.pic field...

(I previously and correctly create an Entry, so now I must get a new Pic, which will be the 'default' one for the Entry)... In the create actions for the Pic table, I wish to update the Entry so it knows the brand new Pic I have just added and that should get linked by 'default' with it...

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);
      }
  }

Finally, I have the following 'updateSetPic' method in the lib/model/doctrine/Entry.class.php file:

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

The problem I have right now is that, seemingly, the upateSetPic method, receiving the recently saved Pic object (in the Pic actions create method), just receives nothing, as if the Pic object that the $form->save() method has nothing in it (even though, it truly saves, but maybe at this stage it isn't commited or something?)

So my question is, am I doing this all right? Or where am I wrong? Is there a way to acomplish what I need? If so, what is that way? Maybe I need to mingle into another part of the Pic save process, but I'm kind of lost by now...

Any ideas?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2024-10-27 12:38:21

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

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

In your "$form->isValid()" if statement, it seems your $entrada, that you're calling updateSetPic on, is not set.

But to answer your question, you might want to look at the postSave() function you can add to your object ( http://www.doctrine-project.org/projects/orm/1.2/docs/manual/event-listeners/en ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文