2015-06-19 67 views
2

为了改进我的Laravel开发风格,我查看了this文章。所以我为我的用户模型创建了一个Interface和Repository。但是这个模型与“主题”模型有关。我也为这个创建了Interface和Repository。但是我需要在关系中提到什么?模型/实体本身还是接口/存储库?创建存储库,在模型关系中引用什么?

$id = 1; 
$user = User::find($id); 
$new_topics = $user -> topics() -> new(); 

只是我想要执行的代码的基本示例。那么我需要参考什么?这是用户模型:

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
} 

/* OR */ 

/* The Interface */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Repositories\TopicInterface'); 
} 

回答

0

您需要将模型传递给hasOne或其他关系方法,可用于Eloquent ORM。所以,你正在寻找的答案是,

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
} 

以往模式,你在的hasMany功能speficied什么东西有延长口才 或关系模式将无法正常工作。

0

您提到的文章解释了如何实现一个使处理实体更优雅的外观,但它与模型化实体间关系的Laravel的ORM部分无关。因此,在处理ORM时,您必须引用模型类而不是存储库。 因此,您的问题的答案是

/* The Model/Entity */ 
public function topics() 
{ 
    return $this -> hasMany('App\Models\Entities\Topic'); 
}