1
votes

I have created a model in Zend Framework 1.x which extends the Zend_Db_Table_Abstract class.

require_once 'Zend/Db/Table/Abstract.php';

class Model_Account extends Zend_Db_Table_Abstract
{

    protected $_name = 'accounts';

    public function someFunction()
    {
         //do something
    }

}

In Zend Framework, you have to declare the table name in the $_name property. At the moment, I only know how to declare one table name at a time, which means that my SQL statements will all be directed to the given table specified in the $_name property.

Is there a way to declare multiple tables in the model so that I can effectively execute a join statement?

1
This is Zend Framework 1.x.. Also, your question is unclear.Yes Barry
@mmmshuddup - thanks I've cleaned it up, hope this is more clearFrank Castle
This is still Zend Framework 1, not 2.Yes Barry

1 Answers

3
votes

I'm understanding that you want to have more than one model so you can execute a join. Actually, you can accomplish a join without actually needing a second model.

require_once 'Zend/Db/Table/Abstract.php';

class Model_Account extends Zend_Db_Table_Abstract
{
    protected $_name = 'accounts';

    public function someFunction()
    {
        $otherTable = array('ot' => 'other_table');
        $columns    = array(
           'accounts.column_name', 
           'accounts.other_column_name'
        );
        $select = $this->select()
            ->from($this, $columns)
            ->join($otherTable, 'ot.accounts_id = accounts.id', array());

        $rowset = $this->fetchAll($select);
        return $rowset;
    }
}

If you want another model, just create a new PHP file in your models folder and declare your class there.

class Model_OtherTable extends Zend_Db_Table_Abstract
{
    protected $_name = 'other_table';

    // ...
}

An example of using that would be:

$accounts = new Model_Account();
$results  = $accounts->someFunction();
foreach ($results as $result) {
    echo $result->column_name, '<br>';
    echo $result->other_column_name, '<br>';
}

// to see output of result set do this
Zend_Debug::dump($results->toArray());