Doctrine Symfony Finding if it's in database with an array - symfony

I'm a student and, for my final project, I need to make a searchtool.
I know how to search if a string is contained in my database but I want to search with an array now. So I do it and that's work... but only if it's exactly what it's in my array, not if it's contained.
So my Repository function is like this
public function findByMots($value)
{
return $this->createQueryBuilder('a')
->andWhere('a.titre IN (:val) OR a.contenu IN (:val) OR t.theme IN (:val)')
->setParameter('val', $value)
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id')
->orderBy('a.id', 'DESC')
->getQuery()
->getResult()
;
}
My value is an array with string in, but if one string is "raccoon" he doesn't find my article who has a title like "I love raccoon". Does anybody know please?
I found a method!
public function findByMots($value)
{
$search = $this->createQueryBuilder('a')
->leftJoin('App\Entity\Theme', 't', 'WITH', 't.id_article = a.id');
$number = 0;
foreach ($value as $valu) {
$search = $search->orWhere('a.titre LIKE :val' . $number . ' OR a.contenu LIKE :val' . $number . ' OR t.theme LIKE :val' . $number . '')
->setParameter('val'.$number, '%'.$valu.'%');
$number++;
}
$search = $search
->orderBy('a.id', 'DESC')
->getQuery()
->getResult();
return $search;
}

Related

Drupal 8 - Get All Nodes With a Part of Url Alias

I try to find a way to get all nodes by a part of the url alias in the nodes. I know i get a specific node by the complete url alias. For example:
$path = \Drupal::service('path.alias_manager')->getPathByAlias('/this-is-the-alias');
if(preg_match('/node\/(\d+)/', $path, $matches)) {
$node = \Drupal\node\Entity\Node::load($matches[1]);
}
And I know I can find multiple nodes by entity query:
$nids = \Drupal::entityQuery('node')
->condition('type','page')
->condition('status',1)
->sort('created', 'DESC')
->range(0, 20)
->execute();
But I want to know how I can implement an additional condition to filter nodes by a part of the URL alias for example "/news/*". I need all nodes with this url fragment.
Can try this-
Reference https://www.jonkamke.com/blog/2019/5/load-a-node-via-url-alias
<?php
/** #var Drupal\Core\Url $url */
$url = $variables['url'];
if ($url instanceof Drupal\Core\Url) {
$nid = $url->getRouteParameters()['node'];
/** #var \Drupal\node\Entity\Node $node */
$node = Node::load($nid);
if ($node instanceof Node) {
$type = $node->getType();
}
}
$input = '/this-is-the-alias';
$node_query_result = [];
$path_query = \Drupal::database()->select('path_alias', 'a');
$path_query->addField('a', 'path');
$path_query->condition('a.alias', '%' . $input . '%', 'LIKE');
$path_query_result = str_replace('/node/', '', $path_query->execute()->fetchCol());
if ($path_query_result) {
$node_query = \Drupal::database()->select('node_field_data', 'n');
$node_query->addField('n', 'nid');
$node_query->addField('n', 'type');
$node_query->addField('n', 'title');
$node_query->condition('n.status', NodeInterface::PUBLISHED);
$node_query->condition('nid', $path_query_result, 'IN');
$node_query_result = $node_query->execute()->fetchAll(\PDO::FETCH_ASSOC);
}
return $node_query_result;

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 !

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

Real dynamic DQL with doctrine in symfony 3.4

I'm trying to achieve a dynamic query with DQL in doctrine. I've checked several post about this subject but all the solutions are static. I want to achieve somethin like this:
$qb->where(
$qb->expr()->orX(
$qb->expr()->like('e.cliente', ':cliente_tag'),
$qb->expr()->like('e.cliente', ':cliente_tag2'),
$qb->expr()->like('e.cliente', ':cliente_tag3')
),
$qb->expr()->orX(
$qb->expr()->like('e.apoderado', ':apoderado_tag'),
$qb->expr()->like('e.apoderado', ':apoderado_tag2'),
$qb->expr()->like('e.apoderado', ':apoderado_tag3')
)
);
but inside a loop like this:
foreach ($options['camposTexto'] as $i => $campoTexto) {
switch ($campoTexto['appliedTo']) {
case 'apoderado': {
$exp = [];
foreach ($campoTexto['tags'] as $tag) {
$exp[] = $qb->expr()->like('e.apoderado', ':apoderado_tag' . $i);
$parameters['apoderado_tag' . $i] = '%' . $tag . '%';
}
if ($isFirst) {
$isFirst = false;
$qb->where($qb->expr()->orX($exp));
} else {
$qb->andWhere($qb->expr()->orX($exp));
}
break;
}
case 'cliente': {
$exp = [];
foreach ($campoTexto['tags'] as $tag) {
$expresiones[] = $qb->expr()->like('e.cliente', ':cliente_tag' . $i);
$parameters['cliente_tag' . $i] = '%' . $tag . '%';
}
if ($isFirst) {
$isFirst = false;
$qb->where($qb->expr()->orX($exp));
} else {
$qb->andWhere($qb->expr()->orX($exp));
}
break;
}
}
}
tags is an array of strings. As you see I passed the array of expresions but doctrine throws me an Exception.
So far so now I have not found any solution to my problem.
Any Idea?
Thanks in advance!
Looking at this post I figured out the solution. It would be something like this:
case 'apoderado': {
$orX = $qb->expr()->orX();
foreach ($campoTexto['tags'] as $y => $tag) {
$orX->add($qb->expr()->like('e.apoderado', $qb->expr()->literal('%' . $tag . '%'))); //<= with literal because I can't set the parameters later in the qb
}
$expresiones[] = $orX;
break;
}
after all the case/break
$andX = $qb->expr()->andX();
$qb->where($andX->addMultiple($expresiones));
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');
}

Resources