Google Groups Home
Help | Sign in
Many-to-Many sorting using refClass
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Lee Saferite  
View profile
 More options 8 May, 15:51
From: Lee Saferite <lee.safer...@gmail.com>
Date: Thu, 8 May 2008 07:51:55 -0700 (PDT)
Local: Thurs 8 May 2008 15:51
Subject: Many-to-Many sorting using refClass
Hi,

Sorry for cross posting this, but I'm getting no interest on the users
mailinglist, I figure maybe you guys would be able to help.

I've been banging my head on the wall trying to figure this out.  I
have a many-to-many relationship with a priority field on the
refClass, how do I use that to sort the records?

I've read this thread:
http://groups.google.com/group/doctrine-user/browse_thread/thread/840...

It really didn't help me much.  Maybe I'm missing something.  Is this
even possible?  I know I could write the query in SQL and hydrate my
objects from that, but I prefer to stay with DQL if at all possible.

Any help is greatly appreciated.

class Section extends Doctrine_Record
{
        public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 64);
                $this->hasColumn('priority', 'integer', 2);
    }
    public function setUp()
    {
        $this->hasMany(
                'Attribute as Attributes',
                array(
                        'local'    => 'section_id',
                        'foreign'  => 'attribute_id',
                        'refClass' => 'SectionAttribute'
                        )
                );
    }

}

class Attribute extends Doctrine_Record
{
        public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 64);
    }
    public function setUp()
    {
        $this->hasMany(
                'Section as Sections',
                array(
                        'local'    => 'attribute_id',
                        'foreign'  => 'section_id',
                        'refClass' => 'SectionAttribute'
                        )
                );
    }

}

class SectionAttribute extends Doctrine_Record
{
        public function setTableDefinition()
    {
                $this->hasColumn('section_id', 'integer', 4,
array('primary'=>true));
                $this->hasColumn('attribute_id', 'integer', 4,
array('primary'=>true));
                $this->hasColumn('priority', 'integer', 2);
    }
    public function setUp()
    {
        $this->hasOne(
                        'Section',
                        array(
                                'local'   => 'section_id',
                                'foreign' => 'id'
                        )
                );
        $this->hasOne(
                'Attribute',
                array(
                        'local'   => 'attribute_id',
                        'foreign' => 'id'
                )
        );
    }

}

$q = new Doctrine_Query();
$q->select('s.*, a.*');
$q->from('Section s');
$q->leftJoin('s.Attributes a');
$q->orderBy('s.priority DESC');

What do I add to the query to get the Attribute records sorted my the
priority on the SectionAttribute objects?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roman Borschel  
View profile
 More options 8 May, 16:00
From: Roman Borschel <r.borsc...@gmx.net>
Date: Thu, 8 May 2008 17:00:31 +0200
Local: Thurs 8 May 2008 16:00
Subject: Re: [doctrine-dev] Many-to-Many sorting using refClass
What about:
$q = new Doctrine_Query();
$q->select('s.*, a.*')
     ->from('Section s')
     ->leftJoin('s.SectionAttribute sa')
     ->leftJoin('sa.Attribute a')
     ->orderBy('s.priority DESC, sa.priority DESC');

Does that not work? When you set up a m-n association Doctrine creates  
1-m associations to the association class (SectionAttribute in your  
example) behind the scenes. You can always override them with your own  
setup to give them prettier names, of course.

Hope this works.

Regards

Roman

On May 8, 2008, at 4:51 PM, Lee Saferite wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roman Borschel  
View profile
 More options 8 May, 16:17
From: Roman Borschel <r.borsc...@gmx.net>
Date: Thu, 8 May 2008 17:17:48 +0200
Local: Thurs 8 May 2008 16:17
Subject: Re: [doctrine-dev] Re: Many-to-Many sorting using refClass
Ok, correction, the query i posted will most likely give you an  
exception because you need to select at least the PKs from the  
SectionAttribute class.
Here's a second try:
$q = new Doctrine_Query();
$q->select('s.*, sa.*, a.*')
     ->from('Section s')
     ->leftJoin('s.SectionAttribute sa')
     ->leftJoin('sa.Attribute a')
     ->orderBy('s.priority DESC, sa.priority DESC');

But i'm actually not sure whether Doctrine is currently smart enough  
so that accessing $a->Attributes will not trigger an additional query  
because this "relation" has not been initialized yet. You might need  
to traverse the associations in the way you fetched them (Section-
 >SectionAttribute->Attribute).

On May 8, 2008, at 5:00 PM, Roman Borschel wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lee Saferite  
View profile
 More options 8 May, 17:05
From: Lee Saferite <lee.safer...@gmail.com>
Date: Thu, 8 May 2008 09:05:07 -0700 (PDT)
Local: Thurs 8 May 2008 17:05
Subject: Re: Many-to-Many sorting using refClass
Yes, we currently access the like you mentioned, Section-

>SectionAttribute->Attribute.

We just wanted to avoid the level of indirection if possible.  I will
continue looking for a way to access Section->Attribute and have the
Attributes sorted by the SectionAttribute.priority, if you have any
more ideas, I'm all ears.

On May 8, 11:17 am, Roman Borschel <r.borsc...@gmx.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lee Saferite  
View profile
 More options 8 May, 17:09
From: Lee Saferite <lee.safer...@gmail.com>
Date: Thu, 8 May 2008 09:09:59 -0700 (PDT)
Local: Thurs 8 May 2008 17:09
Subject: Re: Many-to-Many sorting using refClass
Roman,

BTW, I noticed that there was a patch submitted a few months back to
add an orderBy option to the relation declaration, is that ever going
to become a reality?  Then I wouldn't need to specify an order on my
query.  And while i'm on the subject, why not be able to define an
indexby option as well?

danke,
Lee

On May 8, 11:17 am, Roman Borschel <r.borsc...@gmx.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan Wage  
View profile
 More options 8 May, 17:16
From: "Jonathan Wage" <jonw...@gmail.com>
Date: Thu, 8 May 2008 11:16:22 -0500
Local: Thurs 8 May 2008 17:16
Subject: Re: [doctrine-dev] Re: Many-to-Many sorting using refClass

In Doctrine 2.0 this will be possible to define any DQL parts in the
relationship definition, including order by.

- Jon

On Thu, May 8, 2008 at 11:09 AM, Lee Saferite <lee.safer...@gmail.com>
wrote:

--
Jonathan Wage
http://www.jwage.com
http://www.centresource.com

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lee Saferite  
View profile
 More options 8 May, 19:16
From: Lee Saferite <lee.safer...@gmail.com>
Date: Thu, 8 May 2008 11:16:13 -0700 (PDT)
Local: Thurs 8 May 2008 19:16
Subject: Re: Many-to-Many sorting using refClass
Roman,

   Jon posted a possible solution over in doctrine-user, but it turned
out not to work as well, Just in case you missed it. :)

   http://groups.google.com/group/doctrine-user/browse_thread/thread/c65...

Lee

On May 8, 11:17 am, Roman Borschel <r.borsc...@gmx.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lee Saferite