我在 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 字段中建立的“默认”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 操作 create 方法中),什么也没收到,好像 $form->save() 方法具有的 Pic 对象里面什么都没有(尽管它确实可以节省,但也许在这个阶段它没有被提交或其他什么?)
所以我的问题是,我这样做好吗?或者我哪里错了?有没有办法完成我所需要的?如果是这样,那是什么方法?也许我需要融入 Pic 保存过程的另一部分,但我现在有点迷路了......
有任何想法吗?
谢谢!