搜索操作不起作用

时间:2015-12-18 15:42:49

标签: symfony search entity

我必须在具有位于另一个实体中的字段的实体中执行搜索操作。

这是实体中的字段:

    /**
     * @ORM\ManyToOne(targetEntity="Tipologia", inversedBy="recipes")
     * @ORM\JoinColumn(name="tipologia_id", referencedColumnName="id")
     */
    protected $tipologia;

    /**
     * Set tipologia
     *
     * @param \AppBundle\Entity\Tipologia $tipologia
     *
     * @return Recipe
     */

    public function setTipologia(\AppBundle\Entity\Tipologia $tipologia = null)
    {
        $this->tipologia = $tipologia;

        return $this;
    }

    /**
     * Get tipologia
     *
     * @return \AppBundle\Entity\Tipologia
     */
    public function getTipologia()
    {
        return $this->tipologia;
    }

我所谈论的领域是" tipologia"。

我希望通过此字段执行搜索操作,但如果我将其称为" tipologia"我收到了错误。

这是行动:

public function searchrecipeAction(Request $request)
{
    $request = $this->getRequest();
    $data = $request->request->get('search');

    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery(
                    'SELECT r FROM AppBundle:Recipe r
WHERE r.tipologia LIKE :data')
            ->setParameter('data', $data);

    $result = $query->getResult();

    return $this->render('default/searchrecipe.html.twig', [
        'result' => $result
    ]);
}

我能做些什么才能让它发挥作用? 谢谢你的每一个答案

2 个答案:

答案 0 :(得分:2)

目前,您正在比较字符串'数据'与实体tipologia。我假设Tipologia包含字段(如名称等)。要使用链接的Tipologia搜索带有名称字段的食谱,例如'数据',我建议使用以下查询构建器:

z3 -smt2 <filename>

希望有所帮助;如果没有看到链接实体有哪些其他字段,就无法提供更多帮助。

答案 1 :(得分:0)

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Tipologia
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Tipologia
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

    /**
     * @ORM\OneToMany(targetEntity="Recipe", mappedBy="tipologia")
     */
    protected $tipologia;

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

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Tipologia
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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



    /**
     * Add tipologium
     *
     * @param \AppBundle\Entity\Recipe $tipologium
     *
     * @return Tipologia
     */
    public function addTipologium(\AppBundle\Entity\Recipe $tipologium)
    {
        $this->tipologia[] = $tipologium;

        return $this;
    }

    /**
     * Remove tipologium
     *
     * @param \AppBundle\Entity\Recipe $tipologium
     */
    public function removeTipologium(\AppBundle\Entity\Recipe $tipologium)
    {
        $this->tipologia->removeElement($tipologium);
    }

    /**
     * Get tipologia
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTipologia()
    {
        return $this->tipologia;
    }
}