Doctrine 2 gives 500 Error on simple query - symfony

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

Related

Operator "NOT LIKE" not work correctly

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.

Cannot count query which selects two FROM components, cannot make distinction when i try to break my query to add conditional statement symfony 2 .8

pls help i get this error
Cannot count query which selects two FROM components, cannot make distinction
when i try to break my query to add conditional statement
i have read this
KnpPaginatorBundle/Resources/doc/manual_counting.md and i arrived at this
public function findCategoryProduct($category,$minPrice=null,$maxPrice=null,$gender=null)
{
$countgb = $this->createQueryBuilder('1')
->select('count(p)')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$count = $countgb->getQuery()->getSingleScalarResult();
$query = $this->createQueryBuilder('1')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$query ->getQuery()
->setHint('knp_paginator.count', $count);
return $query;
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$request->query->getInt('page', 1),10,array('distinct' => false));
and i still get the error
Hi I advise you to use subquery like :
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
$ids = $query->getResult();
or with expr:
$query->andWhere($query->expr()->notIn('c.id', $subquery->getDQL()));
some documentation here

doctrine dql exception: Illegal offset type in /var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 601

I want to produce a DQL for following MySQL query:
SELECT * FROM `folders` AS `t` WHERE `t`.`Library` = #myLib AND AND `t`.`Id` NOT IN (
SELECT DISTINCT(`f`.`Id`) FROM `folders` AS `f` JOIN `folders` AS `ff` ON (`f`.`Position` LIKE CONCAT(`ff`.`Position`, '%')) WHERE `ff`.`Active` = 1 AND `ff`.`Library` = #myLib AND `f`.`Library` = #myLib
)
ORDER BY `t`.`Position` ASC
The query works fine in mySQL and returns correct records.
To generate DQL I've tried both below options:
1.
$query = $em->createQuery("SELECT F FROM MyBundle:Folders T WHERE T.Library = :libid AND T.id NOT IN (
SELECT DISTINCT(F.id) FROM MyBundle:Folders F JOIN MyBundle:Folders FF WITH F.Position LIKE CONCAT(FF.Position, '%') AND F.Library = :libid AND FF.Library = :libid AND FF.Active = true
) ORDER BY T.Position ASC")
->setParameter('libid', $library);
$result = $query->getResult();
2.
$q1 = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$q1->join('\MyBundle\Entity\Folders', 'FF', 'WITH', $q1->expr()->like('F.Position', $q1->expr()->literal('CONCAT(FF.Position, \'%\')')))
->where('FF.Active = true')
->andWhere("FF.Library = '$library'")
->andWhere("F.Library = '$library'");
$q2 = $this->createQueryBuilder('T');
$q2->where('T.Library = :libid')
->andWhere($q2->expr()->notIn('T.id', $q1->getDQL()))
->setParameter('libid', $library)
->orderBy('T.Position', 'ASC');
$result = $q2->getQuery()->getResult();
In my perspective it seems OK but I don't know why in both ways it produce following exception:
ContextErrorException: Warning: Illegal offset type in
/var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php line 601
Any help will be appreciated.
It seems no one has an answer for this. I found a temporary solution as below (I call it temporary because I'm changing my unique query to two separate queries and it seems the issue is in core of doctrine).
$qb = $this->createQueryBuilder('F')
->select('DISTINCT(F.id)');
$qb->join('\MyBundle\Entity\Folders', 'FF', 'WITH', 'F.Position LIKE CONCAT(FF.Position, \'%\')')
->where('FF.Active = true')
->andWhere("FF.Library = :library")
->andWhere("F.Library = :library")
->setParameter('library', $library);
$included_folders = $qb->getQuery()->getArrayResult();
$query = $this->createQueryBuilder('F')
->where('F.Active = false')
->andWhere('F.Library = :library')
->setParameter('library', $library)
->orderBy('F.Position', 'ASC');
if (!empty($included_folders)) {
if (count($included_folders) > 1)
{
foreach ($included_folders as $index => $value)
{
if (is_array($value))
{
$included_folders[$index] = !empty($value['id']) ? $value['id'] : $value[1];
}
}
$query->andWhere($query->expr()->notIn('F.id', $included_folders));
}
else {
$query->andWhere('F.id != :folder ')
->setParameter('folder', $included_folders[0]);
}
}
$result = $query->getQuery()->getResult();
As you see instead of getting the dql from my first query and putting it inside my second dql in notIn section which will lead to the warning message, I execute the first query and get the results then put the results inside notIn if amount of returned values are more than one, otherwise it should be in regular !=. This solved my problem for now, but as you see amount of transactions are now increased
If anyone has a better solution or any fix for the warning I will be thankful.
I've encountered the same error and it seems that this has been fixed in latest trunk of Doctrine/ORM.
Using "2.5.*#dev" as version in your composer.json for doctrine/orm should fix this bug and will let you do what you want in a single query.

How to get all data from easyui datagrid

I had try to use the getData e.g.
data = $("#dg").datagrid("getData");`
var total = data.total; (total is 100)`
var rows = data.rows; (rows.length is 25)`
It can result: the total number is correct like 100 records
but the rows return only get the current page rows like 25 rows.
I need to get all of the records (100 rows).
Is there something i missed ? Or how can we do this ? Please help me.
This is because of pagination. let me describe model with pagination for showing 100 row in php.
function get_invoiceinfoList($search_keyword,$custID){
$page = 1;
$rows = 100;
$sort = '';
$order = 'asc';
$offset = ($page - 1) * $rows;
if($custID > 0 && $search_keyword == 0){
$search = " and a.customer_id =$custID";
}else{
$search = "and (b.name like '%$search_keyword%' || a.inv_no like '%$search_keyword%')" ;
}
$vQuery=$this->db->query("SELECT a.inv_id,a.inv_no,a.inv_org,a.inv_date,
a.customer_id,a.heading,a.quot_id,a.total_cost as total_amount,
a.paid_amount,(a.total_cost-a.paid_amount)as due_amount,b.name
from sales_invoice a,view_customer_info b
where
a.customer_id = b.customer_id $search order by a.inv_date DESC limit $offset, $rows");
$result = $vQuery->result();
$rows = array();
foreach ($result as $rowData){
array_push($rows, $rowData);
}
$data['rows'] = $rows;
return $data;
}

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