New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getTableAlias() in defaultScope() broken in 1.1.11 #1072
Comments
Another possible temporary workaround: <?php
public function defaultScope()
{
$ta=$this->getTableAlias(false,false);
return array(
'alias'=>$ta,
// ... additional criteria stuff here
);
} |
@MonkeyMaster can you provide code for models involved? Thanks. |
@MonkeyMaster and check bug with latest code from GitHub. |
@samdark @creocoder 1.1.10: latest code: |
@MonkeyMaster Please provide full SQL query and AcCraft::defaultScope() dump. Need to ensure that all ok there. |
@samdark <?php
class Airline extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'test_airline';
}
}
class Craft extends CActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'test_craft';
}
public function relations()
{
return array(
'airline_r' => array(self::BELONGS_TO, 'Airline', 'airline'),
);
}
public function defaultScope()
{
return array(
'condition' => $this->dbConnection->quoteColumnName($this->getTableAlias(false, false).'.company').'=2',
);
}
}
class GroundActivity extends CActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'test_groundactivity';
}
public function relations()
{
return array(
'craft_r' => array(self::BELONGS_TO, 'Craft', 'craft'),
);
}
}
class TestController extends Controller
{
public function actionIndex()
{
Craft::model()->with('airline_r')->findAll();
GroundActivity::model()->with('craft_r')->findAll();
}
} CREATE TABLE `test_airline` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
CREATE TABLE `test_craft` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company` int(11) DEFAULT NULL,
`airline` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `test_groundactivity` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`craft` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
); 1.1.10 SELECT `t`.`id` AS `t0_c0`, `t`.`craft` AS `t0_c1`, `craft_r`.`id` AS `t1_c0`, `craft_r`.`company` AS `t1_c1`, `craft_r`.`airline` AS `t1_c2`
FROM `test_groundactivity` `t`
LEFT OUTER JOIN `test_craft` `craft_r` ON (`t`.`craft`=`craft_r`.`id`) AND (`craft_r`.`company`=2) 1.1.11 (Column not found: 1054 Unknown column 't.company' in 'on clause'): SELECT `t`.`id` AS `t0_c0`, `t`.`craft` AS `t0_c1`, `craft_r`.`id` AS `t1_c0`, `craft_r`.`company` AS `t1_c1`, `craft_r`.`airline` AS `t1_c2`
FROM `test_groundactivity` `t`
LEFT OUTER JOIN `test_craft` `craft_r` ON (`t`.`craft`=`craft_r`.`id`) AND (`t`.`company`=2) Latest code from GitHub: SELECT `t`.`id` AS `t0_c0`, `t`.`craft` AS `t0_c1`, `craft_r`.`id` AS `t1_c0`, `craft_r`.`company` AS `t1_c1`, `craft_r`.`airline` AS `t1_c2`
FROM `test_groundactivity` `t`
LEFT OUTER JOIN `test_craft` `craft_r` ON (`t`.`craft`=`craft_r`.`id`) |
Since everything is fine now, closing issue. @creocoder, @MonkeyMaster thanks. |
@samdark isn't something missing here? If the default scope is limiting the |
Sorry, I copied just CActiveFinder.php when testing the latest build and missed changes in CActiveRecord.php. |
this is to address this issue: yiisoft#1072 what do you guys think?
This is related to #1043
I use $this->getTableAlias(false, false) in model's defaultScope() to avoid ambiguous column errors. It was working fine till YII 1.1.11. Now I get "Unknown column in on clause" sql errors due to wrong alias in defaultScope(). Example:
The first find statement creates AcCraft's dbCriteria with default tableAlias t.
The second find statement ignores relation's alias "craft_r" and still uses "t"!
I found temporary solution: removing model's dbCriteria between these two statements forces second statement to re-create model's dbCriteria with right alias:
Anyway this looks like a bug to me
The text was updated successfully, but these errors were encountered: