Yii Framework Forum: Model -> Search -> Long Relation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Model -> Search -> Long Relation Urgent! Search through two steps of relations Rate Topic: -----

#1 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 24 January 2013 - 08:15 PM

Hi,

Let's say I have the situation:

A company has departments and departments have employees

Each respective table has `Id` primary key and relations are defined by foreign keys using these Id numbers.

Now I have a CGridView of employees and I would like to show name of department he/she is working in (that is easy) as well as name of the company he/she belongs (difficult).

Well - in a grid I can show:

array('name'=>'company_name', 'value'=>'$data->department->company->name'),


This is showing company names appropriately but no joy with filtering/sorting !!!

I am unable to construct working seach criteria in a model. Whatever I try - I get kind of 'column not found' SQL error.

So - how to overcome? It's urgent!
Thanks ahead for suggestions!
0

#2 User is offline   Maclein 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 25-April 12

Posted 25 January 2013 - 12:48 AM

Your problem is not that much clear to me, I think you are unable to sort and filter using relations.
Use of "with" clause can sort out your problem.
Take a look over here
How to use 'with' clause for relations

Thanks,
Maclein
0

#3 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 25 January 2013 - 04:53 AM

Hi Maclein,

For simplification:

I am looking for Yii criteria syntax for:

SELECT COUNT(DISTINCT `t`.`id`)
FROM `employees` `t`
LEFT OUTER JOIN `departments` `department`
ON (`t`.`department_id`=`department`.`id`) 
LEFT OUTER JOIN `companies` `company`
ON (`department`.`company_id`=`company`.`id`)
WHERE (`company`.`name` LIKE 'Hewlett%');


Actually, I have a problem with "WHERE" condition - I can't find good way to put it into search criteria.
0

#4 User is offline   Ziggi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 14-August 12

Posted 25 January 2013 - 04:57 AM

All right - finally I've got it:

$criteria->compare('`company`.`name`',$this->comp_name, true);
$criteria->with=array('department', 'department.company');

:-)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users