Operator "NOT LIKE" not work correctly - symfony

i am creating a new query in my app.
I use filters, with different options (Contain, equal or different).
If I use operator "different", my query generate like this:
SELECT p FROM ProductosBundle:Producto p
LEFT JOIN p.tipo t
LEFT JOIN p.departamento d
WHERE 1=1 AND t.nombre NOT LIKE '%articulo%'
I have product different from "articulo", but return 0 results...
I tried with NOT LIKE, != and <>, but not return results.
Is there something wrong in my condition where? I do not see the problem, with contain and equal it works.
Any idea? Thanks
EDIT 1:
This is my controller:
...
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT p FROM ProductosBundle:Producto p LEFT JOIN p.tipo t LEFT JOIN p.departamento d";
if (isset($_GET['filterField']) && isset($_GET['filterValue'])){
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
$dql .= " WHERE 1=1";
for($i = 0; $i < count($valores); $i++){
$dql.= " AND ". $filas[$i]. " " . $operadores[$i] ;
if($operadores[$i] == "LIKE" OR $operadores[$i] == "NOT LIKE"){
$dql .= " '%".$valores[$i]."%'";
}else{
$dql .= " '".$valores[$i]."'";
}
}
}
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$productos = $paginator->paginate(
$query,
$request->query->get('page', 1),
25
);
EDIT2:
My profiler is executing this:
SELECT DISTINCT p0_.id AS id_0
FROM producto p0_
LEFT JOIN tipo t1_ ON p0_.tipo = t1_.id
LEFT JOIN departamento d2_ ON p0_.departamento = d2_.id
WHERE t1_.nombre LIKE 'articulo'
AND 1 = 1
AND t1_.nombre NOT LIKE '%articulo%'
LIMIT 25 OFFSET 0
Its a LIKE 'Articulo'... WHY???!!!

Presumably, there's an error in your UI logic / the code setting the query parameters. This:
$valores = explode(",",$_GET['filterValue']);
$filas = explode(",",$_GET['filterField']);
$operadores = explode(",",$_GET['filterOperator']);
generates both
t1_.nombre LIKE 'articulo'
and
t1_.nombre NOT LIKE '%articulo%'
so you should take a look at the code that populates filterValue, filterField and filterOperator.

Related

HOW MAKE A update WITH left join DOCTRINE

i want to do this in doctrine symonfy2
update producto p
left join producto_compra pc ON p.id = pc.id_producto
set p.cantidad = (p.cantidad - pc.cantidad)
where pc.id_compra = '56';
and i tried with this, and it doesnt work
$em = $this->getEntityManager();
$query = $em->createQuery('UPDATE ORM\Entity\Producto p '
. 'LEFT JOIN ORM\Entity\ProductoCompra pc WITH p.id = pc.idProducto '
. 'SET p.cantidad = (p.cantidad - pc.cantidad) '
. 'WHERE pc.idCompra = :fc ')
->setParameter('fc', $facturacompra);
similar question look at here, i see that join is not supported in update or delete queries.

Query filter assign sub query to one field value

I have stuck in this issue from the last two days.
Please help.
I want to assign the following query .
$qr = "( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
to
one view's field value.
I have used the following code.
function general_views_query_alter(&$view, &$query) {
$qr = " ( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = 'node' AND subconfigcode.deleted = '0') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = '1') AND (node.type IN ('offering')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0)";
$query->add_where(1, "node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value", $qr);
}
But, it is returning the following where query.
where node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value = '( select subconfigcode.field_subconfigcode_value AS configcode FROM node node LEFT JOIN field_data_field_select_parent_configuratio select_parent_configuratio ON node.nid = select_parent_configuratio.entity_id AND (select_parent_configuratio.entity_type = node AND select_parent_configuratio.deleted = 0) LEFT JOIN node select_parent_configuratio_node ON select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid LEFT JOIN field_data_field_subconfigcode subconfigcode ON select_parent_configuratio_node.nid = subconfigcode.entity_id AND (subconfigcode.entity_type = \'node\' AND subconfigcode.deleted = \'0\') WHERE (( (select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid) )AND(( (node.status = \'1\') AND (node.type IN (\'offering\')) ))) ORDER BY node.created DESC, configcode ASC LIMIT 1 OFFSET 0) ')
I want to get query without the single quote assigned for sub query and remove extra slashed added to each value.
Please help.
Answer :
Direct query is not applicable for assignment purpose in the add_where condition.
For that, I have generated the query object using.
function general_views_query_alter(&$view, &$query) {
$subQuery = db_select('node', 'node'); $subQuery->leftJoin('field_data_field_select_parent_configuratio', 'select_parent_configuratio ', 'node.nid = select_parent_configuratio .entity_id');
$subQuery->condition('select_parent_configuratio.entity_type', 'node', '=');
$subQuery->condition('select_parent_configuratio.deleted', '0', '=');
$subQuery->leftJoin('node', 'select_parent_configuratio_node', 'select_parent_configuratio.field_select_parent_configuratio_nid = select_parent_configuratio_node.nid');
$subQuery->leftJoin('field_data_field_subconfigcode', 'subconfigcode', 'select_parent_configuratio_node.nid = subconfigcode.entity_id');
$subQuery->condition('subconfigcode.entity_type', 'node', '=');
$subQuery->condition('subconfigcode.deleted', '0', '=');
$subQuery->where("select_parent_configuratio.field_select_parent_configuratio_nid = node_field_data_field_select_parent_configuratio.nid");
$subQuery->condition('node.status', "1", '=');
$subQuery->condition('node.type', array('offering'), 'IN');
$subQuery->orderBy('configcode');
$subQuery->addField('subconfigcode', 'field_subconfigcode_value', 'configcode');
//$subQuery->range(0, 1);
$query->add_where($group,'node_field_data_field_select_parent_configuratio__field_data_field_subconfigcode.field_subconfigcode_value',$subQuery,'in');
}
Using this, I am able to generate it the query and assignment to value of the variable.

