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?
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'
)
);
}
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.
> 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?
> 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' > ) > ); > }
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).
> 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:
>> 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?
>> 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' >> ) >> ); >> }
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:
> 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:
> > 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:
> >> 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?
> >> 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.
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:
> 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:
> > 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:
> >> 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?
> >> 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.
> 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: > > 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:
> > > 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:
> > >> 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?
> > >> 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.
> 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:
> > 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:
> >> 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?
> >> 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.