Using a stored procedure in Laravel 4 - stored-procedures

I'm trying to call a stored procedure from via a laravel route and i keep getting an error:
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'emailAddress' in 'field list' (SQL: CALL getLibraryList(emailAddress))",
I believe the call I'm making is correct:
$result = DB::statement('CALL getLibraryList('.$email.')');
return $result;

Found out a way to get this working here:
$result = DB::select('call getLibraryList(?)',array($email));

You might find some trouble to execute them. Here are some options:
DB::statement(DB::raw('CALL getLibraryList('.$email.');'));
Or
DB::select('CALL getLibraryList(?)',array($email));
And, the dirtiest one:
$db = DB::connection();
$stmt = $db->pdo->prepare("CALL getLibraryList(?)");
$stmt->bindParam(1, $email);
$stmt->execute();
$search = array();
do {
$search = $stmt->fetchAll(PDO::FETCH_CLASS, 'stdClass');
} while ($stmt->nextRowset());

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.

How to get the last generated value for a custom PRIMARY KEY column in ZF2?

With Zend\Db\Adapter\Driver\ResultInterface#getGeneratedValue() Zend\Db provides a simple way to get the ID of the last INSERTed entry, e.g.:
$action = new Insert('my_table');
$action->values($data);
$sql = new Sql($this->dbAdapter);
$statement = $sql->prepareStatementForSqlObject($action);
$result = $statement->execute();
$newId = $result->getGeneratedValue();
But it seems only to work, if the PRIMARY KEY column calls "id". How to retrieve the generatedValue for a PRIMARY KEY defined on another column?
Preferably use Zend\Db\TableGateway\TableGateway offering the method getLastInsertValue().
use Zend\Db\TableGateway\TableGateway;
$myTable = new TableGateway('my_table', $this->dbAdapter);
$action = new Insert('my_table');
$action->values($data);
$myTable->insertWith($action);
$newId = $myTable->getLastInsertValue();
$this->dbAdapter->getDriver()->getLastGeneratedValue();

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 - retrieving bottom row id from table

I am trying to retrieve the latest entered if from the 'order' table within my database.
I'm currently trying to use the following method:
public function getLatestOrderId()
{
$adapter = $this->adapter;
$sql = new Sql($adapter);
$select = $sql->select();
$select->columns(array('id' => 'orderId'));
$select->from('order');
$select->order('orderId DESC');
$select->limit(1);
$statement = $sql->prepareStatementForSqlObject($select);
$statement->execute();
}
I have a feeling that this should be working. I am then accessing this method in the controller like so:
$orderId = $this->getOrderTable()->getLatestOrderId();
I have realised that at present the getLatestOrderId method does not actually return anything e.g. return $statement->execute();. This is because I receive an error saying that the sql result could not be converted to a string.
I'm insure what is going wrong, so any help would be much appreciated.
Thank you.

how to execute MYSQL stored procedure in zend framework 2 with multiple result set

How to execute MYSQL stored procedure in zend framework 2 with multiple result set means if sp have multiple select query then how can i get all the result in array and how i pass dynamic value in sp for insert and update data in table.
thanks..
I recently wrote a small article about this. The solution I found is not a generic one and assumes that you are using PDO. I am not sure whether it works with other databases than MySQL. It is possible that there is a better and more generic way of doing this that I am not aware of.
$driver = $this->dbAdapter->getDriver();
$connection = $driver->getConnection();
$result = $connection->execute('CALL sp_get_profile_for_display (123)');
$statement = $result->getResource();
// Result set 1
$resultSet1 = $statement->fetchAll(\PDO::FETCH_OBJ);
foreach ($resultSet1 as $row) {
$something = $row->some_column;
}
// Result set 2
$statement->nextRowSet(); // Advance to the second result set
$resultSet2 = $statement->fetchAll(\PDO::FETCH_OBJ);
foreach ($resultSet2 as $row) {
/* Do something */
}
// Result set 3
$statement->nextRowSet(); // Advance to the third result set
$resultSet3 = $statement->fetchAll(\PDO::FETCH_OBJ);
foreach ($resultSet3 as $row) {
/* Do something */
}
Replace the 123 with the data you wish to pass to the stored procedure as a parameter. If using user supplied data, remember to escape it to prevent SQL injection!

Resources