Doctrine and Like query symfony2 - symfony

I have a search bar in my page and the action in my in charge of looking for what the user search for is this :
public function searchAction(Request $request){
$em = $this->container->get('doctrine')->getEntityManager();
$evenements= $em->getRepository('Mql14mqlmeBundle:Evenement')->findAll();
if ('POST' === $request->getMethod()) {
$search = $request->get('search');
$query = $this->container->get('doctrine')->getEntityManager()->createQuery( 'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :search')
->setParameter('search', $search);
$resultats = $query->getResult();
return $this->container->get('templating')->renderResponse('Mql14mqlmeBundle:Event:search.html.twig', array(
'resultats'=>$resultats,
));
}
return $this->listerAction();
}
It's working if the user put the exact name of some event in the database, but I want to make the search possible even if it's only a part of the name, I tried this in the query:
$query = $this->container->get('doctrine')->getEntityManager()->createQuery( 'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :%search%')
->setParameter('search', $search);
But I'm getting this error: Invalid parameter format, : given, but :name or ?num expected.

Try to change parameter like this:
$query = $this
->container
->get('doctrine')
->getEntityManager()
->createQuery(
'SELECT e FROM Mql14mqlmeBundle:Evenement e WHERE e.nom LIKE :search'
)
->setParameter('search', '%'.$search.'%');

Related

doctrine pagination with join statement

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

Symfony 3- select query

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

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

is it possible to integrate in a doctrine query a if statement?

I have so far 2 queries which are similar:
the first one I use when user is ROLE_REDAC
$qb = $this->createQueryBuilder('a');
$qb->LeftJoin('a.ArticlePhrases','ap')
->addSelect('ap')
->where(
$qb->expr()->andX(
$qb->expr()->eq('ap.order', '?1'),
$qb->expr()->eq('a.author', '?2')
)
)
->setParameters(//...
);
The second one when user is ROLE_ADMIN
$qb = $this->createQueryBuilder('a');
$qb->LeftJoin('a.ArticlePhrases','ap')
->addSelect('ap')
->where(
$qb->expr()->andX(
$qb->expr()->eq('ap.order', '?1')
)
)
->setParameters(...
));
There are not much difference. is it possible to call from the controller the same repository fonction and that this fonction adapt whether the actual user is ROLE_REDAC or ROLE_ADM? if yes how can I do that?
I haven't tested it, but should work
class MyCustomRepository extends EntityRepository
{
public function myCustomFunction($user_role)
{
$qb = $this->createQueryBuilder('a');
if ($user_role == 'ROLE_REDAC') {
$where_dql_expr = $qb->expr()->andX(
$qb->expr()->eq('ap.order', '?1'),
$qb->expr()->eq('a.author', '?2')
);
$qb->setParameters(//....);
}
else if ($user_role == 'ROLE_ADMIN') {
$where_dql_expr = $qb->expr()->andX(
$qb->expr()->eq('ap.order', '?1')
);
$qb->setParameters(//....);
}
$qb->LeftJoin('a.ArticlePhrases','ap')
->addSelect('ap')
->where($where_dql_expr)
->//and so on
}
}
of course into your controller you have to call it passing the right parameter
$role = //retrieve role
$custom_repo = $this->getDoctrine()
->getManager()
->getRepository('YourBundleName:MyCustomEntity');
$custom_repo->myCustomFunction($role);

No value(s) from doctrine query

I am not retrieving any values with a simple Doctrine PHPCR query I am executing.
In this piece of code I get all the values from field 'name':
public function getPagesAction()
{
$dm = $this->get('doctrine_phpcr.odm.default_document_manager');
$qb = $dm->createQueryBuilder();
$qb->from()->document('Foo\BarBundle\Document\StandardPage', 'p');
return $this->render(
'FooBarBundle:Toolbar:navigation.html.twig',
array('names' => $qb->getQuery()->execute())
);
}
output: home, mypage, testpage, foo
And with this code I get nothing in return:
public function getPagesAction()
{
$dm = $this->get('doctrine_phpcr.odm.default_document_manager');
$qb = $dm->createQueryBuilder();
$qb->from()->document('Foo\BarBundle\Document\StandardPage', 'p');
$qb->where()->like()->field('p.name')->literal('mypage');
return $this->render(
'FooBarBundle:Toolbar:navigation.html.twig',
array('names' => $qb->getQuery()->execute())
);
}
I am also not receiving any errors. My HTML is plain empty. The query does work when I replace 'p.name' with 'p.title'.
Here is my StandardPage.php on Gist

Resources