Yii Framework Forum: defaultScope() + CDbCriteria() = ambiguous columns - Yii Framework Forum

Jump to content

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

defaultScope() + CDbCriteria() = ambiguous columns Rate Topic: -----

#1 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 25 May 2011 - 12:36 PM

I've got a defaultScope() for each model so that only the users rows are returned, eg:

public function defaultScope()
{         
	if (Yii::app()->user->isAdmin())
	{
		// Admins can see all records
		return array(
			'condition'=>'(TRUE)',
		);
	}
	else
	{
		// Only list the users own records
		return array(
			'condition'=>'(user_id='.Yii::app()->user->id.')',
		);     
	}
}


But if I use CDbCriteria() I get an error about user_id being an ambigious column.

Is there a way to prefix the user_id in the condition above with the alias depending on how its being called?

For example, if its being called by itself it should be 't.user_id' but if its called by a CDbCriteria() and with() then it should be the name of the table in with, eg 'withtablename.user_id'

Thanks, Russ
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 25 May 2011 - 01:13 PM

Try this:

public function defaultScope()
{
    return array(
        'alias'=>'someAlias',
        'condition'=>'someAlias.user_id=...',
    );
}

2

#3 User is offline   phtamas 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 529
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 25 May 2011 - 01:32 PM

*
POPULAR

Another possible solution:

'condition' => $this->getTableAlias(false, false) . '.user_id = ...',


7

#4 User is offline   Russell England 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 40
  • Joined: 10-March 11
  • Location:Stourbridge, UK

Posted 26 May 2011 - 08:51 AM

Brilliant! Thank you :)
0

#5 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 12 July 2013 - 06:31 AM

View Postphtamas, on 25 May 2011 - 01:32 PM, said:

Another possible solution:

'condition' => $this->getTableAlias(false, false) . '.user_id = ...',



Thanks a lot phtamas, you safe me from a lot of work! :)
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#6 User is offline   rahulviswanath 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 25-April 13
  • Location:Ernakulam

Posted 29 August 2014 - 12:27 AM

excellent work phtamas... :)
0

#7 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted Today, 12:32 AM

View Postphtamas, on 25 May 2011 - 01:32 PM, said:

Another possible solution:

'condition' => $this->getTableAlias(false, false) . '.user_id = ...',



Excellent phtamas !! :)
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
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