I’m pretty new to symfony/Doctrine and having some problems with querybuilder:
Given this ER:

And following declaration:
namespace xxx\SeoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* xxx\SeoBundle\Entity\Session
*
* @ORM\Table(name="session")
* @ORM\Entity
*/
class Session
{
const repositoryName = "InternetSmSeoBundle:Session";
/**
* @var string $id
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
*/
private $id;
......
/**
* @var Gsite
*
* @ORM\ManyToOne(targetEntity="Gsite")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="gsite_id", referencedColumnName="id")
* })
*/
private $site;
......
}
I need to find sessions which filtering them by site.
I’ve tried following approach:
$rep = $this->em->getRepository(Session::repositoryName);
$qb = $rep->createQueryBuilder("s");
$qb->setMaxResults(200);
$qb->where("1=1");
$qb->orderBy("time", "desc");
//site
if ($params->site != null){
/** @var Gsite **/
$site = $params->site;
$qb->where($qb->expr()->eq("gsite_id",$site->getId()));
}
Or even
$qb->where($qb->expr()->eq("site",$site));
But it doesn’t work. What is correct way to filter data in presence of Many To One foreign keys? Do I need to create declaration of gsite_id column in my Model?
Thanks.
Set the parameter, Doctrine will be able to infer the type (no need to use the foreign key
id):