Reputation: 420
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;
....
}
Upvotes: 0
Views: 57
Reputation: 1847
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());
Upvotes: 1