開發與維運

3分鐘短文:說說Laravel模型中還算常用的2個“關係”

引言

上一章我們介紹了比較簡單的laravel模型關聯關係中的一對一,介紹了關聯操作方法。
太難的概念理解起來都費勁,更不用說寫代碼了,所以對於太難的那些關聯關係,
且不論其效率如何,我們都不先做介紹。

img

本期說一說2個比較常用的關聯模型。

belongsTo 關係

正好像對於一個詞語,找到對應的反義詞,或者說有一個圖片,找到其鏡像圖片這樣的。
有作用力,就有反作用力。一對一關係模型中,A有一個B,則反過來,B屬於一個A。
這就是首先要介紹的 belongsTo 關係。

在模型Profile中添加對應到User模型的關係:

class Profile extends Model {
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

也就是說,有一個profile是從屬於user的,這與User模型的hasOne正好是對應關係。
在代碼中使用該關聯關係:

$email = Profile::where('id', 3)->first()->user->email;

其中first方法返回一個Profile模型對象實例,在Profile類中我們聲明瞭 user() 方法用於關係用戶模型,
所以此處鏈式調用 user 屬性,返回的是一個 AppUser 對象實例,其包含 User 模型的所有屬性,
因此 email 屬性也相應返回數據庫內的字段值。

一對多關係

還有一個常見的關聯關係是一對多。比如一個用戶有多個手機號,一種狀態包含很多個事件,一個商品有多個標籤等等等等,
這都是一對多的常見用法。

我們使用State模型狀態有多個Event事件這個場景,演示一下一對多關係的聲明,以及應用。在命令行創建模型文件,同時創建遷移文件:

php artisan make:model State --migration

默認在 AppState.php 文件內生成下面的代碼:

use Illuminate\Database\Eloquent\Model;
class State extends Model {}

我們還是先去生成數據庫表的遷移文件,手動實現遷移字段:

public function up()
{
    Schema::create('states', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('abbreviation');
        $table->timestamps();
    });
}

以及撤回遷移時刪除表:

public function down()
{
    Schema::drop('states');
}

接著在命令行執行遷移指令:

php artisan migrate

執行成功,數據庫表states就創建成功了。

我們說關聯關係需要外鍵,所以需要手動在events表內追加一個字段 state_id,用於指向剛才創建的表states的id字段。
執行命令行,創建遷移文件:

php artisan make:migration add_state_id_to_events_table --table=events

手動實現遷移文件的修改:

public function up()
{
    Schema::table('events', function (Blueprint $table) {
        $table->integer('state_id')->unsigned()->nullable();
        $table->foreign('state_id')->references('id')->on('states');
    });
}

以及回滾遷移時手動刪除追加的字段:

public function down()
{
    Schema::table('events', function (Blueprint $table) {
        $table->dropForeign('events_state_id_foreign');
        $table->dropColumn('state_id');
    });
}

基礎數據準備完畢,下面在模型內添加關聯關係:

class State extends Model {
    public function events() {
        return $this->hasMany('App\Event');
    }
}

非常直觀,一種狀態,有若干個事件。反過來,一個事件,一定屬於某種狀態,那就是belongsTo關係。

class Event extends Model {
    public function state()
    {
        return $this->belongsTo('App\State');
    }
}

代碼層面也準備好了,下面可以開始使用了。比如創建事件時,手動為其指定狀態:

$event = new Event;
$event->name = "Laravel Hacking and Pizza";
$event->state_id = 41;
$event->save();

注意,hasMany關聯關係,返回的是多個模型的集合,可以後續鏈式調用集合的所有方法。

寫在最後

本文不失簡單地介紹了belongsTo和hasMany兩個關聯關係,這在代碼中僅次於hasOne關係,
使用的頻次比較高的。而效率也就是根據外鍵多查詢一次SQL的消耗而已。但是明白其中原理之後,
在代碼內耗時的操作裡,也絕不可濫用關聯關係,否則會嚴重消耗性能。

Happy coding 🙂

我是@程序員小助手,專注編程知識,圈子動態的IT領域原創作者

Leave a Reply

Your email address will not be published. Required fields are marked *