symfony在控制器'无动作'函数中获取存储库函数

时间:2017-05-16 06:07:11

标签: php symfony

我有一个控制器,我的功能很少,不是动作。我用ajax调用该函数,但我的存储库有问题。我正在调用AjaxControlsController类以及我发送给控制器indexAction女巫action param的所有ajax调用,并检查switch中的操作并从那里调用确切的方法例如,SquadsController setFormation功能。 当我试图在没有动作函数的情况下获取存储库时,它会返回我:

  

错误:在null

上调用成员函数get()

如何在控制器中获取并创建工作存储库功能。

控制器

class SquadsController extends Controller {
    public function setFormation( $request )
        {
            $teamId = $request->request->get('teamid');
            $bIsSquadeEdit = false;
            $currentteamid = $request->request->get('currentteamid');

            if ( $currentteamid ) {
                $bIsSquadeEdit = true;
                $this->get('OulalaiFrame.repository.squad')->updateSquadFromation($this->aSlots,$currentteamid);
            }

            $this->formationList = $this->get('OulalaiFrame.repository.teams')->getFormation();
            $this->aFormationSlots = $this->get('OulalaiFrame.repository.teams')->assignFormationDetails($this->aSlots);
            $this->chackPlayersInFormation($this->aSlots,$teamId,$bIsSquadeEdit,true);

            return array(
                'leagueId'      => $this->leagueId, 
                'formationList' => $this->formationList,
                'playervalue'   => 45,
                'budget'        => $this->aBudget,
                'teamid'        => ( $this->iCurrentTeamId ) ? $this->iCurrentTeamId : null,
                'loadingteam'   => 0,
                'fieldFormation'=> $this->aFormationSlots,
                'formation'     => $this->aSlots,
                'playerslist'   => $this->aPlayers,
                'valid_team'    => $this->bIsValid,
                'budgetleft'    => $this->getBudgetLeft()
            );
        }
}

存储库

namespace OulalaiFrameBundle\Repository;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManager;
/**
 * TeamsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class TeamsRepository 
{
    /**
     * @var EntityManager
     */
    private $em;


    /**
     * ConfigRepository constructor.
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * return array formations
     */
    public function getFormation()
    {
        $em = $this->em->getConnection()
            ->prepare(
                'SELECT * 
                 FROM `formation`'
            );
        $em->execute();
        $fromation = $em->fetchAll();
        return $fromation;
    }
}

1 个答案:

答案 0 :(得分:0)

确保Controller是

的实例
  

的Symfony \捆绑\ FrameworkBundle \控制器\控制器