On 5 Nov 2008, at 23:23, Matthew Weier O'Phinney wrote:

-- WildFoxMedia <[EMAIL PROTECTED]> wrote
(on Wednesday, 05 November 2008, 02:48 PM -0800):
Hey guys, im new to Zend, ive been a long time user of Cake, I have a few
questions related to conventions, etc.


2. Is there a 'magic' save() method for saving to the database? I see
currently there is Insert & Update, I dont mind using insert & update, was
just curious.

There's not. I'm not entirely sure why Zend_Db_Table does not implement this, though my understanding is that there are some sound architectural
reasons not to do so. I've often created such a method myself, though,


Zend_Db_Table_Row has save().

$table = new Pages();
$page = $id > 0 ? $table->fetchRow('id='.$id) : $table->createRow();
$page->title = $newTitle;
$page->body = $newBody;
$page->save();



3. Is there any kind of table inflection when you insert or update? What I mean is if you have a table with 2 columns, id & name the array I try to insert has 3 array keys for id, name & created it throws an error - Cake does table inflection where it only attempts to save columns that exist in
the table.

Again, there's not, and again, uncertain as to why. I've often
implemented such functionality for my own models, however.

Zend_Db_Table_Row won't let you use a property that doesn't exist as a row.

i.e:  $page->not_in_db = '0'; will throw an exception.

4. Is there any decent way to autoload models instead of doing....
$this->someModel = new someModel; in the init() of every controller?

No. If you're interested in doing something like this, however, you may
want to look at action helpers, and in particular how the ViewRenderer
action helper works (it does this sort of functionality for views).

Funnily enough, I just wrote an article on hooks in action helpers at http://akrabat.com .

Essentially, you need to write an action helper that is along the lines of:

class Zend_Controller_Action_Helper_ModelLoader extends Zend_Controller_Action_Helper_Abstract
{
    function preDispatch()
    {
        $controller = $this->getActionController();

        $modelName = $this->_pickModelToLoadBasedOnControllerName($controller);
        $controller->$modelName = new $modelName;
    }

protected function _pickModelToLoadBasedOnControllerName($controller)
    {
        // implementation here - return a string
    }
}

Then you register it with the helper broker in your bootstrap.


Regards,

Rob...

Reply via email to