-- Rob Allen <[EMAIL PROTECTED]> wrote
(on Thursday, 06 November 2008, 07:03 AM +0000):
> 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();

Ah, right. But Zend_Db_Table itself does not have it. (I typically don't
use the row objects -- probably should).

> > > 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.

Right -- and Zend_Db_Table::insert() and update() also throw exceptions
if you attempt to save a key that does not resolve to a field. I
personally feel we should have a flag allowing these methods to ignore
unknown keys -- it would make saving data to multiple tables much
easier.

> > > 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.

There's also an article I wrote on action helpers for a more in-depth
look:

    http://devzone.zend.com/article/3350-Action-Helpers-in-Zend-Framework

The DevZone has a lot of ZF tutorials. Rob himself has also written a
ton on his website, as well as the website for his upcoming book, Zend
Framework in Action. 

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to