[Solar-talk] How can i implement ordering on a _belongsTo or_hasMany relationship

Kilbride, James P. James.Kilbride at gd-ais.com
Fri Feb 15 14:08:47 CST 2008


Antti,

That looks like you are duplicating the relationship defined in the
model in that fetchRelated call when you do the department_id =
someValue. I have a department record, from that record I want to pull
all it's children records which have some column equal to something. Say
I have a user and a conversation table where users have many
conversations and each conversation has many users(basically which users
were in the conversation). The conversation to user relationship is
called users.

I've pulled out a set of conversations. 

$conversations = $convers->fetchAll(someStuff);

and now I want only the inactive users on those conversations:

foreach($conversations as $conversation) {
	$allUsers = $conversation->users;
	$inactiveUsers = $conversation->fetchRelated(array( 'where' =>
"active = 0" ), 'users'); //doesn't work.
}

How do I get all the inactive only users for that given record in the
conversation list?

James Kilbride

-----Original Message-----
From: solar-talk-bounces at lists.solarphp.com
[mailto:solar-talk-bounces at lists.solarphp.com] On Behalf Of Antti
Holvikari
Sent: Friday, February 15, 2008 11:01 AM
To: solar-talk at lists.solarphp.com
Subject: Re: [Solar-talk] How can i implement ordering on a _belongsTo
or_hasMany relationship

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>
_______________________________________________
Solar-talk mailing list
Solar-talk at lists.solarphp.com
http://mailman-mail3.webfaction.com/listinfo/solar-talk



More information about the Solar-talk mailing list