頁面輸出list信息
return $this->render('index', [
'model' => $model,
'cats' => $cats,
'findlist' => $findall,
]);
<?php
$str='';
foreach ($findlist as $k => $list) {
$str= $list['name'].$list['label_img'];
?>
<?= Html::encode($str)?><br>
<?php
}
?>
效果:
333
333
任溶溶
444
上傳插件/image/20171125/1511618863693242.jpg
上傳插件/image/20171125/1511619675659806.jpg
基礎查詢
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()->batch(10); 每次取10條數據
Customer::find()->each(10); 每次取10條數據,迭代查詢
//根據sql語句查詢:查詢name=test的客戶
Customer::model()->findAllBySql("select * from customer where name = test");
//根據主鍵查詢:查詢主鍵值為1的數據
Customer::model()->findByPk(1);
//根據條件查詢(該方法是根據條件查詢一個集合,可以是多個條件,把條件放到數組里面)
Customer::model()->findAllByAttributes(['username'=>'admin']);
//子查詢
$subQuery = (new Query())->select('COUNT(*)')->from('customer');
// SELECT `id`, (SELECT COUNT(*) FROM `customer`) AS `count` FROM `customer`
$query = (new Query())->select(['id', 'count' => $subQuery])->from('customer');
//關聯查詢:查詢客戶表(customer)關聯訂單表(orders),條件是status=1,客戶id為1,從查詢結果的第5條開始,查詢10條數據
$data = (new Query())
->select('*')
->from('customer')
->join('LEFT JOIN','orders','customer.id = orders.customer_id')
->where(['status'=>'1','customer.id'=>'1'])
->offset(5)
->limit(10)
->all()
直接查詢
//createCommand(執行原生的SQL語句)
$sql= "SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id";
$rows=Yii::$app->db->createCommand($sql)->query();
查詢返回多行:
$command = Yii::$app->db->createCommand('SELECT * FROM post');
$posts = $command->queryAll();
返回單行
$command = Yii::$app->db->createCommand('SELECT * FROM post WHERE id=1');
$post = $command->queryOne();
查詢多行單值:
$command = Yii::$app->db->createCommand('SELECT title FROM post');
$titles = $command->queryColumn();
查詢標量值/計算值:
$command = Yii::$app->db->createCommand('SELECT COUNT(*) FROM post');
$postCount = $command->queryScalar();
關聯查詢
/**
*客戶表Model:CustomerModel
*訂單表Model:OrdersModel
*國家表Model:CountrysModel
*首先要建立表與表之間的關系
*在CustomerModel中添加與訂單的關系
*/
Class CustomerModel extends \yii\db\ActiveRecord
{
...
//客戶和訂單是一對多的關系所以用hasMany
//此處OrdersModel在CustomerModel頂部別忘了加對應的命名空間
//id對應的是OrdersModel的id字段,order_id對應CustomerModel的order_id字段
public function getOrders()
{
return $this->hasMany(OrdersModel::className(), ['id'=>'order_id']);
}
//客戶和國家是一對一的關系所以用hasOne
public function getCountry()
{
return $this->hasOne(CountrysModel::className(), ['id'=>'Country_id']);
}
....
}
// 查詢客戶與他們的訂單和國家
CustomerModel::find()->with('orders', 'country')->all();
// 查詢客戶與他們的訂單和訂單的發貨地址(注:orders 與 address都是關聯關系)
CustomerModel::find()->with('orders.address')->all();
// 查詢客戶與他們的國家和狀態為1的訂單
CustomerModel::find()->with([
'orders' => function ($query) {
$query->andWhere('status = 1');
},
'country',
])->all();
