Welcome!

On Feb 3, 2011, at 08:12, Michael Carriere wrote:

>       • How "magical" is the find() function? Should I be able to run one 
> exhaustive query and get back all the nested data that I need for a View to 
> spit out? I guess a better question would be: do you find yourself calling 
> find() on different sets of data, packaging them together yourself 
> (presumably with the Set class, right?) and then passing that to the view to 
> be displayed?
> 
> I have some data that is loosely related, and while I can manage to get at 
> all of it in one query, it requires me using Containable, and dropping 5-6 
> associations in. That just seems quite inefficient for me. (Maybe it's not?)

Based on what i've read, Containable seems like the best answer to your 
question. Without Containable, you can specify a recursion level, but that's 
it. Much better to use Containable to say exactly which items from which levels 
you want to receive, and it figures out the rest.


>       • Coming from many OO languages, after you define a class, you 
> instantiate it as you want to work with an individual object, play with it as 
> you want, and throw it out. Correct me if I'm wrong, but it seems that MVC's 
> approach is more geared towards operating on all the data at once? Or at 
> least in the case of working with a "Model".
> 
> This leads to some confusion for me, because the majority of the instances 
> where I need to access data, it's of a small subset of the data I'm storing 
> in the DB for my model. My webgame has "Buildings" in it, which belong to a 
> User's "Village". Sometimes I want to grab information from a specific 
> Building, and other times I want to grab only the specific Buildings of a 
> Village. Is it proper to be define a function within the model that grabs or 
> manipulates data based on this, like 
> $this->Village->getBuildingsByVillageId()? Better yet, how do you operate on 
> 'instances' of your data as defined by your model?

I have questions along these lines too.

It seems to me that the Model is there to let you access your data, usually 
from a database table (though it doesn't have to be). So a Model does not 
represent an instance of something from your database table*; rather, it is an 
interface to retrieve data from it -- be that a single record or a set of 
records, and possibly, depending on recursion or Containable, related records. 
This data is usually returned by the find() method, and is in the form of a 
rather nested array. You receive the array from the Model (in your Controller, 
or in a Shell perhaps), loop over the records, and do whatever you want with 
them.

Yes, it seems correct to define additional methods in your Model that return 
data from particular queries that are of use to you. It seems to me that if 
your method is getBuildingsByVillageId(), then it would be in the Building 
model (because it primarily returns information about Buildings), not in the 
Village model, though I'm not clear what the guidance is on this topic.

*This is complicated by the fact that in some cases you can get data from a 
particular row to live in the Model -- to be the "active record" -- and you can 
page through all the records in your data set this way. I'm not certain yet 
whether this is just an alternate way to access the same data that's just more 
comfortable to some people, or whether there are cases when this is the only 
way to do it.

It's still unclear to me what the best way is to, for example, define 
additional variables that go with a particular record. My first impulse was to 
define an instance variable in the Model, but in light of the above, that 
doesn't seem correct.




-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to