Yii2 in multi-table associated with the query

 

 

Preparation conditions:

1, first prepare two tables:

customer (user table) (id, name)

order (order form) (id, customer_id, price)

One to many relationship between a customer table and the order table, by customer_id field associated.

2, establish the appropriate model file customer.php and order.php file.

 

Associated with the query:

customer.php files are added getOrder () method:

? < PHP 
 
namespace App \ Models; 
 
use Yii; 
 
/ * * 
 * This IS the Table at The Model class for "the Customer." 
 * 
 * The @Property the above mentioned id int $ 
 * $ String name the @Property user name 
 * / 
class the Customer the extends \ Yii \ db \ ActiveRecord 
{ 
    / * * 
     * to obtain the order information 
     * / 
    public  function getOrder () 
    { 
        // a user corresponding to multiple orders, many relationship using a hasMany () associated 
        return  $ the this -> hasMany (the Order :: className () , [ 'CUSTOMER_ID' => 'ID']) -> asArray (); 
    } 
 
}

order.php files are added getCustomer () method:

<? PHP 
 
namespace App \ Models; 
 
use Yii; 
 
/ * * 
 . * This IS the Table at The Model class for "the Order" 
 * 
 * $ the @Property the above mentioned id int 
 * int $ CUSTOMER_ID the @Property user the above mentioned id 
 * $. Price price the @Property String 
 * / 
class the Order the extends \ Yii \ db \ ActiveRecord 
{ 
    / * * 
     * obtain user information 
     * / 
    public  function getCustomer () 
    { 
        // an order corresponding to a user, using a one to one relationship hasOne () associated 
        return  $ the this -> hasOne ( :: className the Customer (), [ 'ID' => 'CUSTOMER_ID']) -> asArray (); 
    } 
}

 

use:

// query the customer order information, called Joe Smith 
$ the Customer = :: the Customer the Find () -> the WHERE ([ 'name' => 'Joe Smith']) -> One ();
 // by $ customer-> getOrder () method call customer.php model getOrder () method 
$ the Orders = $ the Customer -> getOrder () -> All ();
 // You can also use $ customer-> order property call, 
// order when the program is not found when property, PHP will call __get () function, the program will automatically find getOrder () method call 
// here do not add all (), the program will automatically identify, if you are using the hasMany plus all (), hasOne the plus One () 
$ the orders = $ the customer -> the Order;
 var_dump ( $ the orders ); Exit ; 
 
// query id for the customer order information 1 
$ the Order = the Order :: the Find () -> the WHERE ([ 'id' => 1 ]) ->one();
// call order.php model getCustomer () method 
$ the Customer = $ the Order -> the Customer;
 var_dump ( $ the Customer ); Exit ;

 

and joinWith with the use of:

One problem is the large amount of data when the performance problems associated with the use of the above queries.

the Customers $ = the Customer :: Find () -> All ();    // corresponds to Customer performs select * from 
the foreach ( $ the Customers  AS  $ Customer ) {
     $ Orders = $ Customer -> Order;   // corresponding to perform select * from WHERE CUSTOMER_ID ID = Order; 
}

If there are 100 data $ customers, the inquiry will have to cycle 100 times, the entire program needs to execute SQL statements 101 times.

Then you can use with ():

// equivalent to the implementation of two SQL statements * from the Customer the SELECT 
// the SELECT * from the Order in the WHERE CUSTOMER_ID (...) 
$ the Customers = :: the Customer the Find () -> with ( 'the Order') -> asArray () -> All ();
 the foreach ( $ the Customers  AS  $ Customer ) {
     $ Orders = $ Customer [ 'order'];   // acquired order data table associated 
}

joinWith () usage and with () almost, joinWith () to specify the type of connection, the default connection LEFT JOIN.

 

important point:

1, hasMany defined in the model, when hasOne method, it is best not add all (), one (). Calls $ customer-> order when the program will automatically based on using a hasMany or hasOne with the corresponding all (), one ().

2, when used with (), joinWith () query, added all the time if you define hasMany, hasOne method in the model (), one (), program error.

 

Guess you like

Origin www.cnblogs.com/woods1815/p/11521643.html