Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 671709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:24:13+00:00 2026-05-14T00:24:13+00:00

Using Zend Framework, I’ve created a Model to insert a record into a database.

  • 0

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();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:24:13+00:00Added an answer on May 14, 2026 at 12:24 am

    Zend_Db_Table is a Table Data Gateway. It

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

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

    and then commit() or rollback() 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.

    $fooTable = new Zend_Db_Table('foo');
    

    See the chapter on Zend_Db_Table in the ZF Reference Guide.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One solution would be to set your stop function to… May 15, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer You may want to try the joining the views table… May 15, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer Keep it simple. How about this? while ( my $tag… May 15, 2026 at 7:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.