Yii Framework Forum: Turning Custom Mysql Function Alias Into Model Variable - Yii Framework Forum

Jump to content

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

Turning Custom Mysql Function Alias Into Model Variable Rate Topic: -----

#1 User is offline   Ocean Wind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 14-September 10
  • Location:Denver, Colorado, USA

Posted 10 January 2013 - 10:00 PM

I've got the following table set up for a MANY::MANY relation:

category > category_listing > listing


I've create a custom MySQL function to speed up GeoIP calculations, which I'm calling using CDbExpression in a parameterized scope in the category_listing model and assigning the alias 'distance':

public function withinRadius($radius)
	{
		...
		
    	$this->getDbCriteria()->mergeWith(array(
			'select'=>array(new CDbExpression("fn_distance_cosine({$user->lat},{$user->lng},listing.lat,listing.lng) as distance")),
			'with'=>'listing',
			'order'=>'distance ASC',
			'condition'=>"listing.lat BETWEEN {$min_lat} AND {$max_lat} AND listing.lng BETWEEN {$min_lng} AND {$max_lng}",
			'having'=>"distance <= {$radius}",
		));
			
		return $this;
	}


I've defined distance as a variable within the category_listing model:

public $distance;


And now I can call the following:

$categoryListings = EldCategoryListing::model()->withinRadius(25)->findAllByAttributes(array('category_id'=>$id));


But the problem is, I can't access $distance as a variable. $categoryListings[0]->distance is not set. I've also tried defining $distance as an attribute of listing, and calling $categoryListings[0]->listing->distance, and this doesn't work.

What am I doing wrong?
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 677
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 11 January 2013 - 04:55 AM

I guess you have to use

EldCategoryListing::model()->distance 


to get hold of that property
0

#3 User is offline   Ocean Wind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 14-September 10
  • Location:Denver, Colorado, USA

Posted 11 January 2013 - 03:29 PM

View Postalirz23, on 11 January 2013 - 04:55 AM, said:

I guess you have to use

EldCategoryListing::model()->distance 


to get hold of that property


Strangely, $categoryListings[0]->distance seems to be working now. I'm guessing there was some caching at play that was preventing my code updates from displaying.

Apologies for the false alarm.
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