query() method, the answer is yes.
Alex, I found it tricky to get my head around the seemingly complex
array structure in Cake at first, but it all makes sense.
- You need related model titles as keys to the array to ensure that
you can nest result sets within each other, i.e hasMany/belongsTo
etc,
- You need to keep the Model name for the model you called from
because (amongst numerous other good reasons) be able to access these
results sets consistently throughout the framework, without knowing
which model they happened to be called from originally, as per
Martin's example.
The trick is to manipulate it directly as little as possible - if you
leverage all of cake's auto-magic and features for manipulating data
in Cake structures, you rarely need to concern yourself with how many
dimensions a reuslt array has.
If you just don't like writing $carRecord['Car']['id'] and similar
over and over, just call PHP's extract() method to make the code
quicker to write and easier to read.
Hope that helps...
On Sep 26, 8:39 pm, nachopitt <nachop...@gmail.com> wrote:
> So.. can you "query" an unrelated model from any model with the query
> model?
>
> On Sep 26, 11:01 am, alex_c <alex.cure...@gmail.com> wrote:
>
> > Thanks, that is extremely helpful, and makes a lot of sense!
>
> > >Querying "pictures AS
> > > Picture" will take you one step towards what is returned from Cake's
> > > find-methods
>
> > That helps a lot. I guess I had assumed that since I was using $this->Picture->query, Cake was aware that the model should be Picture - but
>
> > on second thought, it has no way of knowing that the query will
> > actually return a Picture.
>
> > Thank you for the helpful answer, it clears up a lot of things for me.
>
> > Alex
>
> > On Sep 26, 10:15 am, "martin.westin...@gmail.com"
>
> > <martin.westin...@gmail.com> wrote:
> > > The Model is quite a bit different in CakePHP compared to Rails.
> > > Since all data is returned as an array structure (and not objects) it
> > > is "dead" data and not "live" objects. It has to be laid out for you
> > > in advance.
>
> > > query vs. find
> > > query is pretty much a wrapper for straight SQL queries. Since you
> > > query "pictures" that is what you get back. Querying "pictures AS
> > > Picture" will take you one step towards what is returned from Cake's
> > > find-methods. Using find you are asking Cake to find data for Models.
> > > Using qurey you are only using Cakes dbo layer to ask sql queries.
>
> > > find('first') vs. find('all')
> > > When asking for a single record you will get back an array with the
> > > model's name as a key to access its data. $data['Picture']...
> > > When asking for multiple records you will get back an numeric array.
> > > Each one of the elements in that array will be identical to what the
> > > single record returned. $data[2]['Picture']...
>
> > > Why the ['Picture']?
> > > My take on why this structure was chosen is that is allows the same
> > > view-elements to render from many different find operations. If
> > > Article hasMany Picture you would access the picture data the same
> > > from Article as from Picture directly.
> > > Picture->find returns an array with a Picture key
> > > Article->find returns an array with both Article and Picture keys.
> > > A view element used to show pictures anywhere in the application would
> > > never need to know that the pictures were found as related data or
> > > not. The same goes for the form helper and any other code used to
> > > manipulate data.
>
> > > At least that is one possible reason. I was not in the room when they
> > > came up with it ;)
>
> > > I hope that was helpful. Just remember that Cake is (or once was)
> > > similar to Rails in an overall perspective on things but not really in
> > > the details.
>
> > > On Sep 26, 3:49 am, alex_c <alex.cure...@gmail.com> wrote:
>
> > > > Hi all,
>
> > > > I am new to CakePHP. I've used both PHP (with no frameworks) and Ruby
> > > > on Rails in the past, and decided that learning a PHP framework might
> > > > make PHP projects a lot more pleasant. I chose CakePHP because I'm
> > > > familiar with Rails - so far it's great, but I do have to wrap my head
> > > > around the differences.
>
> > > > I'm a bit unsure on what is the "CakePHP way" to use data returned
> > > > from the model. To keep things simple, let's say I have a model
> > > > Picture, with only one field, id.
>
> > > > What's throwing me off is that the data returned doesn't always seem
> > > > to follow a consistent format. For example,
>
> > > > $this->Picture->findById("1")
>
> > > > will return
>
> > > > Array
> > > > (
> > > > [Picture] => Array
> > > > (
> > > > [id] => 1
> > > > )
> > > > )
>
> > > > but
>
> > > > $this->Picture->query("SELECT * FROM pictures WHERE id=1")
>
> > > > will return
>
> > > > Array
> > > > (
> > > > [0] => Array
> > > > (
> > > > [pictures] => Array
> > > > (
> > > > [id] => 1
> > > > )
> > > > )
> > > > )
>
> > > > It feels a bit odd to have to write, for example, $pic["Picture"]
> > > > ["id"] everywhere, rather than $pic["id], but I can live with that.
>
> > > > What's really throwing me off is $pic["Picture"]["id"] versus
> > > > $pic["pictures"]["id"]. Having to account for that every time I
> > > > either read or use data seems... well... it really doesn't feel right.
>
> > > > So, am I missing anything? Am I supposed to massage the data returned
> > > > from Find and Query methods in the controller? How do you guys use it
> > > > in your views and helpers, to keep things as simple and consistent as
> > > > possible?
>
> > > > I'm using 1.2.0.7296-RC2.
>
> > > > Thanks for any help!
>
> > > > Alex
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment