一.声明AR类(模型层)
namespace app\models;
use yii\db\ActiveRecord;
class Customer extends ActiveRecord
{
/**
* @return string 返回该AR类关联的数据表名
*/
public static function tableName()
{
return 'customer';
}
}
输出sql:createCommand()->getRawSql();
二.使用AR类 进行增删改查(控制器层)
// "id" 和 "mail" 是 $customer 对象所关联的数据表的对应字段名
$id = $customer->id;
$email = $customer->email;
AR 提供了两种方法来构建 DB 查询并向 AR 实例里填充数据:
yii\db\ActiveRecord::find()
yii\db\ActiveRecord::findBySql()
1.简单查询方法
Customer::find()->one(); 此方法返回一条数据;
Customer::find()->all(); 此方法返回所有数据;
Customer::find()->count(); 此方法返回记录的数量;
Customer::find()->average(); 此方法返回指定列的平均值;
Customer::find()->min(); 此方法返回指定列的最小值 ;
Customer::find()->max(); 此方法返回指定列的最大值 ;
Customer::find()->scalar(); 此方法返回值的第一行第一列的查询结果;
Customer::find()->column(); 此方法返回查询结果中的第一列的值;
Customer::find()->exists(); 此方法返回一个值指示是否包含查询结果的数据行;
Customer::find()->asArray()->one(); 以数组形式返回一条数据;
Customer::find()->asArray()->all(); 以数组形式返回所有数据;
Customer::find()->where($condition)->asArray()->one(); 根据条件以数组形式返回一条数据;
Customer::find()->where($condition)->asArray()->all(); 根据条件以数组形式返回所有数据;
Customer::find()->where($condition)->asArray()->orderBy('id DESC')->all(); 根据条件以数组形式返回所有数据,并根据ID倒序;
2.findOne()和findAll()
// 查询年龄为30,状态值为1的客户
$customer = Customer::findOne(['age' => 30, 'status' => 1]);
$customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
// 查询id值为10,11,12的客户
$customers = Customer::findAll([10, 11, 12]);
$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
3.where()条件
$customers = Customer::find()->where($cond)->all();
$cond写法举例:
// SQL: (type = 1) AND (status = 2).
$cond = ['type' => 1, 'status' => 2]
// SQL:(id IN (1, 2, 3)) AND (status = 2)
$cond = ['id' => [1, 2, 3], 'status' => 2]
//SQL:status IS NULL
$cond = ['status' => null]
[[and]]:将不同的条件组合在一起,用法举例:
//SQL:`id=1 AND id=2`
$cond = ['and', 'id=1', 'id=2']
//SQL:`type=1 AND (id=1 OR id=2)`
$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]
[[or]]:
//SQL:`(type IN (7, 8, 9) OR (id IN (1, 2, 3)))`
$cond = ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]
[[not]]:
//SQL:`NOT (attribute IS NULL)`
$cond = ['not', ['attribute' => null]]
[[between]]: not between 用法相同
//SQL:`id BETWEEN 1 AND 10`
$cond = ['between', 'id', 1, 10]
[[in]]: not in 用法类似
//SQL:`id IN (1, 2, 3)`
$cond = ['in', 'id', [1, 2, 3]]
//IN条件也适用于多字段
$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]
//也适用于内嵌sql语句
$cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
[[like]]:
//SQL:`name LIKE '%tester%'`
$cond = ['like', 'name', 'tester']
//SQL:`name LIKE '%test%' AND name LIKE '%sample%'`
$cond = ['like', 'name', ['test', 'sample']]
//SQL:`name LIKE '%tester'`
$cond = ['like', 'name', '%tester', false]
此外,您可以指定任意运算符如下
//SQL:`id >= 10`
$cond = ['>=', 'id', 10]
//SQL:`id != 10`
$cond = ['!=', 'id', 10]
3.增删改
// 插入新客户的记录
$customer = new Customer();
$customer->name = 'James';
$customer->email = '[email protected]';
$customer->save(); // 等同于 $customer->insert();
// 更新现有客户记录
$customer = Customer::findOne($id);
$customer->email = '[email protected]';
$customer->save(); // 等同于 $customer->update();
// 删除已有客户记录
$customer = Customer::findOne($id);
$customer->delete();
// 删除多个年龄大于20,性别为男(Male)的客户记录
Customer::deleteAll('age > :age AND gender = :gender', [':age' => 20, ':gender' => 'M']);
// 所有客户的age(年龄)字段加1:
Customer::updateAllCounters(['age' => 1]);
4.两表联查
4.两表联查
[[ActiveRecord::hasOne()]]:返回对应关系的单条记录
[[ActiveRecord::hasMany()]]:返回对应关系的多条记录
//客户表Model:CustomerModel
//订单表Model:OrdersModel
//首先要建立表与表之间的关系
//在CustomerModel中添加与订单的关系
模型层:
Class CustomerModel extends \yii\db\ActiveRecord
{
...
public function getOrders()
{
//客户和订单是一对多的关系所以用hasMany
//此处OrdersModel在CustomerModel顶部别忘了加对应的命名空间
//id对应的是OrdersModel的id字段,order_id对应CustomerModel的order_id字段
return $this->hasMany(OrdersModel::className(), ['id'=>'order_id']);
}
}
控制器层:
public function actionIndex{
$customer = Customer::find()->where(['name'=>'zhangsan'])->one();
$orders = $customer->getOrders();
或者
$orders = $customer->orders; // 魔术方法自动补全 getOrders();
//关联查询的多次查询
$customer = Customer::find()->with('order')->all();
foreach($customer as $customer){
$orders = $customer ->orders;
}
}
视图层向控制器层传值
首先在所在的视图层 use yii\helpers\Url;使用
Get方式
其次使用= Url::to(['控制器/方法'])?>?参数=值