Re: How to use ORDER BY within Containable Behavior

2010-10-08 Thread Paul Bricks
But if i would do it like your way: $this-Post-Tag-Category-
find...,
i have to change my view.
Because
['Post']['Tag']['Category']
is now
['Category']['Tag']['Post']

And what, if i would like to orderBy Tag.name?
Than i have do set up three different view-loops to print out the
result array!

Yesterday, there comes a little idea in my mind: I could do that with
a
database-view. So i will have only one cake-view-loop to print out my
posts
and can sort it like i want to. What do you think about that way??

Another User named teknoid said, that it would work with joins-key
on the find() clause. Didnt get this work with joins in cakephp
until now.
Read it here:
http://nuts-and-bolts-of-cakephp.com/2008/07/17/forcing-an-sql-join-in-cakephp/

May be you get it work with joins??


On 7 Okt., 06:28, cricket zijn.digi...@gmail.com wrote:
 On Tue, Oct 5, 2010 at 6:03 PM, Paul Bricks



 eric.liechtenst...@googlemail.com wrote:
  I would like to have a sorted-array (order by) to print out in my Post-
  View.
  The Problem is, that the order-by-attribute (Category.name) lies
  deeper
  than attributes (Post.id, Tag.id, ...), which have to be ordered.

  Here is my example:

  Relations:
  Post -- hasMany -- Tag -- belongsTo -- Category

  PostsController:
  ...
  $posts = $this-Post-find('all', array (
         'contain' = array (
                 'Tag' = array (
                         'Category' = array (
                                 'fields' = array (
                                         'id',
                                         'name'
                                 )
                         )
                 )
         )
  ));
  $this-set('posts', $posts);
  ...

  Now i would like to to print out the posts-aray in my Post-View
  to get this ordered table (order-by Category.name ASC):

  Post.id         Tag.id          Category.name
  -
  1                       3                       Basketball
  1                       2                       Football
  2                       4                       Hockey
  4                       1                       Tennis

  And my Question is:
  Where do i have to put the order-clause to get this array, shown
  above.
  And if this will not work like this way, how will it work otherwise?

 So, you want to order the Posts by Category? Is that correct?

 Normally, one can include an 'order' param at the same level one would
 put 'fields', at whatever level under 'contain'. However, I don't
 think what you want to do is possible with a find() on Post. What you
 could do, though, is run the find() on Category, taking advantage of
 the chain of associations.

 $data = $this-Post-Tag-Category-find(
         'all',
         array(
                 'fields' = array(
                         'Category.id',
                         'Category.name'
                 ),
                 'order' = array('Category.name' = 'ASC'),
                 'contain' = array(
                         'Tag' = array(
                                 'fields' = array(
                                         'Tag.id',
                                         'Tag.name' // for example
                                 ),
                                 'Post' = array(
                                         'fields' = array(
                                                 'Post.id',
                                                 'Post.title' // for example
                                         )
                                 )
                         )
                 )
         )
 );

 Note that you should specify the fields for Post or you'll fetch
 everything (which it appears you don't want). Also, you should get in
 the habit of specifying the model name. There are situations where not
 doing so can lead to bad queries (eg. Column 'id' in field list is
 ambiguous).

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


How to use ORDER BY within Containable Behavior

2010-10-06 Thread Paul Bricks
I would like to have a sorted-array (order by) to print out in my Post-
View.
The Problem is, that the order-by-attribute (Category.name) lies
deeper
than attributes (Post.id, Tag.id, ...), which have to be ordered.

Here is my example:

Relations:
Post -- hasMany -- Tag -- belongsTo -- Category


PostsController:
...
$posts = $this-Post-find('all', array (
'contain' = array (
'Tag' = array (
'Category' = array (
'fields' = array (
'id',
'name'
)
)
)
)
));
$this-set('posts', $posts);
...


Now i would like to to print out the posts-aray in my Post-View
to get this ordered table (order-by Category.name ASC):

Post.id Tag.id  Category.name
-
1   3   Basketball
1   2   Football
2   4   Hockey
4   1   Tennis


And my Question is:
Where do i have to put the order-clause to get this array, shown
above.
And if this will not work like this way, how will it work otherwise?

PS:
This is the pr($posts); which i want to have:


Array
(
[0] = Array
(
[Post] = Array
(
[id] = 1
)
[Tag] = Array
(
[0] = Array
(
[id] = 3
[Category] = Array
(
[id] = 1
[name] = Basketball
)
)
[1] = Array
(
[id] = 2
[Category] = Array
(
[id] = 5
[name] = Football
)
)
)
)
[1] = Array
(
[Post] = Array
(
[id] = 2
)
[Tag] = Array
(
[0] = Array
(
[id] = 4
[Category] = Array
(
[id] = 1
[name] = Hockey
)
)
)
)
[2] = Array
(
[Post] = Array
(
[id] = 4
)
[Tag] = Array
(
[0] = Array
(
[id] = 1
[Category] = Array
(
[id] = 1
[name] = Tennis
)
)
)
)
)

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en