您好,欢迎访问一九零五行业门户网

yii2怎么查询数据

数据查询
user::find()->all(); 此方法返回所有数据;user::findone($id); 此方法返回 主键 id=1 的一条数据(举个例子); user::find()->where(['name' => '小伙儿'])->one(); 此方法返回 ['name' => '小伙儿'] 的一条数据;user::find()->where(['name' => '小伙儿'])->all(); 此方法返回 ['name' => '小伙儿'] 的所有数据;user::find()->orderby('id desc')->all(); 此方法是排序查询;user::findbysql('select * from user')->all(); 此方法是用 sql 语句查询 user 表里面的所有数据;user::findbysql('select * from user')->one(); 此方法是用 sql 语句查询 user 表里面的一条数据;user::find()->andwhere(['sex' => '男', 'age' => '24'])->count('id'); 统计符合条件的总条数;user::find()->one(); 此方法返回一条数据;user::find()->all(); 此方法返回所有数据;user::find()->count(); 此方法返回记录的数量;user::find()->average(); 此方法返回指定列的平均值;user::find()->min(); 此方法返回指定列的最小值 ;user::find()->max(); 此方法返回指定列的最大值 ;user::find()->scalar(); 此方法返回值的第一行第一列的查询结果;user::find()->column(); 此方法返回查询结果中的第一列的值;user::find()->exists(); 此方法返回一个值指示是否包含查询结果的数据行;user::find()->batch(10); 每次取 10 条数据 user::find()->each(10); 每次取 10 条数据, 迭代查询
多表查询:
/* 多表联查 */$model=new customer();$model->fiind()->join(‘left join‘,‘student‘,‘student.cid=customer.id‘) ->where(‘student.id‘=>\yii::$app->user->id) ->andwhere(‘is_ok=1‘) ->one()
关联查询
使用 ar 方法也可以查询数据表的关联数据(如,选出表a的数据可以拉出表b的关联数据)。 有了 ar, 返回的关联数据连接就像连接关联主表的 ar 对象的属性一样。
建立关联关系后,通过 $customer->orders 可以获取 一个 order 对象的数组,该数组代表当前客户对象的订单集。
定义关联关系使用一个可以返回 [[yii\db\activequery]] 对象的 getter 方法, [[yii\db\activequery]]对象有关联上下文的相关信息,因此可以只查询关联数据。
class customer extends \yii\db\activerecord{ public function getorders() { // 客户和订单通过 order.customer_id -> id 关联建立一对多关系 return $this->hasmany(order::classname(), ['customer_id' => 'id']); }} class order extends \yii\db\activerecord{ // 订单和客户通过 customer.id -> customer_id 关联建立一对一关系 public function getcustomer() { return $this->hasone(customer::classname(), ['id' => 'customer_id']); }}
以上使用了[[yii\db\activerecord::hasmany()]] 和 [[yii\db\activerecord::hasone()]]方法。以上两例分别是关联数据多对一关系和一对一关系的建模范例。如,一个客户有很多订单,一个订单只归属一个客户。两个方法都有两个参数并返回[[yii\db\activequery]]对象。
建立关联关系后,获取关联数据和获取组件属性一样简单, 执行以下相应getter方法即可:
// 取得客户的订单$customer = customer::findone(1);$orders = $customer->orders; // $orders 是 order 对象数组
以上代码实际执行了以下两条 sql 语句:
select * from customer where id=1;select * from order where customer_id=1;
有时候需要在关联查询中传递参数,如不需要返回客户全部订单, 只需要返回购买金额超过设定值的大订单, 通过以下getter方法声明一个关联数据 bigorders:
class customer extends \yii\db\activerecord{ public function getbigorders($threshold = 100) { return $this->hasmany(order::classname(), ['customer_id' => 'id']) ->where('subtotal > :threshold', [':threshold' => $threshold]) ->orderby('id'); }}
联合查询
使用关系数据库时,普遍要做的是连接多个表并明确地运用各种 join 查询。 join sql语句的查询条件和参数,使用 [[yii\db\activequery::joinwith()]] 可以重用已定义关系并调用 而不是使用 [[yii\db\activequery::join()]] 来实现目标。
// 查找所有订单并以客户 id 和订单 id 排序,并贪婪加载 "customer" 表$orders = order::find()->joinwith('customer')->orderby('customer.id, order.id')->all();// 查找包括书籍的所有订单,并以 `inner join` 的连接方式即时加载 "books" 表$orders = order::find()->innerjoinwith('books')->all();
以上方法[[yii\db\activequery::innerjoinwith()|innerjoinwith()]]是访问inner join类型的[[yii\db\activequery::joinwith()
|joinwith()]]的快捷方式。
可以连接一个或多个关联关系,可以自由使用查询条件到关联查询, 也可以嵌套连接关联查询。如:
// 连接多重关系// 找出24小时内注册客户包含书籍的订单$orders = order::find()->innerjoinwith([ 'books', 'customer' => function ($query) { $query->where('customer.created_at > ' . (time() - 24 * 3600)); }])->all();// 连接嵌套关系:连接 books 表及其 author 列$orders = order::find()->joinwith('books.author')->all();
代码背后, yii 先执行一条 join sql 语句把满足 join sql 语句查询条件的主要模型查出, 然后为每个关系执行一条查询语句, bing填充相应的关联记录。
[[yii\db\activequery::joinwith()|joinwith()]] 和 [[yii\db\activequery::with()|with()]] 的区别是 前者连接主模型类和关联模型类的数据表来检索主模型, 而后者只查询和检索主模型类。 检索主模型
由于这个区别,你可以应用只针对一条 join sql 语句起效的查询条件。 如,通过关联模型的查询条件过滤主模型,如前例, 可以使用关联表的列来挑选主模型数据,
当使用 [[yii\db\activequery::joinwith()|joinwith()]] 方法时可以响应没有歧义的列名。 in the above examples, we useitem.id and order.id to disambiguate the id column references 因为订单表和项目表都包括 id 列。
当连接关联关系时,关联关系默认使用即时加载。你可以 通过传参数 $eagerloading 来决定在指定关联查询中是否使用即时加载。
默认 [[yii\db\activequery::joinwith()|joinwith()]] 使用左连接来连接关联表。 你也可以传 $jointype 参数来定制连接类型。 你也可以使用 [[yii\db\activequery::innerjoinwith()|innerjoinwith()]]。
yii2 分页
控制器 commentcontroller 里面的任意一个方法,在这里我的方法是 actioncomment();
use yii\data\pagination;use app\models\comment; public function actioncomment(){ $data = comment::find()->andwhere(['id' => '10']); $pages = new pagination(['totalcount' =>$data->count(), 'pagesize' => '2']); $model = $data->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('comment',[ 'model' => $model, 'pages' => $pages, ]); }
view 里面的代码
<?phpuse yii\widgets\linkpager;?> foreach($model as $key=>$val) { 这里就是遍历数据 } <?= linkpager::widget(['pagination' => $pages]); ?>
in() 操作
select * from `categ_price` where `id` in (9, 11)
$categ_price_id=[9>1,11=>3] $categpricemodel= \common\models\categprice::find()->where(['id' =>array_keys($categ_price_id)])->all(); #>where(['id' => [1, 2, 3]])
not in() 操作
select * from `shop` where (status=1) and (`id` not in (88, 93))
$shopmodel= shop::find()->where('status=1')->andwhere(['not in','id',[88,98]])->all();
,有大量免费的yii入门教程,欢迎大家学习!
以上就是yii2怎么查询数据的详细内容。
其它类似信息

推荐信息