首页  >  文章  >  后端开发  >  Eloquent ORM学习笔记

Eloquent ORM学习笔记

WBOY
WBOY原创
2016-08-08 09:31:09883浏览

  最近在学习Laravel,觉得ORM功能很强大,我这里只是简单探索了一点,如果有更好的笔记,还请分享。

  因为重点在于Eloquent ORM,所以路由设置,控制器就不详细描述了,这里直接进入Models么模块。

  1.数据库准备(Mysql)

  在这里需要创建users和users_ext两张表,并初始化数据:  

<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.配置数据库

  在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.创建UserModel

  在app\models下创建UserModel.php  

<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> }

  这样一个User模型就创建成功了,代码很简单。至于其中成员变量的含义,相信都能看得懂,依次代表的是表名称、主键、数据库连接标识(配置文件中的)。

  5.使用UserModel

  现在你可以在任何地方使用了。控制器,路由里面都可以用。下面就在做个简单的列子:

  现在有查询语句:  

<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>

  这里是简单的一个查询语句,接下来就以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> }

  下面是查询结果:

  

  ok ,以上只是对查询做了一个简单的例子,还有很多需要研究,比如模块之间的关联等等。

  

  

  

以上就介绍了Eloquent ORM学习笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn