i'm trying to load an entity passing a parameter to a foreign key which is the inverse side
i have this two entities
Ad
/**
* Ad
*
* @ORM\Table(name="Ad")
* @ORM\Entity(repositoryClass="Symarket\MarketBundle\Repository\AdRepository")
*/
class Ad
{
/**
* @var AdImage
*
* @ORM\OneToMany(targetEntity="AdImage", mappedBy="ad", cascade={"persist", "merge"})
*/
private $images;
//...
}
AdImage
/**
* AdImage
*
* @ORM\Table(name="AdImage")
* @ORM\Entity(repositoryClass="Symarket\MarketBundle\Repository\AdImageRepository")
*/
class AdImage {
/**
* @var boolean
*
* @ORM\Column(name="adi_is_visible", type="boolean")
*/
protected $isVisible;
/**
* @var Ad
*
* @ORM\ManyToOne(targetEntity="Ad", inversedBy="images", cascade={"persist"})
* @ORM\JoinColumn(name="adi_ad_id", referencedColumnName="ad_id")
*/
protected $ad;
}
now, by getting a Ad
from the database i want to get ONLY the images which are "isVisibile" => true
how is this possible with queryBuilder?
what i tried so far with the findBy function is this
public function findById($adId) {
$res = $this->findBy(array("id" => $adId, "isVisible" => true, "images" => array("isVisible" => true)));
$ad = reset($res);
return $ad;
}
and i got this error
You cannot search for the association field 'Symarket\MarketBundle\Entity\Ad#images', because it is the inverse side of an association. Find methods only work on owning side associations.
then i tried this way with the querybuilder
public function findById($adId) {
$res = $query = $this->createQueryBuilder('ad')
->leftJoin('ad.images', 'img')
->where('img.isVisible = :adVisible')
->andWhere('ad.id = :id')
->setParameter('adId', $adId)
->setParameter('imgVisible', true)
->getQuery();
$ad = reset($res);
return $ad;
}
and i get NULL
Thanks in advance.