2015-01-12 244 views
0

我有麻煩設置數據透視表的正確關係(belongsTo,hasMany,...)。Laravel 4:雄辯和關係

爲簡明起見,我將縮寫代碼。 我有兩個重要的表格:'派對'和'p2p_relations'。 這是各方

public function up() 
    { 
     Schema::create('parties', function ($table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->unsignedInteger('kind'); 
      $table->timestamps(); 
      $table->softDeletes(); 

      $table->foreign('kind')->references('id')->on('kinds'); 
     }); 
    } 

遷移這是p2p_relations(方方關係)

public function up() 
    { 
     Schema::create('p2p_relations', function ($table) { 
      $table->bigIncrements('id'); 
      $table->unsignedInteger('context'); 
      $table->unsignedInteger('reference'); 
      $table->datetime('start'); 
      $table->datetime('end')->nullable(); 
      $table->unsignedInteger('kind')->nullable(); 
      $table->timestamps(); 
      $table->softDeletes(); 

      $table->foreign('context')->references('id')->on('parties'); 
      $table->foreign('reference')->references('id')->on('parties'); 
      $table->foreign('kind')->references('id')->on('kinds'); 
     }); 
    } 

模型遷移黨

class Party extends Ardent 
{ 
    use SoftDeletingTrait; 

    protected $softDelete = true; 
    protected $dates = ['created_at', 'updated_at', 'deleted_at']; 

    protected $table = 'parties'; 

    public static $rules = array(
     'name' => 'required', 
     'kind' => 'required|numeric' 
    ); 

} 

模型爲關係

class Relation extends Ardent 
{ 
    use SoftDeletingTrait; 

    protected $softDelete = true; 
    protected $dates = ['created_at', 'updated_at', 'deleted_at']; 

    protected $table = 'p2p_relations'; 

    public static $rules = array(
     'context' => 'required|numeric', 
     'reference' => 'required|numeric', 
     'kind' => 'required|numeric', 
     'start' => 'required|date', 
     'end' => 'date' 
    ); 
} 

如何設置的關係,這樣我可以關聯方作爲關係上下文或引用。 我認爲屬於關聯會幫助像這樣在課堂上關係

public function context() { 
     return $this->belongsTo('Party', 'context', 'id'); 
    } 
    public function reference() { 
     return $this->belongsTo('Party', 'reference', 'id'); 
    } 

但是當我運行這個單元測試,我得到一個錯誤:未定義的屬性:關係:: $背景

$context = new Party(); 
     $context->name = 'Person A'; 
     $context->kind = 1; 
     $context->save(); 

     $ref = new Party(); 
     $ref->name = 'Company B'; 
     $ref->kind = 2; 
     $ref->save(); 

     $relation = new Relation(); 
     $relation->start = new DateTime(); 
     $relation->context()->associate($context); 
     $relation->reference()->associate($ref); 
     $relation->kind = 3; 
     $relation->save(); 

有什麼想法嗎?我真的是這個框架的新手。

+0

請嘗試看看文檔。 http://laravel.com/docs/4.2/eloquent#relationships您不應該被要求手動定義關係模型。 – Kao

+0

因此,將以下內容放到Party模型中: 'public function context(){ return $ this-> belongsToMany('Party','p2p_relations','context'); } public function reference(){ return $ this-> belongsToMany('Party','p2p_relations','reference'); }' 但是,如何在關係中添加額外的屬性?作爲開始,善良,... – BakGat

+0

@jaan首先閱讀文檔,檢查'withPivot','WherePivot'方法。然後,修復您評論中的關係。接下來檢查一下:https://github.com/jarektkaczyk/Eloquent-triple-pivot –

回答

1

得益於給予我學到了很多東西:-)

更新了我黨型號:

public function references() { 
    return $this->belongsToMany('Party', 'p2p_relations', 'context', 'reference') 
     ->withPivot('reference', 'start', 'kind') 
     ->withTimestamps() ; 
} 

沒有關係模型所需。

數據透視表完美地工作。

謝謝