YII ????????? Active Record
???????????? ???????[ 2013/11/12 11:16:42 ] ????????
????Active Record
???????????????????????? PHP ??????в????????? SQL ???????? ???????????????????? Yii DAO
?????????????????
???????????£? ????? db ??????????????? CDbConnection ?????????????? ????????????????? db ???????????????????????? AR ??????????????????? CActiveRecord::getDbConnection()??
???????? AR ??
class Post extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'tbl_post';
// return '{{post}}'; // ????????????????????????.
}
}
???????? AR ??????????????????????
return array(
'import'=>array(
'application.models.*'??
)??
);
????????????????????????????????? AR ??????????·?????? primaryKey()
public function primaryKey()
{
return 'id';
// ????????????????????????????μ?????
// return array('pk1'?? 'pk2');
}
???????????
$post=new Post;
$post->title='sample post';
$post->content='content for the sample post';
$post->create_time=time();
$post->save();
???????? Yii ?? ??????????????????????п????? CWebLogRoute ????????????????????????й??? SQL ???
??????????
// ????????????????????е?????
$post=Post::model()->find($condition??$params);
// ??????????????????????
$post=Post::model()->findByPk($postID??$condition??$params);
// ???????????????????
$post=Post::model()->findByAttributes($attributes??$condition??$params);
// ???????? SQL ?????????е?????
$post=Post::model()->findBySql($sql??$params);
???????????????? $condition ??????????????????
$criteria=new CDbCriteria;
$criteria->select='title'; // ???? 'title' ??
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria); // $params ???????
?????????? CDbCriteria ???????? find ??????????????顣
$post=Post::model()->find(array(
'select'=>'title'??
'condition'=>'postID=:postID'??
'params'=>array(':postID'=>10)??
));
??????find????? ??findAll?? findAllByPk?? findAllByAttributes?? findAllBySql?????? ???????κζ???????????????findAll ??????????????顣??? find ?????find ????????????????????? null??
??????

???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11