在非对象(实际上是对象)上调用成员函数xx()

时间:2012-03-23 22:18:37

标签: symfony

ValidateGatherType.php

namespace D2E\CoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class ValidateGatherType extends AbstractType
{
    private $gather;

    public function __construct(Gather $gather) 
    {
        $this->gather = $gather;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $gather_players = $gather->getGatherPlayers();
    }

}

GatherController.php

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class GatherController extends Controller
{

/**
 * @Route("/teams/{id_team}/gather/{id_gather}")
 * @Template()
 */
public function showAction(Team $team, Gather $gather) 
{
    new ValidateGatherType($gather);
}

执行此操作时遇到错误$ gather-> getGatherPlayers():

Fatal error: Call to a member function getGatherPlayers() on a non-object

Soooooo,wtf(它被迫作为Gather但是它不再是一个对象?)?我想我忘了使用什么东西,但我找不到什么。 控制器代码是不同的,但由于该行不起作用(即使我像我给你看的那样使用它)我没有把剩下的代码。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

public function buildForm(FormBuilder $builder, array $options)
{
    $gather_players = $gather->getGatherPlayers();
}

此处未定义局部变量$gather。您可以理解为实例属性$this->gather