Eloquent ORM's associated query

Source: Internet
Author: User

  Small friends, this article is in my previous essay on the basis of completion, have not browsed the classmate, please move the eloquent ORM learning notes.

The previous article used the Leftjoin method, in fact eloquent for the correlation between the module query has its own encapsulation, then we will study together eloquent how the association is applied.

  

1. Create Models

The previous article created the Usermodel, in fact, for Users_ext can also create a Model:UserExtModel.php  

 <? php  class  Userextmodel extends   \eloquent { protected   $table  = ' users_ext '  protected   $primaryKey  = ' iautoid    ' ;  public  function   user () { return   $this ->belon    Gsto (' Usermodel ', ' Iuserid ', ' iautoid '  '); }}

  2. Establishing an association

Modify usermodel.php, add a one-to-one association to the file, and Users_ext.

1<?PHP2 3 classUsermodelextends\eloquent {4 5     protected $table= ' users ';6     protected $primaryKey= ' Iautoid ';7     protected $connection= ' user ';8 9      Public functionUserext () {Ten         return $this->hasone (' Userextmodel ', ' Iuserid ', ' iautoid '); One     } A}

  3. Related queries

(1) One-to-one association:

The above two models use the Hasone () and Belongsto () method to establish the association model is a "one-to-one" model, can now make a simple query:  

1<?PHP2 3 classHomeControllerextendsBasecontroller {4 5      Public functiongetusers () {6         $resData= Usermodel::find (1)->userext->ToArray ();7         Var_dump($resData);8         Exit();9     }Ten}

  The above query is equivalent to the SQL statement:

1 SELECT 2     Users_ext.*3from4    users5leftJOIN on=  users_ext.iuserid6WHERE7       =1

  

  (2) One-to-many associations:

Change Hasone () in the Usermodel model to Hasmany() so that the "one-to-many" association model is established, and you can now do the following simple query:  

1<?PHP2 3 classHomeControllerextendsBasecontroller {4 5      Public functiongetusers () {6         $resData= Usermodel::find (1)->userext ()->where (' SSex ', ' = ', 1)->get ()ToArray ();7         Var_dump($resData);8         Exit();9     }Ten}

  

  The above query is equivalent to the SQL statement:  

1 SELECT2Users_ext.*3  from4 Users5  Left JOINUsers_ext onUsers.iautoid=Users_ext.iuserid6 WHERE7Users.iautoid= 18  andUsers_ext.ssex= 1

  

  (3) Many-to-many associations:

This model is relatively complex and is illustrated by the relationship between the user and the role. You need to create another two tables of roles and user_role, and initialize them:  

1 CREATE TABLE2 IF  not EXISTSRoles (3IautoidINT( One) Auto_increment,4SrolenameVARCHAR( -),5     PRIMARY KEY(iautoid)6) ENGINE=INNODBDEFAULTCHARSET=UTF8 auto_increment= 1;7 8 INSERT  intoroles (srolename)9 VALUESTen('Admin'), One('Root'); A  - CREATE TABLE - IF  not EXISTSUser_role ( theIautoidINT( One) Auto_increment, -IuseridINT( One), -IroleidINT( One), -     PRIMARY KEY(iautoid) +) ENGINE=INNODBDEFAULTCHARSET=UTF8 auto_increment= 1; -  + INSERT  intouser_role (Iuserid, Iroleid) A VALUES at(1,1), -(1,2), -(2,1), -(3,2), -(3,1);

the Rolemodel can then be established:

1 <? PHP 2 3 class extends \eloquent  {45     protected$table = ' roles '; 6     protected $primaryKey = ' iautoid '; 7     protected $connection = ' user '; 8 }

  Then create a "many-to-many" association relationship in Usermodel:  

1<?PHP2 3 classUsermodelextends\eloquent {4 5     protected $table= ' users ';6     protected $primaryKey= ' Iautoid ';7     protected $connection= ' user ';8 9      Public functionroles () {Ten         return $this->belongstomany (' Rolemodel ', ' user_role ', ' Iuserid ', ' Iroleid '); One     } A}

The following query is implemented:  

1 <? PHP2 3 class HomeController extends Basecontroller {4 5      Public functiongetusers () {6         7$resData=Usermodel::find (1) -roles;8Var_dump ($resData -ToArray ());9         Exit();Ten     } One}

  

  The results of the above query are:

  

  

Well, it's just a simple study, and there's a more complicated need for the little friends to see the API themselves.

  

Eloquent ORM's associated query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.