首頁  >  文章  >  後端開發  >  Eloquent ORM學習筆記

Eloquent ORM學習筆記

WBOY
WBOY原創
2016-08-08 09:31:09885瀏覽

  最近在學習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

  在appmodels下建立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