使用仅在子类中定义的方法

时间:2017-05-30 17:45:44

标签: php laravel

我有一个方法:

protected function chooseAndUpdateParentFight(Fight $fight, Fight $parentFight)
{
    $fighterToUpdate = $fight->getParentFighterToUpdate();
    ...
}

我有两种打架:PreliminaryFightDirectEliminationFight

两者都是Fight

的子类

事情是getParentFighterToUpdate()的定义只存在于两个子类中,但不存在于Fight类中,所以我得到:

BadMethodCallException in Builder.php line 2443:
Call to undefined method 
Illuminate\Database\Query\Builder::getParentFighterToUpdate()

我该怎么办?

编辑:以下是getParentFighterToUpdate方法:

DirectEliminationFight类:

public function getParentFighterToUpdate()
{
    $childrenGroup = $this->group->parent->children;
    foreach ($childrenGroup as $key => $children) {
        $childFight = $children->fights->get(0);
        if ($childFight->id == $this->id) {
            if ($key % 2 == 0) {
                return "c1";
            }
            if ($key % 2 == 1) {
                return "c2";
            }
        }
    }
    return null;
}

PrimaryFight Class

public function getParentFighterToUpdate()
 {
    $childrenGroup = $this->group->parent->children;
    foreach ($childrenGroup as $key => $children) {
        $childFights = $children->fights;
        dd($childFights);
        // Still writing it
    }
    return null;
}

编辑2:

/**
 * Update fighter if it is possible
 * @param $fightsByRound
 */
protected function updateParentFight(Collection $fightsByRound)
{

    foreach ($fightsByRound as $fight) {
        $parentGroup = $fight->group->parent;
        if ($parentGroup == null) break;
        $parentFight = $parentGroup->fights->get(0);
        $this->chooseAndUpdateParentFight($fight, $parentFight);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用定义getParentFighterToUpdate方法的接口,让Fight类实现它。

但实际问题似乎是在其他地方,因为你显然是将Illuminate\Database\Query\Builder的实例传递给你的方法,而不是Fight。检查方法调用,看是否传递了正确的变量