Hi i got following line:
$query = $qb->select($qb->expr()->substring("p.website",1,$qb->expr()->length("p.website")-4))
->from("AppBundle\Entity\Image" ,"p")
->getQuery();
and got following Exception:
Notice: Object of class Doctrine\ORM\Query\Expr\Func could not be converted to int
I understand why this Exception is thrown but how is it possible to get a substring based on the string size with Query Builder?
OK got it...
The expression $qb->expr()->length("p.website") is first converted into LENGTH("p.website")
So the -4 has to concat as string.
$qb->expr()->length("p.website") **.'-4'**
$query = $qb->select($qb->expr()->substring("p.website",1,$qb->expr()->length("p.website").'-4))
->from("AppBundle\Entity\Image" ,"p")
->getQuery();
In your EntityRepository:
public function getAll()
{
$qb = $this->createQueryBuilder('p');
$query = $qb
->select($qb->expr()->substring("p.website", 1, $qb->expr()->length("p.website").'-4'))
->getQuery();
return $query->getResult();
}
Related
I need help to change this code into a query builder in Symfony 5.
$em = $this->getEntityManager();
$query = "SELECT * FROM fizuser where roles::text LIKE :role";
$statement = $em->getConnection()->prepare($query);
$statement->bindValue('role', '%'.$role.'%');
$statement->execute();
$result = $statement->fetchAll();
return $result;
This is what I've tried:
function (UserRepository $er) {
return $er->createQueryBuilder('u')
->where("u.roles :: text like '%a%'");
However, I can't use "::" tag.
You can do it with -
return $this->createQueryBuilder('u')
->andWhere('u.roles LIKE :role')
->setParameter('role', '%'.'a'.'%')
->getQuery()
->getResult();
I'd like to add some details,
the column type in psql is json.
Using this type of query builder gives me error
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: json ~~ unknown
LINE 1: ...ted AS activated_25 FROM fizuser f0_ WHERE f0_.roles LIKE $1
I'm trying to do this SQL query with Doctrine QueryBuilder:
SELECT * FROM events WHERE NOT id in (SELECT event_id FROM ues WHERE user_id = $userID)
The UserEventStatus has foreign keys from User and event, as well as an integer for status.
I now want to query all events that dont have an entry in UserEventStatus from an particular User.
My function for this in the EventRepository looks like this:
public function getUnReactedEvents(int $userID){
$expr = $this->getEntityManager()->getExpressionBuilder();
$originalQuery = $this->createQueryBuilder('e');
$subquery= $this->createQueryBuilder('b');
$originalQuery->where(
$expr->not(
$expr->in(
'e.id',
$subquery
->select('ues.user')
->from('App/Entity/UserEventStatus', "ues")
->where(
$expr->eq('ues.user', $userID)
)
)
)
);
return $originalQuery->getQuery()->getResult();
}
But i get an error that says:
Error: Method Doctrine\Common\Collections\ArrayCollection::__toString() must not throw an exception, caught ErrorException: Catchable Fatal Error: Object of class Doctrine\ORM\EntityManager could not be converted to string (500 Internal Server Error)
Can anyone help me or point me to right point in the docs? Cause i failed to find something that describes my problem.
And another thing is, that I don't know if its possible, but it would be nice. Can I somehow make direct Object requests? I mean not with the string App/Entity/UserEventStatus but with something like UserEventStatus::class or something.
Thanks for your help in advance. :)
EDIT: It has to be $originalQuery->getQuery()->getResult() of course.
If its like it was with $subquery instead i recive [Semantical Error] line I0, col 41 near 'App/Entity/UserEventStatus': Error: Class 'App' is not defined. (500 Internal Server Error)
Second EDIT:
$expr = $this->getEntityManager()->getExpressionBuilder();
$queryBuilder = $this->createQueryBuilder('e');
$subquery= $this->createQueryBuilder('b')
->select('ues.user')
->from('UserEventStatus', "ues")
->add('where', $expr->eq('ues.user', $userID));
$originalQueryExpression = $expr->not($expr->in('e.id', $subquery));
$queryBuilder->add('where', $originalQueryExpression);
return $queryBuilder->getQuery()->getResult();
Third EDIT: Thanks to #Dilek I made it work with a JOIN. This is the final Query:
$queryBuilder = $this->createQueryBuilder('e')
->leftJoin('App\Entity\UserEventStatus', 'ues', 'WITH', 'ues.user=:userID')
->setParameter('userID', $userID)
->where($expr->orX($expr->not(
$expr->eq('e.id','ues.event')
),
$expr->not($expr->eq('ues.user', $userID)))
);
return $queryBuilder->getQuery()->getResult();
Building AND WHERE into a Query
public function search($term)
{
return $this->createQueryBuilder('cat')
->andWhere('cat.name = :searchTerm')
->setParameter('searchTerm', $term)
->getQuery()
->execute();
}
simple is: ->where('cat.name = :searchTerm')
UPDATE :
I think you need to use where in
$qb->add('where', $qb->expr()->in('ues.user', $userID));
And WHERE Or WHERE
I need to execute the following request to the MySQL server from symfony.
SELECT count(DISTINCT hotel_id) FROM symfony.search_result where request_id=#requrst_id
Code:
In controller:
$qwery2=$repository->hotelsCount($searchId);
Function:
public function hotelsCount($requestId){
$qb = $this->createQueryBuilder('self');
$qb->select('count(hotel_id)')
->where('self.request_id=:req_id')
->setParameter('self.req_id',$requestId)->getQuery()->getResult();
$rez=$qb->getQuery()->getSingleScalarResult();
var_dump($rez);
return $rez->fetchAll();
}
But i got error:
[Semantical Error] line 0, col 13 near 'hotel_id) FROM': Error: 'hotel_id' is not defined.
Table:
Use count(self.hotel_id) in your select
or count(self.hotelId). And in your where: self.reqId < check the entitys for that
I'm still new myself, so I admit I didn't completely follow your code, but try single quotes around 'hotel_id'
Try this, in this case, entity name is "SearchResult" and bundle name is "AppBundle", change it if is needed.
public function hotelsCount($requestId) {
$qb = $this->createQueryBuilder();
$qb->select('count(p.hotel_id)');
$qb->from('AppBundle:SearchResult', 't')
->where('p.request_id = :id')
->setParameter('id', $requestId);
$count = $qb->getQuery()->getSingleScalarResult();
return $count;
}
i have a leftjoin with doctrine querybuilder
$registros = $this->em->createQueryBuilder();
$registros->select('u,v')
->from('Entity\RegistroCam', 'u')
->leftjoin('Entity\CmMunicipios','v','WITH','u.camMunicipio = v.idMunicipio')
->orderBy('u.camPaterno', 'DESC');
$query = $registros->getQuery();
// Execute Query
$result = $query->getResult();
My problem is when I send the result to twig , it throws the following error
Twig_Error_Runtime: Method "idRegistro" for object "Entity\CmMunicipios" does not exist in "adminPanel.twig.html" at line 87
if i write
$registros->select('u')
works perfectly
I am trying to filter out admins that have a super admin role. Why is the following not working?
public function findAdmins()
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where($qb->expr()->notIn('ROLE_SUPER_ADMIN', 'a.roles'));
$result = $qb->getQuery()->execute();
return $result;
}
It will give me the following error:
[Syntax Error] line 0, col 68: Error: Expected Literal, got 'a'
The DQL-query looks like this:
SELECT a FROM MyBundle:Admin a WHERE ROLE_SUPER_ADMIN NOT IN(a.roles)
Role itself is not an entity. It is simply an array of strings.
$roles = array('ROLE_ADMIN', 'ROLE_SUPER_ADMIN)'
Try use MEMBER OF operator
$qb
->select('a')
->from('MyBundle:Admin', 'a')
->where(':role NOT MEMBER OF ad.roles')->setParamete('role', 'ROLE_SUPER_ADMIN');
Update.
The only solution found is to check serialized string in db with LIKE. Details here
Don't mix column name with table name
Try this,
$qb
->select('a')
->from('MyBundle:Admin', 'ad')
->where($qb->expr()->notIn('ROLE_SUPER_ADMIN', 'ad.roles'));
It should be like,
SELECT a FROM MyBundle:Admin ad WHERE ROLE_SUPER_ADMIN NOT IN(ad.roles)