How to change Zend_Db_Table name within a Model to insert in multiple tables

Asked
Active3 hr before
Viewed126 times

3 Answers

change
90%

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?,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.,I think you should have another Model_DbTable, Model_DbTable_Bar and call it either,acts as a Gateway to a database table. One instance handles all the rows in the table.

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

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

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

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');
load more v
88%

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?,How to create a database schema for storing text in several languages? - postgresql,Also note that with ZF1.9 you can also instantiate Zend_Db_Table without having to define a specific subclass first, for example.,See the Zend_Db_Table chapter in the ZF Reference Guide .

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(); } } 
load more v
72%

[quote] It starts to falls down if a given model needs to know about two or more database tables to do its work. [/quote],This works fairly well for models where there is a one to one mapping to the underlying database table. It starts to falls down if a given model needs to know about two or more database tables to do its work.,Zend Framework Training,When working with models within Zend Framework, the simplest solution is to extend Zend_Db_Table_Abstract and Zend_Db_Table_Row_Abstract along these lines: class Users extends Zend_Db_Table_Abstract { protected $_name = 'users'; protected $_rowClass = 'User';

The “is a” relationship

When working with models within Zend Framework, the simplest solution is to extend Zend_Db_Table_Abstract and Zend_Db_Table_Row_Abstract along these lines:

class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_rowClass = 'User';

class Users extends Zend_Db_Table_Abstract {
   protected $_name = 'users';
   protected $_rowClass = 'User';
load more v

Other "change-undefined" queries related to "How to change Zend_Db_Table name within a Model to insert in multiple tables"