IdentityGenerator.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Id;
  20. use Doctrine\ORM\EntityManager;
  21. /**
  22. * Id generator that obtains IDs from special "identity" columns. These are columns
  23. * that automatically get a database-generated, auto-incremented identifier on INSERT.
  24. * This generator obtains the last insert id after such an insert.
  25. */
  26. class IdentityGenerator extends AbstractIdGenerator
  27. {
  28. /** @var string The name of the sequence to pass to lastInsertId(), if any. */
  29. private $_seqName;
  30. /**
  31. * @param string $seqName The name of the sequence to pass to lastInsertId()
  32. * to obtain the last generated identifier within the current
  33. * database session/connection, if any.
  34. */
  35. public function __construct($seqName = null)
  36. {
  37. $this->_seqName = $seqName;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function generate(EntityManager $em, $entity)
  43. {
  44. return $em->getConnection()->lastInsertId($this->_seqName);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function isPostInsertGenerator()
  50. {
  51. return true;
  52. }
  53. }