简介
其实大家都知道,数据表之间都是可以关联的,Eloquent ORM是数据模型操作代替表操作,那么表的关联查询,在Eloquent这里也就是模型间的关联查询,这就是本章的主要内容;
Eloquent一个支持以下6种表间关系:
-
One To One(一对一)
-
One To Many(一对多)
-
Many To Many(多对多)
-
Has Many Through(跨表一对多)
-
Polymorphic Relations(belongsTo多态)
-
Many To Many Polymorphic Relations(belongsToMany多态)
前三个关系是什么就不多解释了(请自行百度),这个地方需要很多耐心,努力让自己习惯对象思维,暂时先不要用sql,练一段时间就会发现非常好用;
当然,也有人吐槽Eloquent由于过于强大,造成每次加载都带一大堆东西,影响效率(实际没啥影响,不服的拿数据跟我说),但是在代码的编写效率,可读性,可维护性上可以说是质的飞跃,所以还是强烈建议优先使用。
这一章我是学得最早,但是教程是写的最晚的,因为各种关系比较复杂,好在我是做产品出身,应该可以把复杂的事情解释清楚,下面就让我们来认识一下这个星球上最强大的ORM:Eloquent
基本概念
先看看是怎么使用的吧:
$user->posts()->where('active', 1)->get();
这个就是通过 $user 对象查出他一共发布了多少篇文章(状态为已发布的)。
我们看到 posts() 在这里是 $user 的一个方法,所以要使用关联查询,得先在 $user 模型里定义关系(方法);
一对一
从这张图中,我们可以看到要使用Eloquent 模型关系,第一步要在模型里定义模型关系,第二步数据库要准备正确的表;第三部分使用 查询的方法,第四部分是使用绑定和解除绑定的方法,我们后续的模型关系都按照这个流程理解和解释;
要点:反复观察图!
定义模型
定义User模型
namespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ public function phone() { return $this->hasOne('App\Phone'); }}
定义Phone模型
namespace App;use Illuminate\Database\Eloquent\Model;class Phone extends Model{ public function user() { return $this->belongsTo('App\User'); }}
注意:定义模型的时候方法名注意单复数,“一”就用单数,“多”就用复数,这样不容易混淆搞错。 hasOne() 类似这个关系绑定方法有很多,需要在使用中慢慢熟悉,注意观察。
数据表
主要是注意,外键和中间表这些东西,本例中没有中间表;
关系查询
模型关系方法查询
$user->posts()->where('active', 1)->get();
这就是模型方法查询, $user->posts() 这里查出来是个DQB对象,后面可以接各种数据库查询方法;
动态属性查询
$user->posts;
你可以把关系当做属性用,直接查出来一个collection集;缺点是后面不能跟DQB方法了。
这两种方法都很常用,酌情使用;(下同,不再解释)
关系绑定和解除
通用的绑定方法就是save();
通用的解除方法,对于belongsTo的关系是dissociate(); 对于belongsToMany的关系是detach();
还有一些特定的绑定解除方法,我们再单独的关系模型中讲;
一对多
反绑
如果你要通过belongsTo关系绑定,可以用associate()来绑定,最后要save()一下,如图;
多对多
由于belongsTo换成了belongsToMany, 所以对应的绑定方法换成了attach()和detach();注意这里面填对象ID,可以用数组形式批量绑定;
save()方法仍然适用与关系绑定;
对中间表的操作
由于多对多关系多了一张中间表,Eloquent除了默认对这里面的外键进行操作以外,还可以对中间表的其他字段进行操作,看示例:
App\User::find(1)->roles()->save($role, ['expires' => $expires]);
$user->roles()->attach($roleId, ['expires' => $expires]);
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
sync()方法
sync就是同步,意思是说中间表会同步成sync()里面参数一样,没有写在参数里的都会移除,因此可以用来解除绑定;
Has Many Through(跨表一对多)
其实这只是一种快速查询方法,看图,本来可以先通过Conuntry查User,然后通过User查Post,现在你可以通过Country直接查Post;
通常Country和User,User和Post的表间关系都是事先建立好的,这个时候你再使用hasManyThrough;
Polymorphic Relations(belongsTo多态)
本来belongsTo只能属于一种对象的,就像女朋友只能属于男朋友;现在好了,女朋友不仅可以属于男朋友,还可以属于干爹了。
开玩笑的,最常见的应用还是图片,可以属于多种模型,看图;
我们发现这里的动词变成了morphTo(), morphMany();
先上一小段英语课,提升一下语感:
morph这个词的意思是改变形态,简称变态;这个和变形金刚的那个transform是不一样的,那个主要是改变形状;
morphTo(), morphMany(); 其实就是belongsTo() 和 hasMany() 多态形式,多态就是不仅拥有男票,还有干爹,有点“变态”;
关于绑定和解绑
associate()不适用用于morphTo();所以只能单向的用save()绑定了;
关于解绑,dissociate()也不适用用于morphTo();所以,只能把Photo实例删除!!删除了就没任何关系了(因为没有中间表)
感觉有点不是很自然的设计,不过目前调查下来情况就是这样;
Many To Many Polymorphic Relations(belongsToMany多态)
这个最典型的应用就是标签了,标签要涉及多对多的关系,还涉及对应不同类型的模型的问题,所以就是belongsToMany+多态;
看图,可绑定,可解绑;
Eager Loading(预加载)
这个问题很简单,看个实例你就懂了:
$books = App\Book::all();foreach ($books as $book) { echo $book->author->name;}
万一books有一万本,那么循环就要1万次,每次循环,因为用了关联查询 $book->author->name; ,都会读数据库一次,意味着至少读数据库一万次,数据库哭了。
Eager Loading 就是让数据库读取发生在循环之前:
$books = App\Book::with('author')->get();foreach ($books as $book) { echo $book->author->name;}
看,加了个神奇的with()后,所有数据在foreach前都读出来了,后面循环的只是读出来的数据,一共查询数据库2次!!
Eager Loading可以极大的缓解数据库查询压力,是性能优化的重要手段!
Eager Loading 多个关系
就是一次多加几张关联表而已;
$books = App\Book::with('author', 'publisher')->get();
嵌套 Eager Loading
$books = App\Book::with('author.contacts')->get();
把书的作者读出来,顺便把作者的联系方式读出来。
有条件的 Eager Loading
上面说的是整张表整张表的读出来,太土豪了,其实有时候我们只需要表里的部分记录:
$users = App\User::with(['posts' => function ($query) { $query->where('title', 'like', '%first%');}])->get();
这中闭包的写法我们讲过多次,就是加个条件而已;
Lazy Eager Loading
这标题难道不是自相矛盾吗?又Lazy,又Eager?
哦,原来是在lazy的流程里判断需不需要eager loading一下:
$books = App\Book::all();if ($someCondition) { $books->load('author', 'publisher');}
$books->load(['author' => function ($query) { $query->orderBy('published_date', 'asc');}]);
注意这个load(),这是对collection用的;
跨模型更新时间戳
简单的来说,就是一条评论更新的时候,顺便把文章的'updated_at'字段也更新了;
namespace App;use Illuminate\Database\Eloquent\Model;class Comment extends Model{ /** * All of the relationships to be touched. * * @var array */ protected $touches = ['post']; /** * Get the post that the comment belongs to. */ public function post() { return $this->belongsTo('App\Post'); }}
设置$touches这个属性;
然后你更新Comment的时候,就会把Post的'updated_at'字段也更新了;
更多内容请访问: Laravel 5.1 文档攻略

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools
