Doctrine Paginator with NativeQuery - php

I'm trying to use Doctrine Paginator, but I'm HAVING TO use NativeQuery since I use a custom DB function in my Query.
But I get this error:
Fatal error: Uncaught Error: Call to undefined method Doctrine\ORM\NativeQuery::getFirstResult()
My code so far is:
$sql = "SELECT std_modulos_aulas.idaula, std_modulos_aulas.idmodulo, conteudo, std_modulos_aulas.titulo
FROM uov.std_modulos_aulas
INNER JOIN uov.std_modulos ON std_modulos_aulas.idmodulo = std_modulos.idmodulo
INNER JOIN uov.std_cursos ON std_cursos.idcurso = std_modulos.idcurso
WHERE std_modulos.publicar = 's' AND std_modulos.excluido = 'n' AND std_modulos_aulas.publicar = 's' AND std_cursos.idcurso = ".$curso." AND academico.strip_tags(LOWER(std_modulos_aulas.conteudo)) LIKE '".$termo."'
ORDER BY std_modulos.ordem ASC, std_modulos_aulas ASC";
$rsm = new ResultSetMapping;
$rsm->addScalarResult('idaula', 'idaula');
$rsm->addScalarResult('idmodulo', 'idmodulo');
$rsm->addScalarResult('conteudo', 'conteudo');
$rsm->addScalarResult('titulo', 'titulo');
$query = $this->em->createNativeQuery($sql, $rsm);
return new Paginator($query, $fetchJoinCollection = false);
Any help would be nice. I've already tried using QueryBuilder, but my strip_tags function isn't working in that way. The old topics couldn't help me.

For those who might have the same problem in the future:
I ended up solving this by:
At my Model, I returned a standard query result:
$sql = "SELECT std_modulos_aulas.idaula, std_modulos_aulas.idmodulo, conteudo, std_modulos_aulas.titulo
FROM uov.std_modulos_aulas
INNER JOIN uov.std_modulos ON std_modulos_aulas.idmodulo = std_modulos.idmodulo
INNER JOIN uov.std_cursos ON std_cursos.idcurso = std_modulos.idcurso
WHERE std_modulos.publicar = 's' AND std_modulos.excluido = 'n' AND std_modulos_aulas.publicar = 's' AND std_cursos.idcurso = ".$curso." AND academico.strip_tags(LOWER(std_modulos_aulas.conteudo)) LIKE '".$termo."'
ORDER BY std_modulos.ordem ASC, std_modulos_aulas ASC";
$rsm = new ResultSetMapping;
$rsm->addScalarResult('idaula', 'idaula');
$rsm->addScalarResult('idmodulo', 'idmodulo');
$rsm->addScalarResult('conteudo', 'conteudo');
$rsm->addScalarResult('titulo', 'titulo');
$query = $this->em->createNativeQuery($sql, $rsm);
return $query->execute();
And then, at my Controller, I used a Zend Paginator:
$iteratorAdapter = new \Zend\Paginator\Adapter\ArrayAdapter($result);
$paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage(10);

Related

Joomla query Object (stdClass) with random

I'm building a Joomla template. For this I need to test/query two fields in the DB. I'm trying to get familiar with getDBO class but stuck here.
These two queries do nearly the same. I need both variables $category and $hasField. How can merge these two queries into one? This is a bit redundant.
$db = JFactory::getDBO();
$id = JRequest::getInt('id');
$db->setQuery('
SELECT
#__categories.title
FROM
#__content,
#__categories
WHERE
#__content.catid = #__categories.id
AND
#__content.id = '.$id
);
$category = $db->loadResult();
$db->setQuery('
SELECT
#__attachments.filename,
#__attachments.parent_id
FROM
#__attachments
WHERE
#__attachments.parent_id =' . $id
);
$hasField = $db->loadResult();
You can try joining #__attachments to the 1st query.
SELECT
#__categories.title,
#__attachments.filename,
#__attachments.parent_id
FROM
#__content,
#__categories,
#__attachments
WHERE
#__content.catid = #__categories.id
AND
#__attachments.parent_id = #__content.id
AND
#__content.id = $id
I might be wrong there as no place to test but it suppose to be like left joined:
SELECT
#__categories.title, atch.filename
FROM
#__content,
#__categories
LEFT JOIN
#__attachments AS atch ON atch.parent_id = #__content.id
WHERE
#__content.catid = #__categories.id
AND
#__content.id = '.$id
);

Zend Framework 2: sql subquery

In ZF1 it worked like this:
$selectColumns = array(
'*',
'orders_total' => "(".$db->select()->from("orders", array("COUNT(*)"))->where("orders.parent_id=mytable.id").")",
);
$select = $db->select()->from('mytable', $selectColumns);
How to do this in the ZF2? Thanks.
Please try this.
$sql = new Sql($this->_adapter);
$mainSelect = $sql->select()->from('mytable');
$subQry = $sql->select()
->from('orders')
->columns(array('orderCount' => new \Zend\Db\Sql\Expression('COUNT(orders.id)')));
$mainSelect->columns(
array(
'id',
'orders_total' => new \Zend\Db\Sql\Expression('?', array($subQry)),
)
);
$statement = $sql->prepareStatementForSqlObject($mainSelect);
$comments = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($comments);
return $resultSet->toArray();
Link: ZF2 - subqueries
You can try this:
// Make your query here using the builder if you wish,
// but we will need to convert to string for the Expression
$sub = new Select('orders');
$sub->columns(array(new Expression('COUNT(*) as total')))
->where(array('id' => 4))
;
// You could just create an expression..
$subquery = new \Zend\Db\Sql\Expression("({$sub->getSqlString()})");
$select = new \Zend\Db\Sql\select('tablename'); // this is inside a
$select->columns(array('*', 'orders_total' => $subquery));
the output will be something like this:
SELECT
.*,
(SELECT COUNT(*) as total FROM "orders" WHERE "id" = '4') AS orders_total
FROM tablename
I haven't figured out a nice way of using the query builder to perform these kind of queries without having to use raw queries.
Without any more info you could try:
$selectColumns = array(
'*',
'orders_total' => "(".$db->select()->from("orders", array("COUNT(*)"))->where("orders.parent_id", "mytable.id").")",
);
$select = $db->select()->from('mytable', $selectColumns);
You will need to add at top with the use statements:
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
You can try restructuring your SQL to something like:
SELECT
*,
SUM(IF(O.parent_id IS NULL, 0, 1)) AS orders_total
FROM mytable
LEFT JOIN orders O ON mytable.id = O.parent_id
Which you can represent using Zend Framework 2 as:
$select = new Select('mytable');
$select->columns(array(
'*',
'orders_total' => new Expression("SUM(IF(O.parent_id IS NULL, 0, 1))")
));
$select->join(array('O', 'orders'),
"mytable.id = O.parent_id",
Select::JOIN_LEFT);
$select->group(array('mytable.id'));
$result = $dbh->selectWith($select);
Assuming $dbh is your database adapter.

Zend_Db_Select: Working with JOIN's

I have this query:
SELECT
groups.name
categories.name,
categories.label
FROM
groups
JOIN
categories
ON
(categories.group1 = groups.id
OR
categories.group2 = groups.id)
AND
groups.label = :section
AND
categories.active = 1
Now, this is my JOIN using Zend_Db_Select but it gives me array error
$select->from($dao, array('groups.name', 'categories.name', 'categories.label'))
->join(array('categories', 'categories.group1 = groups.id OR categories.group2 = groups.id'))
->where('groups.label = ?', $group)
->where('categories.active = 1');
My error:
Exception information:
Message: Select query cannot join with
another table
Does anyone know what I did wrong?
UPDATE / SOLUTION:
I've found the solution thanx to Eran. I just post the solution here in case anyone else is stuck on a problem like this one. The solution is:
$db = Zend_Registry::get('db');
$dao = new Default_Model_Db_CategoryDao('db');
$select = $dao->select();
$select->setIntegrityCheck(false)
->from(array('c' => 'categories'), array('name', 'label'))
->join(array('g' => 'groups'), 'c.group1 = g.id OR c.group2 = g.id', 'g.label')
->where('g.label = ?', $group)
->where('c.active = 1');
return $dao->fetchAll($select);
You are using a Zend_Db_Table_Select object. Those by default have integrity check enabled and cannot select data that is outside of their table.
You can turn it off by adding -> setIntegrityCheck(false) to the select object before querying with it.
You can read more about it in the manual under Select API -> Advanced usage

Zend Framework: How Do I 'auto' generate a SQL query correctly

