How can i bind parameter in zf2 tablegateway - zend-framework2

How can i bind parameter in zend framework 2 using tablegateway, here is the code i am using
$adapter = $this->tableGateway->getAdapter();
$result = $adapter->query(
"SELECT * "
. "FROM TABLE "
. "WHERE SOME_ID = $SOME "
. "AND STATUS = 1 "
);
$dataSource = $result->execute();
$statement = $dataSource->getResource();
$result = $statement->fetchAll(\PDO::FETCH_OBJ);
please suggest me a secure query builder code

You are trying to bind parameter in Adapter not in TableGateway.
It can be done on many ways, but example that you post
$id = 123;
$res = $adapter->query(
"SELECT * FROM TABLE WHERE SOME_ID = ? AND STATUS = 1", [$id]
);
var_dump($res->current());
There is a second parameter in function query() which is
#param string|array|ParameterContainer $parametersOrQueryMode
So you can play little bit with this option(s)... also check function Zend\Db\Adapter\Adapter::query();
Easier way is to use TableGateway:
$res = $this->tableGateway->select(['SOME_ID' => $id]);
$res->current(); // than you can use also toArray(), current(), etc.

Related

Get relationship type in a php variable of neo4j

What I want is that: I have two nodes with id 1 and id 2. It has a relationship type (id:1)-[r:Knows]->(id:2). My query is working fine in Neo4j as localhost but I am not able to store that relationship type in a PHP variable to use further. I am using graphware.
Note: $id = 1 and $s_id = 2.
$queryString = "MATCH (a:signup{iid:'$id'})-[r]->(b:signup{iid:'$s_id'}) return type(r) ";
$query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
$result = $query->getResultSet();
if($result == 'Knows')
{
$s_relation = $result;
}
else
{
$s_relation = 'empty';
}
It returns the else part as EMPTY.
Thank you.
Well, I found a solution for this.

get all data from mysql query in zend 2

$sql = "select * from users";
$statement1 = $db->query($sql);
$result = $statement1->execute()->current();
The above code returns the data from a single user.
How to get the data from all users?
Please help.
As #tasmaniski mentioned in the comment, you need to remove the current(), and $result becomes a "resulSet", which is readable by a foreach. Try this:
$sql = "select * from users";
$statement1 = $db->query($sql);
$results = $statement1->execute();
foreach($results as $result){
var_dump($result);
}
More documentation here:
http://framework.zend.com/manual/current/en/modules/zend.db.result-set.html
you can get all data like this:
$sql = "select * from users";
$statement = $db->query($sql);
$results = $statement->execute();
$rec = $results->getResource()
->fetchAll();
you can use \PDO::FETCH_ASSOC in fetchAll to get data in assoc format.

How to use having() in tableGateway in ZF2

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

ZF2 - Select with CONCAT

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

How manage a VIEW with Doctrine 2?

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!

Resources