? ??? ThinkPhp? ORM? ??? ? ??? ??? ???? ?????? ?? ??? ????? ??? ?????. Hasmany () ? Solgstomany () ???? ???? ?? ??? ???? ???? ??? ?? ???? ?? ??? ?????.
ThinkPHP ???? ??? ? ??? ??? ?????
ThinkPhp? ORM (Object-Relational Mapping)? ?????? ??? ???? ??? ??? ???? PHP ??? ?????? ?? ?? ??? ??????. ??? ??? ?? ???? ? ???? ?? ???? ?? ???? ?? ? ??? ??? ??? ?????. ?? ??, User
??? ? ???? ?? ???? ?? ??? Post
??? ??? ??? ?? ? ????. hasMany()
???? ???? User
?? ? ??? ??? ?????. ??? ??? ????.
<code class="php"><?php namespace app\model; use think\Model; class User extends Model { public function posts() { return $this->hasMany('Post', 'user_id', 'id'); } }</code>
? ??? hasMany
??? ?????. 'Post'
?? ??? ???? 'user_id'
User
???? ???? Post
???? ?? ??? 'id'
? User
???? ?? ????. ?? ???? ?????? User
???? posts()
???? ??? ? ????.
<code class="php">$user = User::find(1); $posts = $user->posts; // Accesses all posts associated with the user. foreach ($posts as $post) { echo $post->title . "<br>"; }</code>
??? ??? ?? ? ?????. ?? ???? ?????. ???? ?? ??? ? ? ?? ?? ????? ??? ? ??? User
? Role
??? ??? ?? ? ???. user_id
? role_id
???? user_role
?? ???? ?????. User
???? :
<code class="php"><?php namespace app\model; use think\Model; class User extends Model { public function roles() { return $this->belongsToMany('Role', 'user_role', 'user_id', 'role_id'); } }</code>
????? Role
???? :
<code class="php"><?php namespace app\model; use think\Model; class Role extends Model { public function users() { return $this->belongsToMany('User', 'user_role', 'role_id', 'user_id'); } }</code>
??? belongsToMany()
???? ??? ??? ?????. ? ?? ??? ?? ??? ???? ? ??? ? ?? ??? ?? ???? ?? ????. ?? ??? ????? ?? ???? ?????.
<code class="php">$user = User::find(1); $roles = $user->roles; // Accesses all roles associated with the user. foreach ($roles as $role) { echo $role->name . "<br>"; }</code>
ThinkPhp?? ?????? ??? ?????? ?? ??
ThinkPhp? ???? ?????? ?? ?? ? ?? ?? ??? ???? ??? :
- ??? ?? ?? : ?????? ???? ???? ???? ???? ??? ?? ??? ?????. ?? ? ??? ?? ?? ??? ??????.
- ??? ?? ?? : ???, ? ? ??? ?? ??? ?? ?? ??? ???, ??? ? ?? ??? ??????. ??? ??? ???? ??????.
- ???? ?? ?? : ?????? ??? ????? ?? ?? ??? ??????. ?? ???? ?? ???? ???? ?? ??? ??? ?????? (
with()
)? ??????. - ??? ??? ?? : ?? ??? ??? ??? ??? ???? ??? ???? ??????? ???? ?? ?????. ??? ??? ???? ?????.
- ?? ?? : ?????? ?? ?? ?? ?? ??? ?? ??? ? ??? ???? ?????? ??? ?? ?? ????? ?????.
- ???? ?? : ?? ???? ??? ?? (?? ? ????)? ???? ?????? ????? ???? ??? ???? ??????. ?? ??? ?? ?? ????? ?????.
- ?? : ?? ????? ???? ?? ?? ????? ???? ?????? ??? ??? ??? ????? ?? ??????. ThinkPhp? ?? ??? ???? ??? ?????.
ThinkPhp? ORM? ???? ?? ???? ????? ?????
ThinkPhp? ORM? ?? ???? ???? ????? ??? ??? ?????. with()
???? ???? ??? ??? n 1 ??? ??? ? ?????. ? ?? ???? ?? ??? ??? ???? ?? Eger Loading? ?? ???? ?? ?? ???? ?????.
<code class="php">$users = User::with('posts')->select(); // Eager loads posts for all users foreach ($users as $user) { foreach ($user->posts as $post) { echo $post->title . "<br>"; } }</code>
?? ??? ????? ?? with()
??? ??? ??? ??? ? ????.
<code class="php">$users = User::with(['posts' => function ($query) { $query->where('status', 'published'); }])->select(); // Eager loads only published posts</code>
?? ??? ?? ??? ??? ?? ????. ??? ??? ??? ????.
<code class="php">$users = User::alias('u') ->join('post p', 'u.id = p.user_id') ->field('u.name, p.title') ->select();</code>
?? User
? Post
???? ?? ???? ?? ??? ????? ?? ? ? ????.
ThinkPhp? ?? ??? ??? ?????? ??? ??????
ThinkPhp? ?? ??? ?? ???? ??? ??? ?????? ??? ?? ??????. ?? SQL ??? ???? ?? ORM? ?? ??? ???? ?? ????? ???? ???? ???? ???? ???? ?? ? ? ??????. ?? ?? ???, ?? ?? ???? ?? ??? SQL ?? ???? ??? ????.
?? ??, ?? ???? ??? ???? ???? ?? ????. ??? ???? ?? ? ? ????.
<code class="php">// Assuming Post has a hasMany relationship with Comment $users = User::with(['posts' => function ($query) { $query->with('comments'); }])->select(); foreach ($users as $user) { foreach ($user->posts as $post) { echo $post->title . "<br>"; foreach ($post->comments as $comment) { echo $comment->content . "<br>"; } } }</code>
????? ?? SQL ???? ?? ??? ???? ???? ??? ???? ???? ????. ThinkPhp? ORM? ?? SQL ??? ???? ???? SQL? ?????? ?? ????? ??? ??? ? ????. ?? ?? ???? ?? ????? ?? ???? ????.
? ??? ThinkPHP ???? ?? (???, ??)? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??









