yii-besic (七)model 的使用
来源:互联网 发布:自己设计服装软件 编辑:程序博客网 时间:2024/05/16 01:12
首先在config/db.php去配置数据库
创建数据模型model/Test.php
<?php
namespace app/models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord
{
}
?>
在控制器中
use app\models\Test;
在方法中执行原生sql 返回数组,数组中每条数据为对象形式
$sql=’select * from test where id=1’;
Test::findBySql($sql)->all();
可防止SQL注入 通过占位符
$sql=’select * from test where id=:id’;
Test::findBySql($sql,array(‘:id’=>$id))->all();
(一)单标查询
数组查询id=1
Test::find ()->where([‘id’=>1])->all();
数组查询id>0
Test::find ()->where([‘>’,’id’,0])->all();
数组查询id>=1 并且id<=2
Test::find ()->where([‘between’,’id’,1,2])->all();
数组查询title like “%title%”
Test::find ()->where([‘like,’title’,’title’])->all();
更多查询条件请查看官网www.yiichina.com/doc/api/2.0/yii-db-query#where()-detail
数据多的话转换数组
Test::find ()->where([‘like,’title’,’title’])->asArray()->all();
批量查询 batch() 表示一次查几条
foreach(Test::find()->batch(2) as $tests)
{
print_r($tests)
}
(二)单表删除
删除数据1
$res=Test::find ()->where([‘id’=>1])->all();
$res[0]->delete();
删除数据2 占位符
Test::deleteAll(‘id>:id’,array(‘:id’=>0));
(三)单表增加
$test=new Test;
Id/title 为数据库字段名
$test->id=3;
$test->title=’title3’;
$test->save();
入库验证在model层 中写 验证id整型,title字符串型 0-5长度
public function rules(){
return[
[‘id’,’integer’],
[‘title’,’string’,‘length’=>[0,5]]
];
}
更多验证器www.yiichina.com/doc/guide/2.0/tutorial-core-validators
调用方式
$test=new Test;
$test->id=3;
$test->title=’title3’;
$this->validate();
If($this->hasErrors())
{
Echo ‘date is error!’;
Die;
}
$test->save();
(四)单表修改
$res=Test::find ()->where([‘id’=>1])->one();
$res->title=’titles’;
$res->save();
(五)关联查询
首先建立一个model Customer 同名数据库也建立 顾客表
<?php
namespace app/models;
use yii\db\ActiveRecord;
class Customer extendsActiveRecord
{
}
?>
然后建立领一个model Order 同名数据库也建立 订单表
<?php
namespace app/models;
use yii\db\ActiveRecord;
class Order extendsActiveRecord
{
}
?>
在控制器中
<?php
namespace app/controllers;
use yii\db\Controller;
use app\models\Order;
use app\models\ Customer;
class HelloControllerextends actionIndex{
根据顾客表查询订单表
$cus=Customer:;find()->where([‘name’=>’zhangsan’])->one();
Ord_id为order表中的 cus_id为customer的 两个id相互对应
$ord=$cus->hasMany(‘app\models\Order;’,[‘ord_id’=>’cus_id’])->toArray->all();
Order::className() === app\models\Order
hasMany指1对多或多对多的关系 hasOne 指多对一或一对一
}
?>
为了使用model操作数据库
封装model Customer中
Public function getOrders()
{
$this->hasMany(Order::className(),[‘ord_id’=>’cus_id’])->toArray->all();
}
控制器中调用
$ord=$cus->getOrders();
或
$ord=$cus->orders();
会默认调用getOrders()方法但是会在后面追加->all();
所以需要修改getOrders方法 去除->all();
替换原有的
$ord=$cus->hasMany(‘app\models\Order;’,[‘ord_id’=>’cus_id’])->toArray->all();
- yii-besic (七)model 的使用
- yii-besic (四) cookie的使用
- yii-besic (五)视图
- yii-besic (六) 视图布局传值
- Yii的Model ( 模型)创建及使用
- yii的Model ( 模型)创建及使用
- Yii的Model ( 模型)创建及使用
- Yii的Model ( 模型)创建及使用
- YII model使用
- YII model创建和使用
- YII Framework学习教程-YII的Model-数据库操作2-多关系的CActiveRecord使用举例
- YII Framework学习教程-YII的Model-基本规则和使用-2011-11-22
- YII Framework学习教程-YII的Model-数据库操作4-Query Builder使用举例
- YII的Model-数据库操作
- yii mongo model的写法
- UVM的register model介绍(七)
- yii分析 yii的 view语法和model中的relations
- YII的Model-action 和 models
- rocketmq学习笔记 六 流程之拉消息
- gearman的安装与使用示例
- mac下安装软件
- iOS7上leftBarButtonItem无法实现滑动返回的完美解决方案
- 对文件操作的指令
- yii-besic (七)model 的使用
- 数据结构实验之排序七:选课名单
- JPA映射关联关系-双向一对多
- jsp有哪些内置对象,作用分别是什么?
- NOIP2016游记
- Excel快捷键之移动&选择
- ECharts 联动
- html
- 三层架构