Active Record
Relevant data can be clustered so that it can be easily accessed through the original data. For example, customer data is related to order data because a customer may have already stored one or more orders. This relationship is declared appropriately, you can use$customer->orders
The expression accesses the customer's order information. This expression will return the containingOrder Active Record
An array of customer order information for instance.
Declare an association
You must firstActive Record
Define an association in a class to useActive Record
associated data. Simply declare an association method for each need to define an association relationship, as shown below,
class Customer extends ActiveRecord { // ... public function getOrders() { return $this->hasMany(Order::className(), ['customer_id' => 'id']); } } class Order extends ActiveRecord { // ... public function getCustomer() { return $this->hasOne(Customer::className(), ['id' => 'customer_id']); } }
In the above code, weCustomer
The class declares aorders
Associate, and asOrder
A statementcustomer
Related.
Each association method must be named like this:getXyz
. Then we passxyz
(lowercase of the initial letter) Call this association name. Please note that the association name is case sensitive.
When declaring an association, the following information must be specified:
- Corresponding relationship: by calling
hasMany()
orhasOne()
Specified. In the example above, you can easily see such an association statement: A customer can have many orders, and each order has only one customer. - Related
Active Record
Class name: used to specifyhasMany()
orhasOne()
The first parameter of the method. The recommended method is to callXyz::className()
to get the string of the class name so that you can use the IDE's automatic completion and make error detection in the compilation phase take effect. - The associated columns of two sets of data: used to specify the columns related to the two sets of data (
hasOne()
/hasMany()
the second parameter of ). The value of the array is filled in the column of the main data (the associated one is currently declaredActive Record
class is the main data), and the keys of the array need to be filled in the columns of the relevant data.
A simple formula, first attach the primary key of the table, and then the primary key of the table. As in the example above,customer_id
yesOrder
attributes, andid
yesCustomer
attributes. (Translator's note:hasMany()
The second parameter of this array key and value order should not be reversed)
Access associated data
After defining the association relationship, you can access the corresponding association data through the association name. Just like accessing an object defined by an associated method, please see the properties for specific concepts. Therefore, now we can call it an associated property.
// SELECT * FROM `customer` WHERE `id` = 123 $customer = Customer::findOne(123); // SELECT * FROM `order` WHERE `customer_id` = 123 // $orders is an array composed of the Order class$orders = $customer->orders;
Tip: When you passgetter
methodgetXyz()
A statement calledxyz
You can access the associated properties like the propertiesxyz
. Note that this naming is case sensitive.
If usinghasMany()
Declare an association relationship, accessing this association property will return the relatedActive Record
array of instances; if usinghasOne()
Declare an association relationship, accessing this association property will return the relatedActive Record
If no relevant data is found, it will returnnull
。
When you first access the associated property, SQL statements are executed to get the data, as shown in the above example. If the same property is accessed again, the previous result will be returned without the SQL statement being re-executed. To force re-execute SQL statements, you should firstunset
This associated attribute, such as:unset($ customer-> orders)
。
$customer->orders; // Get an array of `Order` objects$customer->getOrders(); // Return an instance of ActiveQuery class
Set an alias
class Order extends ActiveRecord { // ... public function getCustomer() { return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->alias('c'); } }
Related Query
$order = Order::find()->joinWith('customer') ->where(['filter1'=>$filter1, 'filter2'=>$filter2]) ->andWhere(['=', 'c.filter3', $filter3]) ->andWhere(['<=', 'cfilter4', $filter4]) ->one();
This is the article about the introduction to the use of the associated model of the PHP Yii2 framework. For more related PHP Yii2 correlation model content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!