Eloquent ORM study notes

WBOY
Release: 2016-08-08 09:31:09
Original
798 people have browsed it

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:

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

 2. Configure database

 Add database configuration in database.php:

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

3. Create UserModel

 Create UserModel.php under appmodels

1 php 2 3 class UserModel extends \Eloquent { 4 protected $table = 'users'; 5 protected $primaryKey = 'iAutoID'; 6 protected $connection = 'user'; 7 }
Copy after login

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:

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

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

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

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.

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!