Skip to content

Instantly share code, notes, and snippets.

@hrach
Last active August 29, 2015 14:21
Show Gist options
  • Save hrach/405f64793b2e32aafab7 to your computer and use it in GitHub Desktop.
Save hrach/405f64793b2e32aafab7 to your computer and use it in GitHub Desktop.
Orm: select entity for update
<?php
use Nextras\Orm\Collection\ICollection;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Mapper\Dbal\Mapper;
use Nextras\Orm\Mapper\Dbal\DbalCollection;
class BaseMapper extends Mapper
{
/**
* @param DbalCollection $collection
* @return IEntity|NULL
*/
public function getForUpdate(DbalCollection $collection)
{
$builder = $collection->getQueryBuilder();
$sql = $builder->getQuerySql() . ' LIMIT 1 FOR UPDATE';
$this->beginTransaction();
$result = $this->connection->queryArgs($sql, $builder->getQueryParameters())->fetch();
if ($result) {
$result = $this->getRepository()->hydrateEntity($result->toArray());
}
return $result;
}
}
<?php
$collection = $orm->books->findBy([]);
// $collection = $orm->books->findById(...);
$book = $orm->books->getMapper()->getForUpdate($collection);
// $book->title = ''; ...
$this->books->persistAndFlush($book)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment