Khadija Mne
Khadija Mne

Reputation: 31

FosUserBundle and doctrine request

When I execute this query

public function SearchByfournisseur($keyWord)
{
           $qb = $this->createQueryBuilder('p')->distinct()->select('p');  
           $qb ->leftJoin('p.Fournisseur', 'f'); 
                   $qb ->where($qb->expr()->eq('f.username', $keyWord)); 
                   $query = $qb->getQuery();      
           $products = $query->getResult();  
           return $products;

 }

I got this error:

[Semantical Error] line 0, col 110 near 'test': Error: 'test' is not defined.

But this one works perfectly:

public function SearchByfournisseur($keyWord)
{
           $qb = $this->createQueryBuilder('p')->distinct()->select('p');  
           $qb ->leftJoin('p.Fournisseur', 'f'); 
                   $qb ->where($qb->expr()->eq('f.id', $keyWord)); 
                   $query = $qb->getQuery();      
           $products = $query->getResult();  
           return $products;
 }

That means that I can't access to the user's attributes except his id.

Is there any away to solve this problem?

EDIT: This is the concerned entities :

    <?php
namespace Ecommerce\boutiqueBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FS\SolrBundle\Doctrine\Annotation as Solr;

/**
 * Ecommerce\boutiqueBundle\Entity\Produit
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Ecommerce\boutiqueBundle\Entity\ProduitRepository")
 */


 class Produit
{

    /**
     * @var integer $id
     *@Solr\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

     /**
     * @ORM\ManyToOne(targetEntity="Ecommerce\UserBundle\Entity\User",inversedBy="Produits")
     *
     * @ORM\JoinColumn(name="Fournisseur_id", referencedColumnName="id")
     */
    private $Fournisseur;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }



    /**
     * Set Fournisseur
     *
     * @param string $Fournisseur
     */
   public function setFournisseur( $Fournisseur)
    {
        $this->Fournisseur = $Fournisseur;
    } 

     /**
     * Get Fournisseur
     *
     * @return string 
     */
     public function getFournisseur()
    {
        return $this->Fournisseur;
    } 
    }

Fournisseur entity:

<?php
namespace Ecommerce\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Ecommerce_user")
*/
class User extends BaseUser
{
    public function __construct()
        {
        parent::__construct();
        $this->Produits = new \Doctrine\Common\Collections\ArrayCollection();

        }

    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

     /**
    * @ORM\OneToMany(targetEntity="Ecommerce\boutiqueBundle\Entity\Produit",
    * mappedBy="Fournisseur")
    */
    protected $Produits;


    public function addProduit(\Ecommerce\boutiqueBundle\Entity\Produit $Produit)
    {
    $this->Produits[] = $Produit;
    $Produit->setCategorie($this);
    }

    public function getProduits()
    {
        return $this->Produits;
    }

}

Upvotes: 1

Views: 339

Answers (1)

Florent
Florent

Reputation: 12420

You have to specify the join condition within the leftJoin() call.

public function SearchByfournisseur($keyWord)
{
    $qb = $this->createQueryBuilder('p')->distinct()->select('p');  
    $qb ->leftJoin('p.Fournisseur', 'f', 'WITH', $qb->expr()->eq('f.username = ?', $keyWord));   
    $products = $query->getResult();  
    return $products;
}

I suggest you to read the Doctrine documentation chapter dealing about the QueryBuilder.

Upvotes: 1

Related Questions