Yii Framework Forum: Using 'alias' property with relational data - Yii Framework Forum

Jump to content

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

Using 'alias' property with relational data Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 29 September 2010 - 07:58 AM

I'm using Yii 1.1.3.

I have table Supplier:

id (int)
name (varchar)
town_id (int)
email (varchar)
phone (varchar)
enabled (int)

I also have table Town:

id (int)
name (varchar)
region_id (int)

I have a join from Supplier to Town, so that I can retrieve the region_id of the Supplier.

I represent this data in a CGridView as follows:

public function relations()
{
	return array(
		'supplier_town'=>array(self::BELONGS_TO, 'Town', 'town_id'),
	);
}

public function search()
{
	$criteria=new CDbCriteria;
	$criteria->alias="supplier";
	$criteria->with=array('supplier_town');

	return new CActiveDataProvider(get_class($this), array(
		'criteria'=>$criteria,
		'sort'=>array(
			'defaultOrder'=>'supplier.name',
			'attributes'=>array(
				'region'=>array(
					'asc'=>'region',
					'desc'=>'region DESC',
				),
				'*',
			)
		),
	));
}


I use the 'alias' property as both the tables contain a 'name' field.

As you can see I have specified an 'attributes' element in the sort array so that the relational field 'region' can be sorted on. I have also included the '*' element in the attributes array - according to the docs this ensures that all other model attributes are available for sorting. So far so good, however when I try to sort on any of the other columns I receive a JavaScript alert error, for example:

Column not found: 1054 Unknown column 't.enabled' in 'order clause'

I found that if I remove the alias property the error does not occur, but then of course I get an ambiguous 'name' column.

The only way I can get this to work with the alias is by taking out the star element and creating seperate elements for all the other attributes. This is a workaround but I was wondering why the 'alias' property is causing this problem?
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 30 September 2010 - 02:40 AM

I had this problem too, but always re-wrote all attributes.

Maybe you can ask as feature request CSort will take in accont the eventual alias of the criteria, but I am not sure it is possible.
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,003
  • Joined: 30-October 09
  • Location:UK

Posted 30 September 2010 - 05:18 AM

View Postzaccaria, on 30 September 2010 - 02:40 AM, said:

I had this problem too, but always re-wrote all attributes.

Maybe you can ask as feature request CSort will take in accont the eventual alias of the criteria, but I am not sure it is possible.

Yeh I think there should be an 'alias' property for CSort!
0

#4 User is offline   ouhman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 118
  • Joined: 26-December 08

Posted 27 October 2010 - 04:58 PM

+1 for an alias attribute for CSort.
Is there any other way when using an alias?
0

#5 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,317
  • Joined: 12-October 09
  • Location:Croatia

Posted 29 October 2010 - 02:00 PM

This issue is fixed in r2582 - http://code.google.c...e/detail?r=2582
Find more about me.... btw. Do you know your WAN IP?
0

#6 User is offline   ouhman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 118
  • Joined: 26-December 08

Posted 29 October 2010 - 03:44 PM

brillant thanks guys
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