I am using symfony3:
I want use like in my query.
$repository = $this->getDoctrine()->getRepository('AppBundle:ABC');
echo $searchstring = "LIKE '%".$searchtxt."%'"; die;
$res=$repository->findBy(array('fname' => $searchstring));
$normalizer = new ObjectNormalizer();
$encoder = new JsonEncoder();
$serializer = new Serializer(array($normalizer), array($encoder));
$response=$serializer->serialize($res, 'json'); // Output: {"name":"foo","sportsman":false}
return new Response($response);
Its return null
Anyone can suggest me ?
The find*() methods of Doctrine repositories don't allow to perform queries using LIKE for comparison. You need to write a custom DQL query (see Querying for Objects with DQL and Doctrine Query Language):
$query = $em->createQuery("SELECT abc FROM AppBundle:ABC abc WHERE fname LIKE :fname");
$query->setParameter('fname', 'LIKE "%'.$searchtxt.'%"');
$result = $query->getResult();
Related
In symfony4 i'm using the iteration solution as explained in doctrine documentation
The problem is that i need to perform a query inside the foreach cicle and if i do, the iteration ends.
//...
$repo = this->_em->getRepository('App\Entity\EmailAddressStatus');
// Gets 15k results
$q = $this->_em->createQuery('select u from App\Entity\User u');
$iterableResult = $q->iterate();
foreach ($iterableResult as $row) {
$emails = $row[0]->getEmails();
foreach($emails as $email){
// If i do this the iteration ends after first result
$check = $repo->isEmailBlackListed($email);
// Do something with $check and $email...
}
// detach from Doctrine, so that it can be Garbage-Collected immediately
$this->_em->detach($row[0]);
}
You should try to select the users with JOIN to EmailAddressStatus so that you get only valid users like this:
$this->_em->createQuery('select u From App\Entity\User u JOIN App\Entity\EmailAddressStatus s WITH s.email = u.email AND s.status <> ?1')
->setParameter(1, 'not-allowed-status');
I use Doctrine for my CocuhDb
I retrieve My Object by
$dm = $this->container->get('doctrine_couchdb.odm.default_document_manager');
$obj = $dm->getRepository("myEntity")->findOneBy(array('title' => 'foo));
I tried with (pseudo-code)
for {
.....
$dm->persist($obj);
$uow = $dm->getUnitOfWork();
$changeset = $uow->getDocumentRevision($obj);
}
$dm->flush()
but my $obj has the old _rev.
I want to check if my document change _rev.
How Can I retrieve that information ?
I am to delete a row based on ID, and i used this query to do the task:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('DELETE buss from StreetBumbApiBundle:BussOwner buss WHERE buss.id = :bussId')
->setParameter("bussId", $bussId);
$list = $query->getResult();
but i am getting this error:
{
"code": 500,
"message": "[Semantical Error] line 0, col 7 near 'buss from StreetBumbApiBundle:BussOwner': Error: Class 'buss' is not defined."
}
What is wrong i am doing?
You could solve this easily by using the QueryBuilder:
$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$query = $qb->delete('StreetBumbApiBundle:BussOwner', 'buss')
->where('buss.id = :bussId')
->setParameter('bussId', "bussId")
->getQuery();
$query->execute();
BTW: If you replace getQuery with getDQL you can see how this translates into DQL (or getSQL)
What essentially is wrong in your method is the from in your Delete statement as delete in DQL looks like this:
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'DELETE StreetBumbApiBundle:BussOwner buss
WHERE buss.id = :bussId')
->setParameter("bussId", $bussId);
$query->execute();
Also, I am not sure why you use $query->getResult().
What would be the expected result of a delete? $query->execute(); does the job.
Apart from that, if you don't do complex delete statements, and in your case you are deleting an Entity querying with an Id.
Why not:
$em = $this->getDoctrine()->getManager();
$em->remove($entity);
And therefore pass the Entity, not just the Id to your delete function?
Alternative solution: your DQL is not a good one, try with this
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'DELETE
StreetBumbApiBundle:BussOwner buss
WHERE
buss.id = :bussId'
)
->setParameter("bussId", $bussId);
$result = $query->execute();
I have an application with symfony2 / doctrine 2 / elastica / fosElasticaBundle / pagerFanta.
I want to use a custom and dynamic queryBuilder in combination with pagerfanta and elastica. Not to transform the results but to prefilter them.
So far I was able to :
1. Use pagerfanta by itself with my custom queryBuilder :
$page = $request->get('page', 1);
$search = $request->get('search');
$querybuilder = $this->getDoctrine()->getRepository('AppBundle:FoodAnalytics\Recipe')->findByTopCategoryQueryBuilder($category);
$explorerManager = $this->get('explorer_manager');
$pagerFanta = $explorerManager->getPagerFanta($querybuilder, $page, 4);
$recipes = $pagerFanta->getCurrentPageResults();
Use Elastica with Pagerfanta but without my custom QueryBuilder :
$page = $request->get('page', 1);
$search = $request->get('search');
$finder = $this->container->get('fos_elastica.finder.website.recipe');
$pagerFanta = $finder->findPaginated($search);
$recipes = $pagerFanta->getCurrentPageResults();
Now, how can I also use my custom QueryBuilder ? I know you can set a custom one in elastica config but mine has to be dynamic = take an argument, so I'd like to set it in the controller. Is that possible ?
You can pass custom Elastica\Query to findPaginated method:
$query = new \Elastica\Query::create(new \Elastica\Query\Term(array(
'name' => $request->get('search')
));
$finder = $this->container->get('fos_elastica.finder.website.recipe');
$pagerFanta = $finder->findPaginated($query);
$recipes = $pagerFanta->getCurrentPageResults();
Maybe code is more clear and you can see how it works when you use just PagerFanta with ElasticaAdapter without FOSElasticaBundle:
// Searchable can be any valid searchable Elastica object. For example a Type or Index
$finder = new \Elastica\Index($elasticaClient, 'website.recipe');
// A Query can be any valid Elastica query (json, array, Query object)
$query = new \Elastica\Query::create(new \Elastica\Query\Term(array(
'name' => $request->get('search')
));
$elasticaAdapter = new \Pagerfanta\Adapter\ElasticaAdapter($finder, $query);
$paginator = new \Pagerfanta\Pagerfanta($elasticaAdapter);
$results = $paginator
->setMaxPerPage($limit)
->setCurrentPage($page)
->getCurrentPageResults();
I need to be able to use the following WHERE clause in Doctrine:
WHERE AL.UserID = 41 AND (TheDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 WEEK) AND CURDATE())
and what I'm currently doing is:
$results = $em->getRepository('MyBundle:MyTable')->findOneBy(array('userId' => $userId));
However, I haven't been able to filter results by the last 2 weeks without writing SQL or DQL.
Is there any way I can achieve this through Doctrine's methods?
You can use QueryBuilder. Assuming you are using MySQL BETWEEN, something like:
<?php
// $em instanceof EntityManager
$qb = $em->createQueryBuilder();
$now = new DateTime();
$qb->select('c')
->from('MyClass', 'c')
->where($qb->expr()->andX(
$qb->expr()->eq('c.UserID', '?1'),
$qb->expr()->gte('c.TheDate', '?2'),
$qb->expr()->lte('c.TheDate', '?3')
))
->setParameter(1, 41)
->setParameter(2, $now.sub(new DateInterval('P2W')))
->setParameter(3, $now);
$query = $qb->getQuery();
$c = $query->getSingleResult();
This is cleaner code and should work across the different database platforms supported by Doctrine2 ORM.