This article describes the yii framework database association query operation. Share it for your reference, as follows:
<?php namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //Check all order information based on customer name public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->hasMany('app\models\Order',['customer_id'=>'id'])->asArray()->all(); print_r($orders); } } ?>
The above controller method query, the Customer model has no specific method.
The aboveapp\models\Order Can be improved toOrder::className()
, and add it aboveuse app\models\Order;
Method 2: (Use model method)
Customer model code:
<?php namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public function getOrders(){ return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray()->all(); } }
Controller code:
namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //Check all order information based on customer name public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->getOrders(); print_r($orders); } }
Method 3: (Calling the model's attribute query)
Customer model code:
namespace app\models; use yii\db\ActiveRecord; class Customer extends ActiveRecord{ public function getOrders(){ return $this->hasMany(Order::className(),['customer_id'=>'id'])->asArray(); } }
Controller code:
namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerController extends Controller{ //Check all order information based on customer name public function actionIndex(){ $customer = Customer::find()->where(['name'=>'zhangsan'])->one(); $orders = $customer->orders; //Instructions: When a non-existent property is called, //php will call a __get() method. //The method of __get() will automatically call a get+ attribute method, i.e. getOrders() //And the ->all() or ->one() method will be automatically added when querying, and the hasMany or hasOne decisions based on the hasMany or hasOne query of the model print_r($orders); } }
Obtain the corresponding customer information based on the order id:
Model code:
namespace app\models; use yii\db\ActiveRecord; class Order extends ActiveRecord{ //Get customer information based on order id public function getCustomer(){ return $this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray(); } }
Controller code:
namespace app\controllers; use yii\web\Controller; use app\models\Order; class CustomerController extends Controller{ //Check user information based on order public function actionIndex(){ $orders = Order::find()->where(['id'=>2])->one(); $customer = $orders->customer; print_r($customer); } }
In the above code$orders->customer
The cache will be recorded. If you want to delete the cache, you can use itunset($orders->customer)
。
Multiple queries for associated queries
$customers = Customer::find()->all(); foreach($customers as $customer){ $orders = $customer->orders; }
In this way, if there are 100 pieces of data, a total of 101 queries are required.
optimization:
$customers = Customer::find()->with('orders')->all(); foreach($customers as $customer){ $orders = $customer->orders; }
Queryed twice in total.
For more information about Yii, readers who are interested in this site can view the topic:Yii framework introduction and common techniques summary》、《Summary of excellent development framework for php》、《Basic tutorial on getting started with smarty templates》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Yii framework.