Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CActiveRecord::find* methods don't respect table alias #202

Closed
qiangxue opened this Issue · 3 comments

6 participants

@qiangxue
Owner

If I set the model's table alias in default scope, that alias is not respected in CActiveRecord find* methods. It does respect the alias if I set the alias in the call to the find method (i.e. - $model->find(array('alias'=>'alias') ).

I noticed that the find* methods are using command builder, and one such method (i.e.- createFindCommand) allows $alias to be passed in. $alias defaults to 't'. If there is no alias set in the passed $criteria variable, builder will set the $criteria alias to $alias (t).

Since the AR table alias also defaults to 't', would it not make sense to modify each call to createFindCommand() (from CActiveRecord) to pass $this->getTableAlias as the $alias parameter?

Does this make sense? Do I need to provide a more concrete example or a patch?

If you decide wontfix, just let me know why.

Thanks :-)

Migrated from http://code.google.com/p/yii/issues/detail?id=2392


earlier comments

intel352 said, at 2011-04-29T20:51:52.000Z:

Patch submitted: http://code.google.com/p/yii/issues/detail?id=2393

alexander.makarow said, at 2011-05-12T22:53:32.000Z:

Can't reproduce. 1. class Adv extends CActiveRecord { public function defaultScope() { return array( 'alias'=>'z', ); } } 2. Adv::model()->findAll(); 3. SELECT * FROM `adv` `z` 4. Adv::model()->findAll('z.id=1'); 5. SELECT * FROM `adv` `z` WHERE z.id=1

intel352 said, at 2011-05-14T02:49:39.000Z:

Sorry, that's my fault, I didn't clarify properly. While you can set the table alias via the defaultScope() array (and that works), if you instead set the table alias using setTableAlias, that value is indeed ignored by finder methods. That's what I'm saying, that the $alias parameter (in the internal calls) should default to $this->getTableAlias() instead of allowing the parameter default of 't'. Examples that show this: $finder=Adv::model(); $finder->setTableAlias('z'); $finder->find(); OR function defaultScope() { $this->setTableAlias('z'); return array(); } ---- This means that also if you're setting the default table alias on init (which I now do in my base model class), it's again ignored. The patch doesn't affect backwards compatibility, and is very simple, non-invasive, etc, so even if there was reasoning against the issues I pose here, it doesn't hurt to apply ;-) Thanks for looking into this, my apologies for not being more clear. Cheers

intel352 said, at 2011-05-16T23:46:00.000Z:

Fresh patch against SVN HEAD. You can close issue 2393 as the patch is in this issue now.

mdomba said, at 2011-06-15T07:26:27.000Z:

Issue 2393 has been merged into this issue.

qiang.xue said, at 2012-01-01T03:36:54.000Z:

set for 1.1.10 milestone

@kidol kidol was assigned
@intel352

subscribing @intel352

@klimov-paul
Collaborator

Related issue #228

@cebe
Owner

Hasn't this just been fixed by #2048?

@samdark samdark closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.