Symfony - How to use LIKE with COALESCE in a Doctrine request? - symfony

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();
}

Related

Foreach on getRepository Symfony 3

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 !

How QueryBuilder's select work?

I have a Category that has many Marker. I want to get a category by id with all the markers associated.
I have this query in my Marker's repository:
public function findOneByIdJoinedToCategory($id)
{
return $this->createQueryBuilder('m')
->innerjoin('m.category', 'c')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
}
It works as intended, i get the category's data with an array marker with all the marker data. But I don't want all the data field so I tested this query:
public function findOneByIdJoinedToCategory($id)
{
return $this->createQueryBuilder('m')
->select('m.id', 'm.name', 'm.description', 'm.vicinity', 'm.geo')
->innerjoin('m.category', 'c')
->addSelect('c.id', 'c.name')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getResult()
;
}
When I do this, I except to have only the field I selected, but I have an extra array and get this error : Call to a member function getCategory() on array
How do I select my field and get a category with all its marker ?
I think you need to use Doctrine partial objects here
So select will look something like this
->select('partial m.{id, name, description...

How to return my last count id queryBuilder

Hi i'm new on symfony be indulgent plz, here's my code
public function countTicket()
{
$qb = $this
->createQueryBuilder('t')
->innerJoin('t.visitors', 'v')
->Select("count(v.id)")
->where('t.visitDate IS NOT NULL')
->groupBy('t.visitDate')
;
return $qb
->getQuery()
->getScalarResult();
}
When i dump countTicket() it returns all my Id's entity, group in all visitDate which is normal but i just want to return the count (v.id) from the last visitDate i submit and i don't know how to do that.
Thanks for your help.
You should use getSingleScalarResult() instead of getScalarResult(), and limit your query to fetch the last result only, like this:
public function countTicket()
{
return $this
->createQueryBuilder('t')
->innerJoin('t.visitors', 'v')
->select('count(v.id)')
->where('t.visitDate IS NOT NULL')
->orderBy('t.visitDate', 'desc')
->groupBy('t.visitDate')
->setMaxResults(1)
->getQuery()
->getSingleScalarResult();
}

How to sum the contents of a document field with symfony query builder?

I am just trying to get the total value of one of the fields of my documents but it's not working. What am I missing?
#ProductRepository
public function countTotalValue()
{
return $this->createQueryBuilder('a')
->select('SUM(a.price)')
->getQuery()
->getSingleScalarResult()
;
}
I discovered it!
public function countTotalValue()
{
$array = $this->createQueryBuilder()
->select('price')
->hydrate(false)
->getQuery()
->execute()
->toArray()
;
$array = array_column($array, 'price');
return (array_sum($array));
}
It was returning a complex array, I had to stripe it before make the sum.

Doctrine 2: Separate chain calls in QueryBuilder

What is the approach to separate the querybuilder if I have some condition that I want to check.
What I want to get is something like this:
public function getProfilePhotoByUserId($userId, $checkApproved = false)
{
$profile = $this->createQueryBuilder('p')
->where('p.user = :userId')
->andWhere('p.profile = 1')
->setParameter('userId', $userId);
if($checkApproved) $profile->andWhere('p.approved = 1');
$profile->getQuery();
return $profile->getOneOrNullResult(Query::HYDRATE_ARRAY);
}
However this shows error on getOneOrNullResult:
"Method not found in querybuilder"
getQuery() return a Doctrine\ORM\Query object.
$query = $profile->getQuery();
return query->getOneOrNullResult(Query::HYDRATE_ARRAY);
or
return $profile->getQuery()->getOneOrNullResult(Query::HYDRATE_ARRAY);

Resources