Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findBy() foreignKey is not working

There is a relationship between the rubric and brand table (Many rubric to One brand).

Here is my code

    $dbChoices = $this->getConfigurationPool()
        ->getContainer()
        ->get('Doctrine')
        ->getManager()
        ->getRepository('RibambelMainBundle:RubricMenu')
        ->findBy(array('brand_id' => 2));

Everytime this part is run, I got the following error:

Unrecognized field: brand_id

The brand_id column is found in the rubric table. enter image description here

Anyone who can help me with this or show me another way of doing it?

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

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

    /**
     * @var int
     *
     * @ORM\Column(name="position", type="integer")
     */
    private $position;

    /**
     * @ORM\ManyToOne(targetEntity="Brand")
     * @ORM\JoinColumn(name="brand_id", nullable=false)
     */
    private $brand;


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

    /**
     * Set name
     *
     * @param string $title
     *
     * @return RubricMenu
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

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

    /**
     * Set position
     *
     * @param integer $position
     *
     * @return RubricMenu
     */
    public function setPosition($position)
    {
        $this->position = $position;

        return $this;
    }

    /**
     * Get position
     *
     * @return int
     */
    public function getPosition()
    {
        return $this->position;
    }


    /**
     * Set brand
     *
     * @param \Ribambel\MainBundle\Entity\Brand $brand
     *
     * @return RubricMenu
     */
    public function setBrand(\Ribambel\MainBundle\Entity\Brand $brand = null)
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * Get brand
     *
     * @return \Ribambel\MainBundle\Entity\Brand
     */
    public function getBrand()
    {
        return $this->brand;
    }
}
like image 910
bastien Avatar asked Jan 29 '18 13:01

bastien


2 Answers

Searching by a foreign key simply does not make sense from an object point of view. brand_id is not a field of your RubricMenu entity, brand is.

If you have a Brand entity, then you can use findBy (or the magic method findByBrand) like this: (with $myBrand an instance of Brand)

$repository->findBy(['brand' => $myBrand]);
// OR
$repository->findByBrand($myBrand);

If you simply have the id of your target entity, you can use the IDENTITY DQL function but in that case you cannot use findBy directly as it expects a valid field name. Thus, you will need to add a method in your RubricMenu repository. Your method could look like this:

public function findByBrandId($brandId)
{
    $qb = $this->createQueryBuilder('rm');
    $qb->where('IDENTITY(rm.brand) = :brandId')
       ->setParameter('brandId', $brandId);

    return $qb->getQuery()->getResult();
}

On a side note, as pointed out by @l.g.karolos's answer, doing

$repository->findBy(['brand' => $brandId]);
// OR
$repository->findByBrand($brandId);

will also work as the findBy method internals will be able to resolve that the given parameter is an ID of a Brand entity, thus it will produce the same query as if you had given the corresponding Brand instance.

like image 169
Alan T. Avatar answered Sep 29 '22 06:09

Alan T.


Based on your entity you should do the following.

$dbChoices = $this->getConfigurationPool()
        ->getContainer()
        ->get('Doctrine')
        ->getManager()
        ->getRepository('RibambelMainBundle:RubricMenu')
        ->findBy(array('brand' => 2));
like image 33
l.g.karolos Avatar answered Sep 29 '22 06:09

l.g.karolos