On Fri, Jan 30, 2009 at 2:35 PM, BeijingDave <[email protected]> wrote: > > I think I'm missing something..... > > When I fetch with the pagination function, it returns an array of > associative arrays rather than an array of objects based on my > model. > > Questions... > > 1) Is the model class supposed to be used as a data container where > you can extend it with custom accessor methods etc. Or is it more of > for abstraction of database operations?
It's a little of both. The CakeBook section on models[1] does a good job of explaining it. > 2) Is there an elegant mechanism in Cake (or PHP for that matter) that > will take the result set (array) and convert it into an object (either > based on it's model class or an entirely separate data container > class)? AFAIK, yes. I've seen something that was created to allow one to do just that. I can't remember where, though. I just looked on the google CakePHP group FAQ[2] but it doesn't seem to be there. As far as "elegant" goes, I haven't a clue whether it even works or not. Personally, i'd avoid doing that. If you're set (heh) on using objects with accessor methods then Cake probably isn't for you. I had some difficulties with that at first but have come around to the Cake way. I find it's actually far more flexible in the long run. > 3) Let's take an overly simplified example. I want a derived value > that multiple controllers/views will need to use. I don't want to > repeat the business logic in each controller and I'd preferably like > it in 1 place. Where does one do it? Here's a straw man that I hope > someone can use to help me understand this better..... Use afterFind(). You can concatenate the 'first_name' & 'last_name' values from your data array as a new field called 'fullname'. Have a look at the cookbook[3] for examples of using afterFind. Alternatively, you can use the AutoFieldBehavior[4], which will do the heavy lifting for you. I suggest you get a grip on how afterFind is used, then have a look at how that behavior abstracts the functionality to work with any model. [1] http://book.cakephp.org/view/67/Understanding-Models [2] http://groups.google.com/group/cake-php/web/faq [3] http://book.cakephp.org/view/76/Callback-Methods [4] http://bin.cakephp.org/saved/30870 I've recently modified AutoField so that I can pass field values through callbacks in a similar manner to Model validation. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
