PHP-Laravel - Eloquent ORM

摘要:PHP-Laravel - Eloquent ORM

維護PHP Laravel案子,邊學Laravel,才知道Laravel有ORM技術

http://laravel.com/docs/4.2/eloquent

 

為了查詢一些關鍵字,來瞭解語法,

像morphMany

 

查到

Polymorphic Relations

Polymorphic relations allow a model to belong to more than one other model, on a single association. For example, you might have a photo model that belongs to either a staff model or an order model. We would define this relation like so:

class Photo extends Eloquent {
    public function imageable()
    {
        return $this->morphTo();
    }

}

class Staff extends Eloquent {

    public function photos()
    {
        return $this->morphMany('Photo', 'imageable');
    }

}

class Order extends Eloquent {

    public function photos()
    {
        return $this->morphMany('Photo', 'imageable');
    }

}

發現

The key fields to notice here are the imageable_id and imageable_type on the photos table. The ID will contain the ID value of, in this example, the owning staff or order, while the type will contain the class name of the owning model. This is what allows the ORM to determine which type of owning model to return when accessing the imageable relation.

Laravel 還支援圖檔的schema了。定義好了。

真是相當恐怖的技術。