-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Last inserted ID #14
Comments
We should check how all implementations return this value and return a structure. Good point. |
In |
@bartvanhoutte thanks for the response! -> Temporary PR - #16 |
@mmoreram I've just tested #16 with SQLite and it works, so looks good to me. I'm afraid I can't help you with pgsql. Small question on the side. I see |
@bartvanhoutte yes, you're right. We should change this behavior. |
https://stackoverflow.com/a/2944481/2159370 there is an answer for postgresql here. Let me know if I can help with anything. |
/**
* @param string $table
* @param array $values
*
* @return PromiseInterface
*/
public function insert(
string $table,
array $values
): PromiseInterface {
$queryBuilder = $this
->createQueryBuilder()
->insert($table)
->values(array_combine(
array_keys($values),
array_fill(0, count($values), '?')
))
->setParameters(array_values($values));
if($this->driver instanceof PostgreSQLDriver) {
return $this->queryBySQL($queryBuilder->getSQL() . ' RETURNING id', $queryBuilder->getParameters());
}
return $this->query($queryBuilder);
} I tried this and this is returning correct result for insertId, I get an array with index |
/**
* @param string $table
* @param array $values
*
* @return PromiseInterface
*/
public function insert(
string $table,
array $values
): PromiseInterface {
$queryBuilder = $this
->createQueryBuilder()
->insert($table)
->values(array_combine(
array_keys($values),
array_fill(0, count($values), '?')
))
->setParameters(array_values($values));
if($this->driver instanceof PostgreSQLDriver) {
$query = sprintf("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME = '%s'", $table);
return $this->queryBySQL($query)->then(function (Result $response) use($queryBuilder){
$allRows = $response->fetchAllRows();
$fields = array_map(function ($item){
return $item['column_name'];
}, $allRows);
return $this->queryBySQL($queryBuilder->getSQL() . ' RETURNING ' . implode(',', $fields), $queryBuilder->getParameters());
});
}
return $this->query($queryBuilder);
} this seems to work perfectly, fetches the column as well. |
Any news to merge? Seems working fine? |
@alexmorbo I added some extra changes on the PR - #19 TBH, the PostgreSQL last id implementation is not the best one, but right now, is the one I see. |
Is this issue not resolved already? |
How do you get the last inserted ID (auto increment) after inserting something into the DB?
The text was updated successfully, but these errors were encountered: