Last active
August 29, 2015 14:21
-
-
Save hrach/405f64793b2e32aafab7 to your computer and use it in GitHub Desktop.
Orm: select entity for update
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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