如何更改模型中的 Zend_Db_Table 名称以插入多个表
使用 Zend Framework,我创建了一个模型来将记录插入数据库。我的问题是,在 $this->insert($data)
之后,如何切换活动表以便可以将记录插入到另一个表中?
到目前为止,这是我的代码:
class Model_DbTable_Foo extends Zend_Db_Table_Abstract
{
protected $_name = 'foo';
public function addFoo($params)
{
$data = array(
'foo' => $params['foo'],
);
$this->insert($data);
$foo_id = $this->getAdapter()->lastInsertId();
$data2 = array(
'bar' => $params['bar']
);
// I need to change the Db Table name here.
$this->insert($data2);
$bar_id = $this->getAdapter()->lastInsertId();
}
}
Using Zend Framework, I've created a Model to insert a record into a database. My question is, after $this->insert($data)
how can I switch the active table so that I can insert a record into another table?
Here's my code so far:
class Model_DbTable_Foo extends Zend_Db_Table_Abstract
{
protected $_name = 'foo';
public function addFoo($params)
{
$data = array(
'foo' => $params['foo'],
);
$this->insert($data);
$foo_id = $this->getAdapter()->lastInsertId();
$data2 = array(
'bar' => $params['bar']
);
// I need to change the Db Table name here.
$this->insert($data2);
$bar_id = $this->getAdapter()->lastInsertId();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Zend_Db_Table
是一个表数据网关。它这意味着,每张桌子有一个班级。您的
Model_DbTable_Foo
代表数据库中的 Foo 表,并且仅代表此表。它不应该在其他表上进行插入。这就是您使用另一个表类的目的。最干净的选择是在 TDG 之上添加另一个层,该层知道如何处理对多个表的插入,例如但是,这是您的应用程序,您可以决定不打扰并简单地在 Foo 中创建另一个类的新实例类并从那里进行插入,例如
另一个选择是将逻辑放入控制器中,但我不推荐它,因为这不是控制器的责任,通常 控制器应该保持瘦,模型应该胖。
顺便说一句,当您进行多次插入时,您可能希望使用事务来使两个插入按预期工作,例如,
然后
commit()
或rollback()
取决于查询结果。另请注意,从 ZF1.9 开始,您还可以创建 Zend_Db_Table 的实例,而不必先定义具体的子类,例如
请参阅 ZF 参考指南中的 Zend_Db_Table。
Zend_Db_Table
is a Table Data Gateway. ItThis means, you have one class per table. Your
Model_DbTable_Foo
represents the Foo table in your database and only this table. It should not do inserts on other tables. That's what you would use another table class for. The cleanest option would be to add another layer on top of your TDGs that knows how to handle inserts to multiple tables, e.g.However, it's your app and you can decide not to bother and simply create a new instance of the other class in the Foo class and do the insert from there, e.g.
Another option would be to put the logic into the controller, but I cannot recommend it, because this is not the responsibility of a controller and generally controllers should be kept thin and models should be fat.
On a sidenote, when you're doing multiple inserts, you might want to use transactions to make both inserts work as expected, e.g.
and then
commit()
orrollback()
depending on the query outcome.Also note that as of ZF1.9, you can also create instances of Zend_Db_Table without having to define a concrete subclass first, e.g.
See the chapter on Zend_Db_Table in the ZF Reference Guide.
我认为您应该有另一个
Model_DbTable
、Model_DbTable_Bar
并将其命名为内部:
或外部:
I think you should have another
Model_DbTable
,Model_DbTable_Bar
and call it eitherinternally:
or externally:
您也可以使用类Zend_Db_Table。
像这样的事情:
Also you can use the class Zend_Db_Table.
Something like this: