使用Doctrine QueryBuilder时变量未定义

时间:2017-08-05 14:04:17

标签: php symfony doctrine-orm orm

我想通过使用Doctrine来制作存储过程的替代方法但是我被卡住了,任何人都可以帮助我吗?

要形成的示例存储过程:

CREATE PROCEDURE catalog_get_department_details(IN DepartmentName)
    BEGIN
    SELECT name, description
    FROM
    department
    WHERE name = name;

部门实体:

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\departmentsRepository")
 * @ORM\Table(name="departments")
 */
class departments
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $department_id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $description;

    /**
     * @ORM\OneToMany(targetEntity="categories",mappedBy="departments")
     */
    private $categories;

    function __construct()
    {
        $this->categories = new ArrayCollection();
    }

    public function getDepartmentId()
    {
        return $this->department_id;
    }

    public function setDepartmentId($department_id)
    {
        $this->department_id = $department_id;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return mixed
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * @param mixed $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

该方案是路线为 / index / departmentname / Regional ; 我的 DefaultController 会将区域作为参数

捕获

DefaultController:

class DefaultController extends Controller
{
    /**
     * @Route ("/index/department/{department_name}")
     */
    function departmentAction($department_name)
    {
        // accessing departmentsRepository 
        $categoriesRepository = $this->getDoctrine()->getManager()
            ->getRepository('AppBundle:departments');
        $categoriesRepository->getDepartmentDetails($department_name);
    }

departmentsRepository:

class departmentsRepository extends \Doctrine\ORM\EntityRepository
{
    function getDepartmentDetails($departmentName)
    {
        $em=$this->getEntityManager()->getRepository('AppBundle:departments');
        $qb=$em->createQueryBuilder('dep');
        $qb->select('dep.name','dep.description');
        $qb->where("dep.name=$departmentName");

当我致电var_dump($qb->getDQL());die;时,它会向我显示我想要的内容:

  

SELECT dep.name,dep.description FROM AppBundle \ Entity \ departments dep WHERE dep.name = Regional

然后我通过调用

执行它
$qb->getQuery()->execute();

但是我收到以下错误:

  

[语义错误]第0行,第86行附近'区域':错误:'区域'   没有定义。

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

您的dep.name值未被转义。您可能希望查询看起来像这样:

WHERE dep.name='Regional'

但你应该做什么,更安全的是将它绑定到参数,如下所示:

$em = $this->getEntityManager()->getRepository('AppBundle:departments');
$qb = $em->createQueryBuilder('dep');
$qb->select('dep.name', 'dep.description');
$qb->where("dep.name = :departmentName");
$qb->setParameter('departmentName', $departmentName);

Doctrine将为您安全地处理逃脱。这也允许您避免SQL注入攻击。此外,由于您已经在departments存储库中,因此您应该能够使用_em值作为快捷方式,而不必重新指定departments实体,如下所示:

$qb = $this->_em->createQueryBuilder('dep');
$qb->select('dep.name', 'dep.description');
$qb->where("dep.name = :departmentName");
$qb->setParameter('departmentName', $departmentName);

方面没有,在您的控制器操作中,您正在调用存储库函数,但实际上并未将结果保存到任何变量。