首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel - Repository模式问题

Laravel - Repository模式问题
EN

Stack Overflow用户
提问于 2014-11-25 04:46:45
回答 2查看 1.1K关注 0票数 0

我已经使用存储库模式获得了Laravel应用程序。我还有一个名为EloquentRepository的抽象类,它包含基本方法。我的所有存储库都有一个update()方法,其中我只需使用ID和数组来更新模型:

代码语言:javascript
复制
abstract class EloquentRepository {

    public function update($id, array $array) {
        $this->model->whereId($id)->update($array);
    }

}

现在,我还有一个Server存储库:

代码语言:javascript
复制
interface ServerRepository {

   public function update($id, array $options);

}

class EloquentServerRepository extends EloquentRepository implements ServerRepository {

    protected $model;

    public function __construct(Server $model)
    {
        $this->model = $model;
    }
}

所以现在,我不需要将update()方法添加到我的EloquentServerRepository中,也不需要添加任何其他需要这样做的存储库(相当多)。

然而,有一个存储库确实有更新功能,但我想让它做一些“自定义”的事情。假设它是用户存储库:

代码语言:javascript
复制
interface UserRepository {

   public function update($id, array $options, $status);

}

class EloquentUserRepository extends EloquentRepository implements UserRepository {

    protected $model;

    public function __construct(User $model)
    {
        $this->model = $model;
    }

    public function update($id, array $options, $status)
    {
        $this->model->setStatus($status);
        $this->model->whereId($id)->update($options);
    }
}

所以现在,我的用户存储库在每次更新时都需要一个状态。

然而,我得到了一个错误:

Declaration of EloquentUserRepository::update() should be compatible with EloquentRepository::update($id, array $array)

为什么会这样,我的接口肯定指定了声明应该是什么?

EN

回答 2

Stack Overflow用户

发布于 2014-11-25 04:53:11

您可以通过将$status设置为可选并为其提供默认值来传递该错误,例如:

代码语言:javascript
复制
public function update($id, array $options, $status = null)

如果它不是可选的(使用默认值),您就表示此方法需要有第三个参数,这违反了ServerRepository设置的约定

票数 1
EN

Stack Overflow用户

发布于 2014-11-25 05:00:24

这是因为您正在扩展EloquentUserRepository,其中的update方法如下所示:

代码语言:javascript
复制
public function update($id, array $array) {
    $this->model->whereId($id)->update($array);
}

在本例中,您还实现了UserRepository接口,但是根据基类的update方法,您的update方法具有不同的签名,如下所示:

代码语言:javascript
复制
public function update($id, array $options, $status);

因此,错误正在上升,因为你有不同的方法签名。虽然您可以使用下面这样的可选参数使这两个方法的签名相同:

代码语言:javascript
复制
// EloquentUserRepository
public function update($id, array $array, $status = null) {
    $this->model->whereId($id)->update($array);
}

// interface UserRepository
interface UserRepository {
    public function update($id, array $options, $status = null);
}

但我建议只使用一个接口或抽象类,并针对不同的用例覆盖EloquentUserRepository中的方法。它看起来像这样:

代码语言:javascript
复制
abstract class EloquentRepository {
    public function update($id, array $array, $status = null) {
        $this->model->whereId($id)->update($array);
    }
}

// Only extend the EloquentRepository and override the update method
class EloquentUserRepository extends EloquentRepository {

    protected $model;

    public function __construct(User $model)
    {
        $this->model = $model;
    }

    // re-declare the method to override
    public function update($id, array $options, $status = null)
    {
        $this->model->setStatus($status);
        $this->model->whereId($id)->update($options);
    }
}

或者稍微更改一下EloquentRepository,例如:

代码语言:javascript
复制
abstract class EloquentRepository {

    public function update($id, array $array, $status = null) {

        if(!is_null($status)) {
            $this->model->setStatus($status);
        }

        $this->model->whereId($id)->update($array);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27113860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档