• <small id='oo9TJ'></small><noframes id='oo9TJ'>

  • <i id='oo9TJ'><tr id='oo9TJ'><dt id='oo9TJ'><q id='oo9TJ'><span id='oo9TJ'><b id='oo9TJ'><form id='oo9TJ'><ins id='oo9TJ'></ins><ul id='oo9TJ'></ul><sub id='oo9TJ'></sub></form><legend id='oo9TJ'></legend><bdo id='oo9TJ'><pre id='oo9TJ'><center id='oo9TJ'></center></pre></bdo></b><th id='oo9TJ'></th></span></q></dt></tr></i><div id='oo9TJ'><tfoot id='oo9TJ'></tfoot><dl id='oo9TJ'><fieldset id='oo9TJ'></fieldset></dl></div>
      • <bdo id='oo9TJ'></bdo><ul id='oo9TJ'></ul>
      <tfoot id='oo9TJ'></tfoot>

      <legend id='oo9TJ'><style id='oo9TJ'><dir id='oo9TJ'><q id='oo9TJ'></q></dir></style></legend>
      1. 如何在模型中更改 Zend_Db_Table 名称以插入多个表

        时间:2023-05-29

        <small id='oCXPz'></small><noframes id='oCXPz'>

      2. <i id='oCXPz'><tr id='oCXPz'><dt id='oCXPz'><q id='oCXPz'><span id='oCXPz'><b id='oCXPz'><form id='oCXPz'><ins id='oCXPz'></ins><ul id='oCXPz'></ul><sub id='oCXPz'></sub></form><legend id='oCXPz'></legend><bdo id='oCXPz'><pre id='oCXPz'><center id='oCXPz'></center></pre></bdo></b><th id='oCXPz'></th></span></q></dt></tr></i><div id='oCXPz'><tfoot id='oCXPz'></tfoot><dl id='oCXPz'><fieldset id='oCXPz'></fieldset></dl></div>
      3. <legend id='oCXPz'><style id='oCXPz'><dir id='oCXPz'><q id='oCXPz'></q></dir></style></legend>
          <tbody id='oCXPz'></tbody>
        • <bdo id='oCXPz'></bdo><ul id='oCXPz'></ul>
        • <tfoot id='oCXPz'></tfoot>

                  本文介绍了如何在模型中更改 Zend_Db_Table 名称以插入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 Zend Framework,我创建了一个模型来将记录插入到数据库中.我的问题是,在 $this->insert($data) 之后,如何切换活动表以便将记录插入到另一个表中?

                  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?

                  这是我目前的代码:

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

                  推荐答案

                  Zend_Db_Table 是一个 表格数据网关.它

                  充当数据库表的网关.一个实例处理表中的所有行.

                  acts as a Gateway to a database table. One instance handles all the rows in the table.

                  这意味着,每张桌子都有一个班级.您的 Model_DbTable_Foo 表示数据库中的 Foo 表,并且仅表示该表.它不应该在其他表上插入.这就是您将使用另一个表类的目的.最干净的选择是在 TDG 之上添加另一个层,该层知道如何处理对多个表的插入,例如

                  This 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.

                  class Model_Gateway_FooBar
                  {
                      protected $_tables;
                  
                      public function __construct(Zend_Db_Table_Abstract $foo, 
                                                  Zend_Db_Table_Abstract $bar)
                      {
                          $this->_tables['foo'] = $foo;
                          $this->_tables['bar'] = $bar;
                      }
                  
                      public function addFoo($data)
                      {
                          $this->_tables['foo']->insert($data['foo']);
                          // yaddayaddayadda
                          $this->_tables['bar']->insert($data['bar']);
                      }
                  }
                  

                  但是,这是您的应用程序,您可以决定不打扰,只需在 Foo 类中创建另一个类的新实例,然后从那里进行插入,例如

                  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.

                  $otherTable = new Model_DbTable_Bar;
                  $otherTable->insert($data);
                  

                  另一种选择是将逻辑放入控制器中,但我不推荐这样做,因为这不是控制器的责任,通常 控制器要瘦,模型要胖.

                  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.

                  $this->_tables['foo']->getAdapter()->beginTransaction();
                  

                  然后是 commit()rollback() 取决于查询结果.

                  and then commit() or rollback() depending on the query outcome.

                  另请注意,从 ZF1.9 开始,您还可以创建 Zend_Db_Table 的实例,而无需先定义具体的子类,例如

                  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.

                  $fooTable = new Zend_Db_Table('foo');
                  

                  请参阅 ZF 参考指南中关于 Zend_Db_Table 的章节.

                  See the chapter on Zend_Db_Table in the ZF Reference Guide.

                  这篇关于如何在模型中更改 Zend_Db_Table 名称以插入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP、MySQL PDOException 的死锁异常代码? 下一篇:生成 PHPUnit 代码覆盖率报告时无法重新声明类错误

                  相关文章

                  <small id='i8zMp'></small><noframes id='i8zMp'>

                1. <legend id='i8zMp'><style id='i8zMp'><dir id='i8zMp'><q id='i8zMp'></q></dir></style></legend>
                      <bdo id='i8zMp'></bdo><ul id='i8zMp'></ul>
                  1. <i id='i8zMp'><tr id='i8zMp'><dt id='i8zMp'><q id='i8zMp'><span id='i8zMp'><b id='i8zMp'><form id='i8zMp'><ins id='i8zMp'></ins><ul id='i8zMp'></ul><sub id='i8zMp'></sub></form><legend id='i8zMp'></legend><bdo id='i8zMp'><pre id='i8zMp'><center id='i8zMp'></center></pre></bdo></b><th id='i8zMp'></th></span></q></dt></tr></i><div id='i8zMp'><tfoot id='i8zMp'></tfoot><dl id='i8zMp'><fieldset id='i8zMp'></fieldset></dl></div>
                    <tfoot id='i8zMp'></tfoot>