0

I have an entity offer which has a oneToOne relation with a product and I have a user who would like to get all the offers he got on his products.

I would like to access to the user who has made the product from an offer in my SQL request.

So it's something like that:

$user = $this->getUser();
$listofofferusergot = $em->getRepository('blabla:Offer')->findBy(array('product.autor.id' => $user->getId()));

(ps : Offer has a OneToOne relation with product) (ps2: the thing I wrote doesn't work )

So the question is general Can I simply access to a subfield (like id in my case) or must I do a $em->createQuery() stuff

offer class:

<?php

namespace Nemi\TwigBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Offer
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Nemi\TwigBundle\Entity\OfferRepository")
 */
class Offer
{
     /**
     * @ORM\ManyToOne(targetEntity="Nemi\TwigBundle\Entity\Product")
     * @ORM\JoinColumn(nullable=false)
     */
     private $product;

     ...
}

for the product class:

<?php

    namespace Nemi\TwigBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * Product
     *
     * @ORM\Table()
     * @ORM\Entity(repositoryClass="Nemi\TwigBundle\Entity\ProductRepository")
     */
    class Product
    {
        /**
         * @ORM\ManyToOne(targetEntity="Nemi\UserBundle\Entity\User")
         * @ORM\JoinColumn(nullable=false)
         */
        private $autor;

        ....

     }
1
  • Can I see your entity code? Commented Oct 21, 2014 at 12:33

1 Answer 1

1

You could add this method in your OfferRepository:

public function findOffersByProductAuthor(User $user)
{
    return $this->createQueryBuilder('offer')
         ->join('offer.product', 'product')
         ->join('product.author', 'author')
         ->where('author = :user')
         ->setParameter('user', $user)
         ->getQuery()
         ->getResults();
}

Then, call:

$em->getRepository('blabla:Offer')-> findOffersByProductAuthor($this->getUser());
Sign up to request clarification or add additional context in comments.

3 Comments

I would name the function "findOffersByProductAuthor" since the offer also has user
alright thx it's working ! but so there is no way to do the same just using findBy() ?
The findBy is just a shortcut, you can filter over properties of the entity you are requesting, but you cannot use joins, that are required in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.