I want to generate the following SQL:
SELECT `rc`.*, `c`.`name` FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
However I am getting:
SELECT `rc`.*, `c`.* FROM `RunConfigurations` AS `rc` INNER JOIN `Clients` AS `c` ON rc.client_id = c.id WHERE (rc.client_id = ?) ORDER BY `rc`.`config_name` ASC
The difference is I want c.name, not c.*
Using the following ZF PHP code:
public function fetchConfigurations($clientId = null, $order = 'rc.config_name ASC')
{
$db = $this->getDb();
$stmt = $db->select()
->from(array('rc' => 'RunConfigurations','c.name'))
->join(array('c' => 'Clients'),'rc.client_id = c.id')
->order($order);
if(is_numeric($clientId))
{
$stmt->where('rc.client_id = ?')
->bind(array($clientId));
}
$results = $db->fetchAll($stmt);
if(sizeof($results) > 0)
{
$configs = array();
foreach($results as $row)
{
$configs[] = $this->createRunConfigurationFromRow($row);
}
return $configs;
}
else
{
die($stmt->__toString());
return null;
}
}
This is aggravating and I feel like I am missing something at either:
->from(array('rc' => 'RunConfigurations','c.name'))
or
->join(array('c' => 'Clients'),'rc.client_id = c.id')
and the ZF examples are not shedding any light on this.
You are so close! join() actually has a 3rd parameter in which you can supply the column names just like the 2nd parameter from from().
This would mean that ->join(array('c' => 'Clients'),'rc.client_id = c.id',array('name')) should generate the SQL you are looking for.
-- Quote from the Zend Framework manual:
The third argument to join() is an array of column names, like that used in the from() method. It defaults to "*", supports correlation names, expressions, and Zend_Db_Expr in the same way as the array of column names in the from() method.

Zend Framework Select Objects And UNION()

I'm pretty sure this is not possible in Zend Framework (I have searched the Web, the documentation and issue tracker) but I just want to make sure so I'm asking here.
$select = $this->select();
$select->union($select1, $select2);
That doesn't work of course. To explain what I need. I need to use UNION() to merge 2 tables in a SELECT query, I know I could just do:
$select = "$select1 UNION $select2";
The problem is that would return a string and I need to get a select object so I can use it with Zend_Paginator.
I have already solved the issue by modifying my database architecture but I'm just curious if there is some workaround for this.
Here's what I've done to make a union:
$select = $this->select();
//common select from both sides of the union goes here
$select1 = clone($select);
//select1 specifics here
$select2 = clone($select);
//select 2 specifics here
$db = $this->getAdapter();
$pageselect = $db->select()->union(array("($select1)", "($select2)"));
Remember Db_Select's __toString will print out the SQL generated by that select, to help you debug.
Zend_Db_Select has a union method so I'd have thought it is possible, if you can build your query using a select object. I haven't used Zend_Db_Select (or the table subclass) with union but I'd imagine you can do something like
$select = $this->select()
->where('blah')
->union($sql);
a complete example:
public function getReservationById($id)
{
if(!$id) return null;
$sql = $this->table->select();
$sql->union(array(
$this->table->select()->where('id=?', $id),
$this->tableFinished->select()->where('id=?', $id),
$this->tableCanceled->select()->where('id=?', $id),
$this->tableTrashed->select()->where('id=?', $id)
));
echo $sql->__toString();
}
and the generated query:
SELECT reservations.* FROM reservations WHERE (id='5658') UNION SELECT res_finished.* FROM res_finished WHERE (id='5658') UNION SELECT res_cancel.* FROM res_cancel WHERE (id='5658') UNION SELECT res_trash.* FROM res_trash WHERE (id='5658')
This practical example shows a function that returns a rowset of either latest or if a available favourite blog entries of a specific year (artwork blog):
public function fetchBestOf($year)
{
$selectLatest = $this->select()->where('isHidden = 0')
->where('YEAR(dateCreated) = ' . $year)
->where('isHighlight = 0');
$selectHighlights = $this->select()->where('isHidden = 0')
->where('YEAR(dateCreated) = ' . $year)
->where('isHighlight = 1');
$selectUnion = $this->select()->union(array($selectLatest, $selectHighlights), Zend_Db_Select::SQL_UNION_ALL)
->order('isHighlight DESC')
->order('dateCreated DESC')
->order('workID DESC')
->limit('5');
$rowset = $this->fetchAll($selectUnion);
return $rowset;
}
The best way Zend suggest is like follows....
$sql = $this->_db->select()
->union(array($select1, $select2,$select3))
->order('by_someorder');
echo $sql->__toString();
$stmt = $db->query($sql);
$result = $stmt->fetchAll();
echo will show the query
Here $select1, $select2, $select3 can be different select queries with same
number of columns...
This is how it works for me:
$select1 = $this->select();
$select2 = $this->select();
After getting the necessary data in both queries the UNION syntax goes like this:
$select = $this->select()->union(array('('.$select1.')', '('.$select2.')'));

Categories