質問

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.

役に立ちましたか?

解決

for those who may encounter this problem here goes my solution:

public function findById($adId) {
    $ad = $this->getEntityManager()
            ->createQuery("SELECT a, i from MarketBundle:Ad a LEFT JOIN a.images i with i.isVisible = :visible where a.id = :adId")
            ->setParameter("adId", $adId)
            ->setParameter("visible", true)->getSingleResult();

    return $ad;
}

他のヒント

You can do this with the default repository for your AdImage entity. Consider this example:

$entityManager = $this->getEntityManager();
// You said you already have the Ad entity instance you care about so just grabbing by id for demonstration purposes
$ad = $entityManager->getRepository('Ad')->findOneById(5);
$adImagesVisible = $entityManager->getRepository('AdImage')->findBy(array('isVisible' => true, 'ad' => $ad->getId()));

If you approach this from the entity on the "Many" side of the relationship you should be able to get what you want without having to write a custom DQL query.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top