• Yii Ar model 查询


    Ar model 查询
    
    
    参照表:
    CREATE TABLE tbl_user
    (
        id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
        username VARCHAR(128) NOT NULL,
        password VARCHAR(128) NOT NULL,
        email VARCHAR(128) NOT NULL,
        profile TEXT
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
    CREATE TABLE tbl_post
    (
        id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
        title VARCHAR(128) NOT NULL,
        content TEXT NOT NULL,
        tags TEXT,
        status INTEGER NOT NULL,
        create_time INTEGER,
        update_time INTEGER,
        author_id INTEGER NOT NULL,
        CONSTRAINT FK_post_author FOREIGN KEY (author_id)
            REFERENCES tbl_user (id) ON DELETE CASCADE ON UPDATE RESTRICT
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    
    
    $condition = 'postID=:postID';
    $params = array(':postID'=>10);
    
    一、单行所有列查询
    ① $post = Post::model()->find($condition,$params);
    
    ②
    $post = Post::model()->findByPk($postID,$condition,$params);
    
    ③
    $post = Post::model()->findByAttributes($attributes,$condition,$params);
    例如:Post::model()->findByAttributes(array('title'=>'abc'))
    
    ④
    $post = Post::model()->findBySql($sql,$params);
    
    二、单行选择列查询
    ①
    $criteria = new CDbCriteria;
    $criteria->select = 'title';  //现在所需要的列
    $criteria->condition = 'postID=:postID';
    $criteria->params = array(':postID'=>10);
    $post = Post::model()->find($criteria);
    ②
    $post=Post::model()->find(array(
    'select'    => 'title',    
    'condition'    => 'postID=:postID',    
    'params'    => array(':postID'=>10),
    )); 
    
    三、多行所有列查询
    ①
    $post = Post::model()->findAll($condition,$params)
    ②
    $post = Post::model()->findAllByPk($postIDs,$condition,$params); 
    ③
    $post = Post::model()->findAllByAttributes($attributes,$condition,$params); 
    ④
    $post = Post::model()->findAllBySql($sql,$params); 
    
    四、多行选择列(参照与单行选择列查询)
    
    五、查询行数
    $count = Post::model()->count($condition,$params)
    
    六、
    $exists=Post::model()->exists($condition,$params)
    
    七、联合查询
    关系:
    BELONGS_TO: A和B的关系是一对多,那么B属于A 例如:Post属于User
    
    HAS_MANY:A和B之间的关系是一对多,那么A有多个B 例如:User有多个Post
    HAS_ONE: 这是HAS_MANY的一种特殊情况,A至多有一个B  
    MANY_MANY: 这个对应多对多的情况,在AR里会将多对多以BELONGS_TO和HAS_MANY的组合来解释
    例如:
    'VarName'=>array('RelationType','ClassName','ForeignKey', ...additional options)
    class User extends CActiveRecord {
        public function relations() {
            return array(
                'posts'=>array(
                    self::HAS_MANY,
                    'Post',
                    'authorID''order'=>'posts.create_time DESC',
                ),
                'profile'=>array(
                    self::HAS_ONE,
                    'Profile',
                    'ownerID'
                ),
            );
        }
    }
    
    User::model()->with('posts')->findAll();
    User::model()->with(array(‘posts, profile’))->findAll();
    
    八、增改删除
    
    ① 单条记录操作
    $now = time();
    $model = new CBlocks();  
    $model = $model->findByPk($id);(更改)             
    $request = Yii::app()->request;
    $blockName = $request->getPost('block_name');               
    $model->setAttribute('block_name',$blockName); 
    (或者 $model-> block_name = $blockName; 直接调用公共成员变量)
    $model->setAttribute('expired_time', $request->getPost('expired_time'));
    $model->setAttribute('create_time', $now);
    $model->setAttribute('update_time', $now);
    $model->save()
    
    或者:$model->setAttributes($_REQUEST);
    
    修改:CBlocks::model()->updateByPk($pk,$attributes,$condition,$params);
    
    CBlocks()::model()->findByPk($id) ->delete();
    或者:CBlocks::model()->deleteByPk($pk,$condition,$params);
    
    ② 多条记录操作
    
    Post::model()->updateAll($attributes,$condition,$params);
    Post::model()->deleteAll($condition,$params);
    
    
    
    Pdo  model 查询
    
    一、关联查询
    $joinTableName = $nlogNews->tableName();
    $connection = Yii::app()->db->createCommand();  
                    $selectObj = $connection->select("a.contentid,a.title")
                            ->from($this->tableName().' a')
                            ->join("$joinTableName b", "a.contentid = b.contentid")
                            ->where("day = :day", array(':day' => date('Ymd')));
                     
                    if (! empty($catid) ) {
                        $selectObj->andWhere('catid = :catid', array(':catid' => $catid));
                    }
                    
                    $return = $selectObj->order("hit DESC")
                            ->limit($limit)                      
                            ->queryAll();
    单行:
    $selectObj->queryRow ();
    
    单列:
    $selectObj->queryColumn ();
  • 相关阅读:
    Map
    Enumeration输出
    iterator的基本用法
    Annotation整合工厂设计模式
    自定义Annotation
    Annotation
    动态代理设计模式
    静态代理设计模式
    自定义ClassLoader
    获取类的类对象的几种方式
  • 原文地址:https://www.cnblogs.com/kezf/p/Yii_ar_model.html
Copyright © 2020-2023  润新知