I'm trying to make a select like this:
SELECT c.*, CONCAT(c.provider_id,'#',c.name") FROM contact AS c
so, I'm writing something like this...
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array("CONCAT(c.provider_id,'#',c.name"), false)
but, result is:
SELECT c``CONCAT(c.provider_id,'#',c.name AS
CONCAT(c.provider_id,'#',c.name FROM contact AS c
What am i doing wrong?
Thanks for any help!
When i have to extract some columns from a table and add a Sql function, i usually use this code:
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array(
'id', 'name', 'data' => new Expression('CONCAT(c.provider_id,'#',c.name)')
)
);
Expression is an instance of Zend\Db\Sql\Expression, the result is:
SELECT `id`, `name`, CONCAT(c.provider_id,'#',c.name) AS `data` FROM `contact` AS `c`
check out Database Expressions if you need to use MySQL functions or anything else which you don't want to be escaped automatically for you. some examples:
https://github.com/ralphschindler/Zend_Db-Examples
$sql = new Sql($this->adapter);
$query = $sql->select()
->from(array('c' => 'contact'))
->columns(array(
'*', new Expression("CONCAT(c.provider_id,'#',c.name) as data")
))
;
Related
I just want to return one column (per.cd_acao), so I tried something like:
$select->from(array('act' => 'tb_acao'))
->join(array('per' => 'tb_perfil_acao'),
'per.cd_acao = act.cd_acao',
array('cd_acao'),
$select::JOIN_INNER
);
but this is producing a query string like:
SELECT "act".*, "per"."cd_acao" AS "cd_acao" FROM "tb_acao" AS "act" INNER JOIN "tb_perfil_acao" AS "per" ON "per"."cd_acao" = "act"."cd_acao" WHERE "per"."sq_perfil" = '89'
it is bringing all columns from the first table, when I want none. What am I missing here?
Update
summarizing: When I don't inform 'columns' in a select object, it defaults to return all columns to me. But when I'm joining, I don't want any columns to be returned by the first table.
An empty array will suffice
$select->from(array('act' => 'tb_acao'))
->columns(array())
->join(array('per' => 'tb_perfil_acao'),
'per.cd_acao = act.cd_acao',
array('cd_acao'),
$select::JOIN_INNER
);
Try with this query
use Zend\Db\Sql\Sql;
protected $tableGateway;
public $adapter;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
publice function getData(){
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$select = $sql->select();
$select->from('tb_acao')
->join('per'),
'per.cd_acao = act.cd_acao',
array('cd_acao'),
);
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
return $results;
}
I have an sql query like the one below and i would like to create this in Zend Framework 2.
( SELECT id AS id FROM exp_personal_data ORDER BY town ) UNION ( SELECT id AS id FROM pd_unregister )
I would like to have union and add to this LIMIT, ORDER BY etc.
$this->_select->combine($selectPdContest, 'union all');
When i write the query like this.
$this->_select->combine($selectPdContest, 'union all')->limit('10);
Query looks like this:
( SELECT id AS id FROM exp_personal_data ORDER BY town LIMIT 10 ) UNION ( SELECT id AS id FROM pd_unregister )
The limit is added only to firs select. I want the limit will to be added like this.
( SELECT id AS id FROM exp_personal_data ORDER BY town ) UNION ( SELECT id AS id FROM pd_unregister ) LIMIT 10
How make this in Zend framework 2?
Solution 1 (simple):
$adapter = $this->tableGateway->getAdapter();
$resultSet = $adapter->query("(SELECT user_id AS id FROM user ORDER BY id) UNION (SELECT account_id AS id FROM account) LIMIT 10",$adapter::QUERY_MODE_EXECUTE);
print_r($resultSet->toArray());die;
Solution 2 (Complex):
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Select;
//sql query first part
$adapter = $this->tableGateway->getAdapter();//the db connection adapter
$select = new Select('user');
$select->columns(array('id' => 'user_id'));
$select->order('id');
$sql = new Sql($adapter);
$statement = $sql->getSqlStringForSqlObject($select);
//sql query second part
$select2 = new Select('account');
$select2->columns(array('id' => 'account_id'));
$sql = new Sql($adapter);
$statement2 = $sql->getSqlStringForSqlObject($select2);
//combine the two statements into one
$unionQuery = sprintf('%s UNION %s','('.$statement = $sql->getSqlStringForSqlObject($select).')',
'('.$statement2 = $sql->getSqlStringForSqlObject($select2).') LIMIT 10');
//execute the union query
$resultSet = $adapter->query( $unionQuery, $adapter::QUERY_MODE_EXECUTE);
print_r($resultSet->toArray());die;
Add these lines will work,
$select1 = new Select();
$select1->combine($selectPdContest, 'union all');
$select3 = new Select();
$oneTwo = $select3->from(['sub' => $select1])->limit(10);
How to use having() clause in ZF2?
There is almost no examples on the web how to prepare correct select object with having.
I have query like:
SELECT root_schema_id as `schema_id`
FROM `standard_specific_root_schemas`
WHERE `vehicle_id` IN (".implode(",",$vehiclesIds).")
GROUP BY `schema_id`, rootSubGroup_id HAVING count(*)=".$noOfVehicles
And I'm trying to run it in ZF2:
public function getVehicleWithinCommonRootSubgroupInSpecific($vehiclesIds)
{
$where = new Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new Having('count(*) = '.count($vehiclesIds));
$rowset = $this->tableGateway->select(function (Select $select) use ($where, $having) {
$select
->where($where)
->having($having);
});
if (!$rowset) {
throw new \Exception("Could not find schemas for group $groupId");
}
return $rowset;
}
Of course that part in ZF2 is not finished yet as I wanted to check if it's working first.
I've tried few ways of providing params to having method but everything generates errors.
Help please, I'm desperate...
I cannot test your query, but can try and reproduce the query you need.
I adjusted the having to use ->expression() instead of a variable via the construct.
I also added the group statement.
To view the query I added a var_dump:
$where = new \Zend\Db\Sql\Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new \Zend\Db\Sql\Having();
$having->expression('count(*) = ?', count($vehiclesIds));
$rowset = $this->tableGateway->select(function (\Zend\Db\Sql\Select $select) use ($where, $having) {
$select
->where($where)
->group(array('schema_id', 'rootSubGroup_id'))
->having($having);
var_dump( $select->getSqlString() );
});
Let me know if this helps.
To circumvent the error mentioned in the comments you would have to do something like below:
$sql = $this->tableGateway->getSql();
$select = $sql->select();
$where = new \Zend\Db\Sql\Where();
$where->in('vehicle_id', $vehiclesIds);
$having = new \Zend\Db\Sql\Having();
$having->expression('count(*) = ?', count($vehiclesIds));
$select
->where($where)
->group(array('schema_id', 'rootSubGroup_id'))
->having($having);
$preparedQuery = $sql->prepareStatementForSqlObject($select);
var_dump( $preparedQuery->getSql() );
However, if I'm right, the tableGateway does this for you so the error should go away once you start using the select to query the database.
Also, you can use the above to do that too, just replace this:
$preparedQuery = $sql->prepareStatementForSqlObject($select);
var_dump( $preparedQuery->getSql() );
With:
$this->tableGateway->selectWith($select);
I feel like I must me missing something very simple. It's a very simple task, all I want to do is get something like:
SELECT * FROM lookup_items
JOIN lookup ON lookup_items.lookup_id = lookup.id
This returns all the columns for all the joined tables, in regular SQL. Here's my attempt in zf2:
$select = new Select();
$select->from('lookup_items');
$select->join('lookup', 'lookup_items.lookup_id = lookup.id');
The result set only includes the columns in 'lookup_items'. I've tried various ways to get the 'lookup' columns, including:
$select->columns(array('lookup_items.*', 'lookup.*'));
But they all just blow up. Surely there's a way to do this, and it's just so simple I'm missing it completely.
I thought a simple example would be avoid confusion, but here's more code:
class LookupItemsTable extends AbstractTableGateway
{
public function getList($resource)
{
$system_name = str_replace('*', '%', strtoupper($resource));
$joinTable = 'lookup';
$select = new Select();
$select->from($this->table);
$select->join($joinTable, "{$this->table}.lookup_id = {$joinTable}.id");
$where = array();
$where[] = "{$this->table}.enabled is true";
$where[] = "{$joinTable}.enabled is true";
$where[] = "UPPER({$joinTable}.system_name) ilike '{$system_name}'";
$select->where($where);
$sort[] = 'sort_order ASC';
$sort[] = 'value ASC';
$select->order($sort);
$rowset = $this->selectWith($select);
return $rowset;
}
}
Where:
$resource = $this->params()->fromRoute('resource', 'BUSINESS');
And $this->table is 'lookup_items'. Really all I want to do is get columns from both joined tables. I guess there's a zf2 way to just make a straight SQL statement without all the OO falderal, so I could just force it that way. But I'd rather work within the framework as much as possible.
Just change this line
$select->join('lookup', 'lookup_items.lookup_id = lookup.id');
to
$select->join('lookup', 'lookup_items.lookup_id = lookup.id', array('lookupcol1', 'lookupcol2');
Raj answer is the best one but it only works if you don't forget to add these fiels in your LookupItems model.
class LookupItems
{
// Your lookup_items fields here...
...
// And the added lookup fields here, the ones you add in the array
public $lookupcol1;
public $lookupcol2;
And in the exchangeArray method :
public function exchangeArray($data)
{
// .... your fields, and the new ones
$this->lookupcol1 = (! empty($data['lookupcol1'])) ? $data['lookupcol1'] : null;
$this->lookupcol2 = (! empty($data['lookupcol2'])) ? $data['lookupcol2'] : null;
}
I figured it out.
Added this:
$select->columns(array('*'));
And then this near the end:
$sql = new Sql($this->adapter);
$statement = $sql->prepareStatementForSqlObject($select);
$rowset = $statement->execute();
This returns the expected result, with the caveat that now my rows are returned as associative arrays instead of objects.
This is how you can create queries with join in zf2.
$resultSet = $this->select(function (Select $select) {
// omit the table name
//$select->from('foo');
$select->join('users', "users.id foo.createdby", 'firstname', '');
$select->order('id ASC');
// echo $select->getSqlString();// to print your query
});
$entities = array();
foreach ($resultSet as $row) {
$entity = new Entity\Foo();
$entity->setId($row->id)
->setFullname($row->fullname)
->setCaseid($row->caseid)
->setTestimonial($row->testimonial)
->setSortorder($row->sortorder)
->setActive($row->active)
->setCreated($row->created)
->setModified($row->modified)
->setFirstname($row->firstname)
->setCreatedby($row->createdby);
$entities[] = $entity;
}
return $entities;
I would like mapping a sql-view with Doctrine2.
This view is a TempTable containing some statistics that would show without rewriting the sql that generates the view
I try to map like a table, but updating schema drop the view and create a table
I try also with NativeSQL...
public function getMessages(\Project\Bundle\MyBundle\Entity\User $user) {
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addEntityResult('MessageCenter', 'v');
$rsm->addFieldResult('v', 'user_id', 'user_id');
$rsm->addFieldResult('v', 'tot', 'tot');
$rsm->addFieldResult('v', 'read', 'read');
$rsm->addFieldResult('v', 'to_read', 'to_read');
$rsm->addFieldResult('v', 'stored', 'stored');
$rsm->addFieldResult('v', 'spam', 'spam');
$q = "SELECT * FROM message_stats_view WHERE user_id = ?";
$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$query = $this->getEntityManager()->createNativeQuery($q, $rsm);
$query->setParameter(1, $user->getId());
echo $query->getSQL();
var_dump($query->execute());
exit;
}
I create the entity MessageCenter with getter and setter, but my output is:
SELECT * FROM message_stats_view WHERE user_id = ?
array
empty
(Answered by the OP in aq question edit. Transcribed to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I SOLVED!!!!
public function getCentroMessaggi(\Project\Bundle\MyBundle\Entity\User $user) {
$connection = $this->getEntityManager()->getConnection();
$q = "SELECT * FROM message_stats_view WHERE user_id = :id";
$stmt = $connection->executeQuery($q, array('id' => $user->getId()));
return $stmt->fetch();
}
This return an array. PERFECT!