AR->findAll bug when condition contains 'with'


(Humandong) #1



//Yii1.1 Stable,CActiveRecord line 1185:

	public function findAll($condition='',$params=array())

	{

		if($condition instanceof CDbCriteria && !empty($condition->with) || is_array($condition) && isset($condition['with']))

			return $this->with($condition->with)->findAll($condition);

		Yii::trace(get_class($this).'.findAll()','system.db.ar.CActiveRecord');

		$criteria=$this->getCommandBuilder()->createCriteria($condition,$params);

		return $this->query($criteria,true);

	}



if we define option ‘with’ in a condition like :


$models=Member::model()->findAll(array(

            'with'=>array('sales'),

            'limit'=>10,

            'offset'=>1,

        ));




//then this line in AR will be executed:

return $this->with($condition->with)->findAll($condition);

//                 ~~~~~~~~~~~~~~~~It's null because we should use $condition['with']



This bug will disable eager loading when we define with option directly in condition.

the trace log is :

2010/01/12 22:24:54 [trace] [system.db.ar.CActiveRecord] [color="#FF0000"]Member.findAll() eagerly[/color]

2010/01/12 22:24:54 [trace] [system.db.CDbCommand] Querying SQL: SELECT t.mid AS t0_c0, t.name AS t0_c1, t.password AS t0_c2, t.data AS t0_c3, t.expire_time AS t0_c4, t.del_flag AS t0_c5, t.email AS t0_c6, t.sales_id AS t0_c7, t.remark AS t0_c8, t.last_login_time AS t0_c9, t.login_times AS t0_c10, t.created AS t0_c11, t.modified AS t0_c12 FROM member t LIMIT 10 OFFSET 1

2010/01/12 22:24:54 [trace] [system.db.ar.CActiveRecord] [color="#FF0000"]lazy loading[/color] Member.sales

2010/01/12 22:24:54 [trace] [system.db.CDbCommand] Querying SQL: SELECT sales.mid AS t1_c0, sales.name AS t1_c1, sales.password AS t1_c2, sales.data AS t1_c3, sales.expire_time AS t1_c4, sales.del_flag AS t1_c5, sales.email AS t1_c6, sales.sales_id AS t1_c7, sales.remark AS t1_c8, sales.last_login_time AS t1_c9, sales.login_times AS t1_c10, sales.created AS t1_c11, sales.modified AS t1_c12 FROM member sales WHERE (sales.mid=:ypl0)


(Humandong) #2

Yii1.1稳定版怎么会有这样的bug??


(Qiang Xue) #3

Thank you. I have fixed this bug in SVN.