2015-01-12 245 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() ; 
} 

没有关系模型所需。

数据透视表完美地工作。

谢谢