How to change the name Zend_Db_Table in a model to be inserted into multiple tables

Using the Zend Framework, I created a model for inserting 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 is my code:

 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(); } } 
+3
source share
3 answers

Zend_Db_Table is a Table Data Gateway . it

acts as a gateway to the database table. One instance processes all the rows in the table.

This means that you have one class for each table. Your Model_DbTable_Foo represents the Foo table in your database and only this table. It should not be inserted into other tables. This is what you would use a different table class for. The cleanest option is to add another layer on top of TDG, which knows how to handle inserts into multiple tables, for example.

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

However, this is an application, and you can decide not to worry and just create a new instance of another class in the Foo class and make an insert there, for example.

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

Another option would be to put the logic in the controller, but I cannot recommend it, because it is not the responsibility of the controller and usually the controllers should be thin and the models should be thick .

In isolation, when you do multiple inserts, you can use transactions to make both inserts work as expected, for example.

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

and then commit() or rollback() depending on the result of the request.

Also note that with ZF1.9 you can also instantiate Zend_Db_Table without having to define a specific subclass first, for example.

 $fooTable = new Zend_Db_Table('foo'); 

See the Zend_Db_Table chapter in the ZF Reference Guide .

+6
source

I think you should have Model_DbTable , Model_DbTable_Bar and call it either

inside

 class Model_DbTable_Foo ... { public function addFooAndBar() { ... $bar = new Model_DbTable_Bar(); $bar->insert($data2); } } 

or outside :

 class ModelX ... { public function addFoos() { $foo = new Model_DbTable_Foo(); $foo->insert($data); $bar = new Model_DbTable_Bar(); $bar->insert($data2); } } 
+1
source

You can also use the Zend_Db_Table class. Something like that:

  $fooTbl = new Zend_Db_Table('foo'); $lastFooID = $fooTbl->insert($data2) ->getAdapter() ->lastInsertId(); 
0
source

Source: https://habr.com/ru/post/1305391/


All Articles