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 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...
随机推荐
- FastDFS介绍(一)
1.简介 FastDFS对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载.文件删除)等,解决了大容量文件存储的问题,特别适合以文件为载体的在线服务,如相册网站.文档网站.图片 ...
- 用soapUI开发webservice接口
1,下载soapUI软件,安装到本地 2,打开soapUI软件 3,创建一个开发好的接口 4,进行接口调用 测试:
- C#防止窗口重复打开
修改Program.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Windo ...
- C#在WinForm开发中Label换行方法
很多朋友都会在开发WinForm中遇到Label要显示的内容太长,但却不能换行的问题.这里我总结了几种方法,供大家参考. 第一种是把Label的AutoSize属性设为False,手动修改Label的 ...
- CodeForces - 35D
题目:https://vjudge.net/contest/326867#problem/A 题意:有一个农场,自己有m斤粮食,有n天,每天动物吃的量不同,那个动物的食量的是由他是从那天开始进这个农场 ...
- 学习日记14、EF 时间段查询
m_Rep.GetList(a => System.Data.Entity.DbFunctions.DiffDays(DateTime.Now, a.EndDate) < date); 命 ...
- php用户签到,领取红包
<?php /** * create by jxkshu * Date 2017-09-28 * 用户签到领取红包 */ /** Redis set: key signed:user:mark ...
- PyTestReport使用
PyTestReport详细介绍: https://testerhome.com/opensource_projects/78 示例代码 #coding:utf-8 import os,unittes ...
- time in china
https://www.timeanddate.com/worldclock/china
- Vue作用域插槽:用作循环结构的模版
一 项目结构 二 App组件 <template> <div id="app"> <!-- 子组件 --> <todos :list=&q ...