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 ();
Yii Ar model 查询的更多相关文章
- Yii AR Model CRUD数据库操作
Yii AR很好很强大,但刚开始不知道怎么使用,可以先看下官方文档 官方文档:http://www.yiichina.com/guide/database.ar 下面是我对AR的一些理解 对于一个Mo ...
- YII AR查询方法
ActiveRecord类文档:http://www.yiiframework.com/doc/guide/1.1/en/database.ar 对于一个Model Post 有如下的4中查询方法,返 ...
- Django的model查询操作 与 查询性能优化
Django的model查询操作 与 查询性能优化 1 如何 在做ORM查询时 查看SQl的执行情况 (1) 最底层的 django.db.connection 在 django shell 中使用 ...
- Thinkphp5.0 的使用模型Model查询
Thinkphp5.0 的使用模型Model查询 一.查询多条记录 获取多个数据可以使用:select()方法和all()方法. 示例一:使用all()方法. //(1)筛选条件使用闭包函数 $res ...
- Laravel Model查询结果的3种存储格式内存占用对比
PHP Laravel框架支持Model查询数据后可以有多种方式返回数据,对新手会造成一些困扰,比如数组Model对象.集合.纯数组 今天从内存占用的角度对比一下3种数据返回方式 按数组Model对象 ...
- 使用yii AR 完成单个表的CURD操作
什么是AR(ActiveRecord) Active Record (活动记录,以下简称AR)提供了一个面向对象的接口, 用以访问数据库中的数据.一个 AR 类关联一张数据表, 每个 AR 对象对应表 ...
- YII进行数据查询及类库追踪
一般处理过程: 模型进行数据操作,继承自CActiveRecord (活跃记录) AR数据库向上的封装.AR通过OOP面向对象方式操作数据库.AR须要终于转变为详细的sql语句.通过一个中间类(cri ...
- php yii多表查询
一个Company记录可以对应多个CompanyUser纪录Company表: [['id', 'nature_id', 'scale_id', 'pro_id', 'created_at', 'up ...
- Yii Active Record 查询结果转化成数组
使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- ctrl+鼠标左键监听
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Gym 100917F Find the Length
题目链接:http://codeforces.com/gym/100917/problem/F ---------------------------------------------------- ...
- centos mysql初探 -- 配置、基本操作及问题
目录: centos安装mysql 使用mysql客户端进行简单操作 python2和python3连接mysql mysql导入文件问题 死锁解决办法 windows 7 远程连接 mysql 服务 ...
- 关闭myeclipse中烦人的鼠标划过,自动提示功能
eclipse越来越智能,身为码农的我却越来越伤心.虽然你很智能,但请你提供一些有用的信息给我,不要乱七八槽的,不问青红皂白就塞一大堆提示给我,对不起,哥不需要这些!!! 都知道,使用myeclips ...
- python web自动化测试框架搭建(功能&接口)——通用模块
1.通用模块: config.conf: 公共配置文件,配置报告.日志.截图路径,以及邮件相关配置 [report] reportpath = E:\workspace\WebAutomation\s ...
- Unity 指定参数
构造函数参数初始化 InjectionConstructor IContainer.RegisterType<T, Class>(new InjectionConstructor(&quo ...
- cts-on-gsi测试流程
测试前提: 1.发货user版本 2.selinux:Enable 3.连接ADB,stay awake 4.烧录XXX申请的key 5.外网环境(ipv6) ATV9测试准备(正常准备环境+fast ...
- Aria2+WebUI+caddy搭建私有网盘
Aria2安装 wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/ar ...
- 10: Django + Uwsgi + Nginx 的生产环境部署
1.1 一些重要概念 1.Web协议介绍 Web协议出现顺序: CGI -> FCGI -> WSGI -> uwsgi 1. CGI: 最早的协议 2. FCGI: 比CGI快 ...
- 2、NumPy 数据类型
1.NumPy 数据类型 numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型.下表列举了常用 NumP ...