Yii ActiveRecord用法记录备忘
ActiveRecord 使用方法
Example1
in查询
$criteria = new CDbCriteria();
$criteria->select = $select;
$criteria->addCondition(["shop_id='$storeId.'", "on_state=$saleStatus"]);
$criteria->addCondition("goods_id in (". join(",",$goodsIds) . ")");
$model = ARRecord::model();
$cb = static::db()->getSlave()->getCommandBuilder();
$fc = $cb->createFindCommand($model->getTableSchema(), $criteria);
$fc->getText();
return $fc->queryAll();
说明
此方法可以记录ActiveRecord的最终SQL,方便记录SQL执行日志
Example2
查询
$criteria = new CDbCriteria();
$criteria->select = $select;
$criteria->addCondition("shop_id='$storeId'");
if($status == 0 || $status == 1){
$criteria->addCondition("on_state=$status");
}
return ARRecord::model()->findAll($criteria);
Example 3
retry
public static function retry($params, $callback){
for($retry = 0; $retry < 3;$retry++){
try{
if($params instanceof CModel){
return $params->$callback();
}
}catch(CDbException $e){
if (!(strpos($e, "error: 2006") && !strpos($e, "error: 2013"))) {
throw new Exception($e->getMessage(), YOUR_ERROR_CODE);
}
}
}
return false;
}