views:

276

answers:

2

I'm trying to understand how the Zend Framework works.Are the models designed to do something like this?I have just a basic setup, so I can use in my controllers something like this:

$db->query($this->selectAll())

Can you also give me an example on how to use this on a controller?

class Country extends Zend_Db_Table
{

    protected $_name = 'country';

    public function selectAll()
    {
     return 'SELECT * FROM'.$this->_name.'';
    }

}

Best Regards!

+1  A: 

Zend Models are desigend to be linked to a table and help you to interact with a table.

class BugsProducts extends Zend_Db_Table_Abstract
{
    protected $_name = 'bugs_products';
    protected $_primary = array('bug_id', 'product_id');
}

$table = new BugsProducts();

$rows = $table->fetchAll('bug_status = "NEW"', 'bug_id ASC', 10, 0);
$rows = $table->fetchAll($table->select()->where('bug_status = ?', 'NEW')
                                         ->order('bug_id ASC')
                                         ->limit(10, 0));

// Fetching a single row
$row = $table->fetchRow('bug_status = "NEW"', 'bug_id ASC');
$row = $table->fetchRow($table->select()->where('bug_status = ?', 'NEW')
                                        ->order('bug_id ASC'));

more informations in the manual

solomongaby
+2  A: 

Pedantic terminology: Zend_Db_Table is a class to represent database tables. This is not the same thing as a Model in the MVC sense.

I wrote a lot of the documentation for the Zend_Db components, and nowhere did I treat Tables and Models as synonyms (as many frameworks do).

Also see a blog I wrote on this subject:

http://karwin.blogspot.com/2008/05/activerecord-does-not-suck.html

Bill Karwin