Quote value into Zend Framework 2

2019-09-06 14:57发布

问题:

I'm working on an application using ZF2. In my application, I have to insert many rows in a database (about 900).

I've got a table model for this, so I first try to do :

$table->insert(array('x' => $x, 'y' => $y));

in my loop. This technically work, but this is so slow that I can hardly insert half of the datas before php's timeout (and I can't change the timeout).

Then, I've decide to use a prepared statment. So I've prepared it outside of the loop, then execute it in my loop... it was even slower.

So, I decide to stop using ZF2 tools, as they seems to be too slow to be used in my case, and i've created my own request. I'm using mysql, so i can do a single request with all my values. But I can't find any method in any of the interface to escape my values...

Is there any way to do this ?

Thank you for your help and sorry for my poor english.

回答1:

If you want to perform raw queries you can do so using the Database Adapter:

$sql = 'SELECT * FROM '
    . $adapter->platform->quoteIdentifier('users')
    . ' WHERE ' . $adapter->platform->quoteIdentifier('id') . ' = ' . $adapter->driver->formatParameterName('id');

/* @var $statement \Zend\Db\Adapter\Driver\StatementInterface */
$statement = $adapter->query($sql);
$parameters = array('id' => 99);

/* @var $results Zend\Db\ResultSet\ResultSet */
$results = $statement->execute($parameters);

$row = $results->current();


回答2:

use transactions: http://dev.mysql.com/doc/refman/5.0/en/commit.html Than will help you to decrease the execution time