Doctrine 2 gives 500 Error on simple query

I have this doctrine query:
$attributeQuery = $em->createQuery("SELECT PARTIAL p.{id} FROM m:Product p JOIN m:BtgNode n WITH n.attribute = p.amazonCatUs1 WHERE n.id = :node")->setMaxResults(5);
$attributeQuery->setParameter('node', $amazonCategoryId);
$products = $attributeQuery->getResult();
Which causes a 500 error. When I comment out the getResult line, the page finishes loading, and when I run $attributeQuery->getSql() and execute the result though phpmyadmin the result return correctly in less than 0.001 seconds.
I cannot find anything at all to explain this behavior.
The full script:
$em = $app['orm.em'];
echo ini_get('memory_limit').'<br>';
ini_set('xdebug.var_display_max_depth', -1);
ini_set('xdebug.var_display_max_data', -1);
$productQuery = $em->createQuery("SELECT p FROM m:Product p WHERE p.amazonUsBrowse1 = :node OR p.amazonUsBrowse2 = :node");
$productQuery->setParameter('node', $amazonCategoryId);
$productQuery->useQueryCache(false);
//if results, then not clothing and simple, else complicated
$products = $productQuery->getResult();
print memory_get_usage(true);
if (count($products) == 0) {
$em->clear();
echo 'complex';
$attributeQuery = $em->createQuery("SELECT PARTIAL p.{id} FROM m:Product p JOIN m:BtgNode n WITH n.attribute = p.amazonCatUs1 WHERE n.id = :node")->setMaxResults(5);
$attributeQuery->setParameter('node', $amazonCategoryId);
//$attributeQuery->useQueryCache(false);
var_dump($attributeQuery->getSql());
$products = $attributeQuery->getResult();
var_dump($attributeQuery->getSql());
}
var_dump($products);
die();

innerjoin query in drupal 7

I am applying this query for below D6 query , not working ..dont know wat wrong i'm doing ....does innerjoin fails in some condition
$result = db_select('px_slides','s')
->join('node','n','s.vid = n.vid')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=')
->execute()->fetchObject();
drupal 6 query i have:
$result = db_query('
SELECT n.nid, n.vid, n.sticky, n.title, n.created, s.cid, s.ref_id, s.viewurl, s.specimen_type, s.tissue_type, s.body_site, s.test_type, s.algorithm, s.result
FROM {px_slides} s INNER JOIN {node} n ON n.vid = s.vid
WHERE n.status = 1 ')->execute();
You need to put your call to ->join() on a separate line altogether, as it doesn't return the query object:
$query = db_select('px_slides','s')
->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$query->join('node','n','s.vid = n.vid');
$result = $query->execute()->fetchObject();
The join method does not chain like that. You will have to do something like:
$query = db_select('px_slides','s')
->join('node','n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'))
->fields('n',array('sticky','title'))
->condition('n.status','1','=')
->condition('s.cid','126','=');
$result = $query->execute()->fetchObject();
Also you can use the to string magic method to see the query it is going to execute.
$query->__toString();
Try this...
$query = db_select('px_slides', 's');
$query->innerJoin('node,'n','s.vid = n.vid');
$query->fields('s',array('tissue_type','body_site'));
$query->fields('n',array('sticky','title'));
$query->condition('n.status','1');
$query->condition('s.cid','126');
$result= $query->execute()->fetchAll(PDO::FETCH_ASSOC);

Drupal db_query_range SQL query

I'm trying to get this working and the query executes but nothing comes back. I've tried everything I can think of, can you spot what I'm doing wrong?
$nido = $node->nid;
$result = db_query_range('
SELECT i.nid, i.iid, a.fid, p.filename, p.filepath
FROM {drup_image_attach} i
LEFT JOIN {drup_image} a ON i.iid = a.nid
LEFT JOIN {drup_files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nido, 0, 10);
echo "Filepath = " . $result ->filepath. "<br>";
echo "Filepath = " . $result ->filename . "<br>";
echo "IID = " . $result ->iid. "<br>";
echo "NID = " . $result ->nid . "<br>";
}
EDIT - I sorted out a couple of bits, but the output is still empty!
EDIT - this is the working code:
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
$obj_image = db_fetch_object($get_image);
$imagefilename = $obj_image->imagefilename;
$result is a mysql(i) resource only. You first have to fetch the row/object.
$result = db_query_range(....);
$object = db_fetch_object($result);
print_r $object;
Assuming you have a range ob results not a single result this should work. You need to store the results in an array or process them in a different manner. for a single result see db_result()
$nodeid = $node->nid;
$get_image = db_query('
SELECT p.filepath as imagefilename
FROM {image_attach} i
LEFT JOIN {image} a ON i.iid = a.nid
LEFT JOIN {files} p ON a.fid = p.fid
WHERE i.nid = %d AND p.filename = "recipe_thumb"', $nodeid);
while(($obj_image = db_fetch_object($obj_image)) == TRUE){
$imagefilename[] = $obj_image->imagefilename;
}

Resources