I'm working on a symfony project entity with query builder. When I try to run this function I get this issue.
[Semantical Error] line 0, col 9 near 'category FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.
public function json_filterAllproductsAction() {
$search = "";
$category = 1;
//Combine tables and create the query with querybuilder
$em = $this->container->get('doctrine.orm.entity_manager');
$qb = $em->createQueryBuilder();
$qb->select('p.category')
->from('EagleAdminBundle:Products', 'p')
->orderBy('p.id', 'DESC');
if ($category != 0) {
$qb->andWhere('p.category = :category')
->setParameter('category', $category);
}
$qb->andWhere('p.productTitle LIKE :title')
->setParameter('title', "$search%");
//convert to json using "JMSSerializerBundle"
$serializer = $this->container->get('serializer');
$jsonproducts = $serializer->serialize($qb->getQuery()->getResult(), 'json');
return new Response($jsonproducts);
}
I think error is in,
$qb->select('p.category')
It would be great help someone can help me.
You need to fetch category as well in your join. Something like this should work fine:
$qb->select('p', 'c')
->from('EagleAdminBundle:Products', 'p')
->orderBy('p.id', 'DESC')
->join('p.category', 'c');
if ($category != 0) {
$qb->andWhere('p.category = :category')
->setParameter('category', $category);
}
$qb->andWhere('p.productTitle LIKE :title')
->setParameter('title', "$search%");
Note if you don't want to limit your search to only products that have categories you can change the join to a leftJoin.
Also note you can have the serializer configured to serialize the category property of product. Then you should just be able to fetch a product and have it automatically serialize the category for you.
Related
I have some trouble with LIKE and COALESCE in a Doctrine request (not 100% sure that the trouble is there).
I would like to search inside a database with a filter that match only if exist, and only with a part of the value (for example find 'abc' with the filter set to 'ab').
This request works fine :
public function findUsers($entreprise, $filtres)
{
$filtre_name = $filtres['name'];
return $this->createQueryBuilder('users')
->where('users.entreprise = :entreprise')
->andWhere('users.name = COALESCE(:filtre_name, users.name)')
->setParameter('entreprise', $entreprise)
->setParameter('filtre_name', $filtre_name)
->orderBy('users.name', 'ASC')
->getQuery()
->getResult();
}
It return all the users of the company "entreprise" where "filtre_name" match (if not null) with "name" in the database. (If "filtre_name" is null, then the where match for all the database thanks to COALESCE).
I would like now to do the same thing but with "LIKE" instead of "=" because for now the name has to match perfectly and I would like a match for "abc" with only "ab" inside the filter for example.
public function findUsers($entreprise, $filtres)
{
$filtre_name = $filtres['name'];
return $this->createQueryBuilder('users')
->where('users.entreprise = :entreprise')
->andWhere('users.name LIKE COALESCE(:filtre_name, users.name)')
->setParameter('entreprise', $entreprise)
->setParameter('filtre_name', '%'.$filtre_name.'%')
->orderBy('users.name', 'ASC')
->getQuery()
->getResult();
}
The result is an error : "Warning: Undefined property: Doctrine\ORM\Query\AST\CoalesceExpression::$type".
I have find a solution. I build my queryBuilder with parameter only if they are not Null. So I remove COALESCE and now I can use LIKE.
public function findUsers($entreprise, $filtres)
{
$filtre_nom = $filtres['nom'];
$filtre_gestionnaire = $filtres['gestionnaire'];
$qb = $this ->createQueryBuilder('users');
$qb ->where('users.entreprise = :entreprise')
->setParameter('entreprise', $entreprise);
if ($filtre_nom != Null) {
$qb ->andWhere('users.nom LIKE :filtre_nom')
->setParameter('filtre_nom', '%'.$filtre_nom.'%');
}
if ($filtre_gestionnaire != Null) {
$qb ->andWhere('users.gestionnaire LIKE :filtre_gestionnaire')
->setParameter('filtre_gestionnaire', '%'.$filtre_gestionnaire.'%');
}
$qb->addorderBy('users.nom', 'ASC');
return $qb ->getQuery()
->getResult();
}
In my table I have: 'id, message, user(from User::class, foreign key) etc...'
How can I select specific fields ? I would like fetch only message and username, how can I do ?
I tried this
public function getNameAndMessage()
{
return $this->createQueryBuilder('c')
->select('c.message, c.user.username')
->orderBy('c.id', 'ASC')
->setFirstResult(5)
->getQuery()
->getResult()
;
}
But an error appear: [Semantical Error] line 0, col 25 near 'username FROM': Error: Class App\Entity\Comment has no field or association named user.username
Someone can help me please
To access the properties of the User entity, you must join it to your query like this:
public function getNameAndMessage(){
return $this->createQueryBuilder('c')
->select('c.message, u.username')
->leftJoin('c.user','u')
->orderBy('c.id', 'ASC')
->getQuery()
->getResult();
}
On my controler I get some results from database
$commandes = $em->getRepository('SWPlatformBundle:Vente')->getCommandesAFacturer($client[0]->getId());
with this SQL
public function getCommandesAFacturer($id) {
$qb = $this->createQueryBuilder('v')
->select("c.id, c.montantTTC, c.horodatageCreation, SUM(v.prixTotalVente) AS sansFrais")
->leftJoin('v.commande', 'c')
->where('c.facture IS NULL')
->andwhere('c.commercant = :commercantId')
->setParameter('commercantId', $id)
->groupBy('c.id');
return $qb
->getQuery()
->getResult();
}
And I would like to sum the sansFrais value of my request but I don't reach the sansFrais Value.
I've tried like this, but it doesn't work :
foreach($commandes as $commande){
$montantHTCollecte += $commande['sansFrais'];
}
Thanks for your help !
I am trying to right a query which return me list of the users which has uploaded video(user_id in video table ), and have paginating thingy in the query
the function is like this :
public function getUsersHasVideoShoutOut($offset, $limit)
{
$qb = $this->createQueryBuilder('u')
->Join('u.video', 'uv');
$qb->where('uv.muted=0')
->andwhere('u.muted = 0')
->addOrderBy('uv.release_date', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit);
return $qb->getQuery()->getResult();
}
but the problem is that I get duplicate data in next pages , is it because of join statement and pagination in doctorine ?
You can get distinct data:
public function getUsersHasVideoShoutOut($offset, $limit)
{
$qb = $this->createQueryBuilder('u')
->Join('u.video', 'uv');
$qb->where('uv.muted=0')
->andwhere('u.muted = 0')
->addOrderBy('uv.release_date', 'DESC')
->setFirstResult($offset)
->setMaxResults($limit)
->distinct();
return $qb->getQuery()->getResult();
}
I'm new to symfony and I'm having a hard time developing a query ... thanks if anyone could help me
What I need is
select id and code from table where id = userid and code = usercode
if the values are equal if statement to insert data into the database, otherwise error message
i try this
$teste = $this->getDoctrine()->getRepository('AppBundle:Codigo');
$query = $teste->createQueryBuilder('p')
->where('p.codigo = :codigo')
->andWhere('p.utilizadorID = :user')
->setParameter('codigo', $codigo)
->setParameter('user', $user)
->getQuery();
}
$cod = $query->getResult();
if ( $cod <> NULL) {
$link = $data['linkpodcast'];
$podcast->setUrl($link);
$podcast->setIdUser($user);
$sn = $this->getDoctrine()->getManager();
$sn -> persist($podcast);
$sn -> flush();
$this->addFlash(
'notice',
'Podcast Adicionado'
);
return $this->redirectToRoute('gravar_list');
}