[Solar-talk] How can i implement ordering on a _belongsTo or
_hasMany relationship
Antti Holvikari
anttih at gmail.com
Fri Feb 15 10:01:06 CST 2008
On Fri, Feb 15, 2008 at 5:57 PM, Paul M Jones <pmjones at paul-m-jones.com> wrote:
>
>
> On Feb 15, 2008, at 09:41 , Cruz, Darwin wrote:
>
> > I'm using the Solar models and need to implement a order by against
> > a database table column that i'm querying through a
> >
> > _belongsTo or _hasMany relationship.
> >
> > So if i have a DEPARTMENT model that has many EMPLOYEES my
> > DEPARTMENT model has the following relationship defined.
> >
> > $this->_hasMany('employees', array(
> > 'foreign_class' => 'Project_Models_Employee',
> > 'foreign_key' => 'department_id',
> > ));
> >
> > So if i want to find out all the employess in a specific department
> > i cause do the following $myDepartment->employees to get all the
> > employee records. Is there any way i can implement an ORDER BY
> > clause against those employee records?
> >
>
> Try adding an 'order' to that array. E.g., 'order' =>
> 'employees.department_id' -- that *should* do the trick.
>
> For more info on the available keys for the relationship array, take a
> look at ...
>
> Well hell, I thought I had inline docs on those. Short version: you
> can add keys for these portions of the relationship query:
>
> * cols (string|array)
> * distinct (bool)
> * where (string)
> * group (string)
> * having (string)
> * order (string)
> * paging (int)
> * fetch (string)
>
> See the properties named for those keys in Solar_Sql_Model_Related.
>
> Hope this helps, please let me know if it does not.
Yep but that is a permanent order. That is to say, it will then be
always that, everytime you query. Maybe that is what you want. But I
think here's another option:
At a quick glance on the API I think you can use
$model->fetchRelatedObject() or *Array().
$department_model->fetchRelatedObject(
array('order' => 'whatever', 'where' => array('department_id =
:dep_id')),
'employees',
array(':dep_id' => $department_record->id),
);
--
Antti Holvikari <http://anttih.com>
More information about the Solar-talk
mailing list