Home>

You can use activerecord to perform related queries (for example, when reading data from table a and read out the data of related table b), in active record, you can access related data like accessing the properties of the activerecord object of the main table Just as simple.

For example, with the right relationship statement,You can use $customer->orders to get an array of order objects,Orders placed on behalf of this customer.

To declare a relationship, define a getter method that returns a yii \ db \ activequery object with the associated context information,This will query only relevant data that meets the conditions.such as:

class customer extends \ yii \ db \ activerecord
{
 public function getorders ()
 {
  //customer has_many order via order.customer_id->id
  return $this->hasmany (order ::classname (), ["customer_id" =>"id"]);
 }
}
class order extends \ yii \ db \ activerecord
{
 //order has_one customer via customer.id->customer_id
 public function getcustomer ()
 {
  return $this->hasone (customer ::classname (), ["id" =>"customer_id"]);
 }
}

The yii \ db \ activerecord ::hasmany () and yii \ db \ activerecord ::hasone () in the above code are used to model one-to-many and one-to-one associations in relational databases.For example, a customer has multiple order orders, and an order is owned or attributed to a user.Both methods take two parameters and return a yii \ db \ activequery object:

$class:the class name of the associated model.

$link:column association between two tables.This has to be an array.The key of the array element is the column name of the table corresponding to $class.The value of the array element is the column name of the currently declared class.It is good programming practice to define these relationships in terms of table foreign key associations.

After completing the above statement,You can get the associated data like accessing object properties by defining the corresponding getter method:

//get the orders of a customer
$customer=customer ::findone (1);
$orders=$customer->orders;//$orders is an array of order objects

The above code actually executed the following two SQL queries behind the scenes, corresponding to the above two lines of code:

select * from customer where id=1;
select * from order where customer_id=1;

Tip:If you visit $customer->orders again, the above 2nd line sql query will not be executed repeatedly. This query is executed only the first time the expression is accessed.Subsequent accesses will return directly to the internal buffered data.If i want to re-execute the query,Just call unset first to clear the cache:

unset ($customer->orders) ;.

Sometimes, you may want to pass parameters to a relational query to qualify the query.For example, you only want to read large orders that exceed the specified amount,Not all orders.To do this, you can use the following getter method to declare a bigorders relationship:

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");
 }
}

rememberhasmany () The returned object is a yii \ db \ activequery, so the methods of activequery can be used to customize this related query.

With the above statement,If you visit $customer->bigorders, it will only return orders with an amount greater than 100. If i want to specify a different limit value,Use the following code:

$orders=$customer->getbigorders (200)->all ();

note:The association method returns a yii \ db \ activequery instance. If you access it as a property,The returned data is a yii \ db \ activerecord instance, or the activerecord array is null. For example, $customer->getorders () returns an activequery instance, while $customer->orders returns an array of order objects (or an empty array,If the query result is empty).

Intermediate table related query

Sometimes, some data tables are related through a pivot table.To declare such a relationship,We can customize the yii \ db \ activequery object by calling its via () or viatable () method.

For example, if the order table order and the item table item are related through the join table order_item, we can declare the items relationship in the order class as follows:

class order extends \ yii \ db \ activerecord
{
 public function getitems ()
 {
  return $this->hasmany (item ::classname (), ["id" =>"item_id"])
   ->Viatable ("order_item", ["order_id" =>"id"]);
 }
}

The via () method is similar to viatable (), but the first parameter is a relation name declared in the current activerecord class, not the name of the intermediate table.For example, the above items relationship can also be declared in the following way:

class order extends \ yii \ db \ activerecord
{
 public function getorderitems ()
 {
  return $this->hasmany (orderitem ::classname (), ["order_id" =>"id"]);
 }
 public function getitems ()
 {
  return $this->hasmany (item ::classname (), ["id" =>"item_id"])
   ->Via ("orderitems");
 }
}
php
  • Previous Complete Bootstrap plugin
  • Next Summary of methods for obtaining various height, width and position of web pages in JS