Home  >  Article  >  Backend Development  >  Eloquent ORM study notes

Eloquent ORM study notes

WBOY
WBOYOriginal
2016-08-08 09:31:09883browse

 I have been learning Laravel recently and feel that the ORM function is very powerful. I have only briefly explored it here. If you have better notes, please share them.

Since the focus is on Eloquent ORM, the routing settings and controller will not be described in detail. Here we go directly to the Models module.

 1. Database preparation (Mysql)

  Here you need to create two tables, users and users_ext, and initialize the data:  

<span> 1</span> <span>CREATE</span> <span>TABLE</span>
<span> 2</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span><span> users (
</span><span> 3</span>     iAutoId <span>INT</span> (<span>11</span>) <span>NOT</span> <span>NULL</span><span> AUTO_INCREMENT,
</span><span> 4</span>     sNmame <span>VARCHAR</span> (<span>20</span><span>),
</span><span> 5</span>     iStatus <span>TINYINT</span> (<span>4</span><span>),
</span><span> 6</span>     iCreateTime <span>INT</span> (<span>11</span><span>),
</span><span> 7</span>     <span>PRIMARY</span> <span>KEY</span><span> (iAutoId)
</span><span> 8</span> ) ENGINE <span>=</span> INNODB <span>DEFAULT</span> CHARSET <span>=</span> utf8 AUTO_INCREMENT <span>=</span> <span>1</span><span>;
</span><span> 9</span> 
<span>10</span> <span>INSERT</span> <span>INTO</span><span> users (sNmame, iStatus, iCreateTime)
</span><span>11</span> <span>VALUES</span>
<span>12</span>     (<span>'</span><span>test1</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>),
</span><span>13</span>     (<span>'</span><span>test2</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>),
</span><span>14</span>     (<span>'</span><span>test3</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>),
</span><span>15</span>     (<span>'</span><span>test4</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>),
</span><span>16</span>     (<span>'</span><span>test5</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>),
</span><span>17</span>     (<span>'</span><span>test6</span><span>'</span>, <span>1</span>, <span>1400085387</span><span>);
</span><span>18</span> 
<span>19</span> <span>CREATE</span> <span>TABLE</span>
<span>20</span> <span>IF</span> <span>NOT</span> <span>EXISTS</span><span> users_ext (
</span><span>21</span>     iAutoId <span>INT</span> (<span>11</span>) <span>NOT</span> <span>NULL</span><span> AUTO_INCREMENT,
</span><span>22</span>     iAge <span>DECIMAL</span> (<span>3</span>, <span>0</span><span>),
</span><span>23</span>     sSex <span>TINYINT</span> (<span>4</span><span>),
</span><span>24</span>     iUserID <span>INT</span> (<span>11</span><span>),
</span><span>25</span>     <span>PRIMARY</span> <span>KEY</span><span> (iAutoId)
</span><span>26</span> ) ENGINE <span>=</span> INNODB <span>DEFAULT</span> CHARSET <span>=</span> utf8 AUTO_INCREMENT <span>=</span> <span>1</span><span>;
</span><span>27</span> 
<span>28</span> <span>INSERT</span> <span>INTO</span><span> users_ext (iAge, sSex, iUserID)
</span><span>29</span> <span>VALUES</span>
<span>30</span>     (<span>24</span>, <span>1</span>, <span>1</span><span>),
</span><span>31</span>     (<span>109</span>, <span>0</span>, <span>2</span><span>),
</span><span>32</span>     (<span>25</span>, <span>1</span>, <span>3</span><span>),
</span><span>33</span>     (<span>20</span>, <span>1</span>, <span>5</span><span>),
</span><span>34</span>     (<span>68</span>, <span>0</span>, <span>4</span><span>),
</span><span>35</span>     (<span>48</span>, <span>1</span>, <span>6</span>);

  2. Configure database

 Add database configuration in database.php:

 

<span> 1</span> <?<span>php
</span><span> 2</span> 
<span> 3</span> <span>return</span><span> [
</span><span> 4</span> 
<span> 5</span>     'fetch' => PDO::FETCH_CLASS,
<span> 6</span> 
<span> 7</span>     'default' => 'user',
<span> 8</span> 
<span> 9</span>     'connections' =><span> [
</span><span>10</span>         'user' =><span> [
</span><span>11</span>             'driver'    => 'mysql',
<span>12</span>             'host'      => 'localhost:3306',
<span>13</span>             'database'  => 'test',
<span>14</span>             'username'  => 'root',
<span>15</span>             'password'  => 'root',
<span>16</span>             'charset'   => 'utf8',
<span>17</span>             'collation' => 'utf8_unicode_ci',
<span>18</span>             'prefix'    => '',
<span>19</span>         ],
<span>20</span> <span>    ]
</span><span>21</span> ];

 3. Create UserModel

 Create UserModel.php under appmodels 

<span>1</span> <?<span>php
</span><span>2</span> 
<span>3</span> <span>class</span> UserModel <span>extends</span><span> \Eloquent {
</span><span>4</span>     <span>protected</span> <span>$table</span> = 'users'<span>;
</span><span>5</span>     <span>protected</span> <span>$primaryKey</span> = 'iAutoID'<span>;
</span><span>6</span>     <span>protected</span> <span>$connection</span> = 'user'<span>;
</span><span>7</span> }

  Such a User model has been created successfully, and the code is very simple. As for the meaning of the member variables, I believe everyone can understand them. They represent the table name, primary key, and database connection identifier (in the configuration file) in order.

 5.Use UserModel

 Now you can use it anywhere. It can be used in controllers and routers. Here’s a simple example:

 Now there is a query statement: 

<span> 1</span> <span>SELECT</span>
<span> 2</span> <span>    users.sNmame,
</span><span> 3</span> <span>    users.iCreateTime,
</span><span> 4</span> <span>    users_ext.iAge,
</span><span> 5</span> <span>    users_ext.sSex
</span><span> 6</span> <span>FROM</span>
<span> 7</span> <span>    users
</span><span> 8</span> <span>LEFT</span> <span>JOIN</span> users_ext <span>ON</span> users.iAutoId <span>=</span><span> users_ext.iUserID
</span><span> 9</span> <span>WHERE</span>
<span>10</span>     users.iStatus <span>=</span> <span>1</span>
<span>11</span> <span>AND</span> users_ext.sSex <span>=</span> <span>0</span>
<span>12</span> <span>ORDER</span> <span>BY</span>
<span>13</span> <span>    users.iCreateTime
</span><span>14</span> LIMIT <span>0</span><span>,
</span><span>15</span>  <span>1</span>

  Here is a simple query statement, and then it will be implemented in the form of ORM:

<span>1</span> <span>public</span> <span>function</span><span> getUsers(){
</span><span>2</span> 
<span>3</span>         <span>$select</span> = 'users.sNmame,users.iCreateTime,users_ext.iAge,users_ext.sSex'<span>;
</span><span>4</span>         <span>$resData</span> = UserModel::selectRaw(<span>$select</span>)->leftJoin('users_ext','users.iAutoId','=','users_ext.iUserID')->where('users.iStatus','=',1)->where('users_ext.sSex','=',0)->skip(0)->limit(1)-><span>get();
</span><span>5</span>         <span>var_dump</span>(<span>$resData</span>-><span>toArray());
</span><span>6</span>         <span>exit</span><span>();
</span><span>7</span> }

 The following are the query results:

 

  ok, the above is just a simple example of query, there is still a lot to study, such as the association between modules, etc.

 

 

 

The above introduces the Eloquent ORM study notes, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn