【发布时间】:2014-09-08 18:51:23
【问题描述】:
我正在使用存储库模式并尝试建立模型之间的关系。当我尝试运行试图使用 user() 方法(与 Party 模型建立关系)的 store() 方法(在控制器中)时,我收到以下错误消息:
不应静态调用非静态方法 Party::user(),假设 $this 来自不兼容的上下文
我不明白为什么当我尝试运行 user() 关系方法时出现此错误,但所有其他方法(包括 $this->party->all()、$this->party- >create($data)),工作得很好。
以下是相关代码:
// PartiesController.php
public function __construct(Party $party){
$this->party = $party
}
public function store(){
$data = Input::all();
$user = Sentry::getUser();
$this->party->user()->create($data);
}
// Party.php
class Party extends Eloquent{
public function user(){
return $this->belongsTo('User');
}
}
// User.php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel implements UserInterface, RemindableInterface {
public function party(){
return $this->hasMany('Party');
}
}
// PartyRepository.php
namespace repositories\Party;
interface PartyRepository{
public function all();
public function findByID($id);
public function create($input);
public function user();
}
// EloquentPartyRepository.php
namespace repositories\Party;
use Party;
class EloquentPartyRepository implements PartyRepository{
public function all(){
return Party::all();
}
public function create($input){
return Party::create($input);
}
public function user(){
return Party::user();
}
}
【问题讨论】:
标签: php laravel-4 repository-pattern static-methods non-static