EntityManager.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use Exception;
  21. use Doctrine\Common\EventManager;
  22. use Doctrine\Common\Persistence\ObjectManager;
  23. use Doctrine\DBAL\Connection;
  24. use Doctrine\DBAL\LockMode;
  25. use Doctrine\ORM\Mapping\ClassMetadata;
  26. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  27. use Doctrine\ORM\Query\ResultSetMapping;
  28. use Doctrine\ORM\Proxy\ProxyFactory;
  29. use Doctrine\ORM\Query\FilterCollection;
  30. use Doctrine\Common\Util\ClassUtils;
  31. /**
  32. * The EntityManager is the central access point to ORM functionality.
  33. *
  34. * It is a facade to all different ORM subsystems such as UnitOfWork,
  35. * Query Language and Repository API. Instantiation is done through
  36. * the static create() method. The quickest way to obtain a fully
  37. * configured EntityManager is:
  38. *
  39. * use Doctrine\ORM\Tools\Setup;
  40. * use Doctrine\ORM\EntityManager;
  41. *
  42. * $paths = array('/path/to/entity/mapping/files');
  43. *
  44. * $config = Setup::createAnnotationMetadataConfiguration($paths);
  45. * $dbParams = array('driver' => 'pdo_sqlite', 'memory' => true);
  46. * $entityManager = EntityManager::create($dbParams, $config);
  47. *
  48. * For more information see
  49. * {@link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html}
  50. *
  51. * You should never attempt to inherit from the EntityManager: Inheritance
  52. * is not a valid extension point for the EntityManager. Instead you
  53. * should take a look at the {@see \Doctrine\ORM\Decorator\EntityManagerDecorator}
  54. * and wrap your entity manager in a decorator.
  55. *
  56. * @since 2.0
  57. * @author Benjamin Eberlei <kontakt@beberlei.de>
  58. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  59. * @author Jonathan Wage <jonwage@gmail.com>
  60. * @author Roman Borschel <roman@code-factory.org>
  61. */
  62. /* final */class EntityManager implements EntityManagerInterface
  63. {
  64. /**
  65. * The used Configuration.
  66. *
  67. * @var \Doctrine\ORM\Configuration
  68. */
  69. private $config;
  70. /**
  71. * The database connection used by the EntityManager.
  72. *
  73. * @var \Doctrine\DBAL\Connection
  74. */
  75. private $conn;
  76. /**
  77. * The metadata factory, used to retrieve the ORM metadata of entity classes.
  78. *
  79. * @var \Doctrine\ORM\Mapping\ClassMetadataFactory
  80. */
  81. private $metadataFactory;
  82. /**
  83. * The UnitOfWork used to coordinate object-level transactions.
  84. *
  85. * @var \Doctrine\ORM\UnitOfWork
  86. */
  87. private $unitOfWork;
  88. /**
  89. * The event manager that is the central point of the event system.
  90. *
  91. * @var \Doctrine\Common\EventManager
  92. */
  93. private $eventManager;
  94. /**
  95. * The proxy factory used to create dynamic proxies.
  96. *
  97. * @var \Doctrine\ORM\Proxy\ProxyFactory
  98. */
  99. private $proxyFactory;
  100. /**
  101. * The repository factory used to create dynamic repositories.
  102. *
  103. * @var \Doctrine\ORM\Repository\RepositoryFactory
  104. */
  105. private $repositoryFactory;
  106. /**
  107. * The expression builder instance used to generate query expressions.
  108. *
  109. * @var \Doctrine\ORM\Query\Expr
  110. */
  111. private $expressionBuilder;
  112. /**
  113. * Whether the EntityManager is closed or not.
  114. *
  115. * @var bool
  116. */
  117. private $closed = false;
  118. /**
  119. * Collection of query filters.
  120. *
  121. * @var \Doctrine\ORM\Query\FilterCollection
  122. */
  123. private $filterCollection;
  124. /**
  125. * Creates a new EntityManager that operates on the given database connection
  126. * and uses the given Configuration and EventManager implementations.
  127. *
  128. * @param \Doctrine\DBAL\Connection $conn
  129. * @param \Doctrine\ORM\Configuration $config
  130. * @param \Doctrine\Common\EventManager $eventManager
  131. */
  132. protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager)
  133. {
  134. $this->conn = $conn;
  135. $this->config = $config;
  136. $this->eventManager = $eventManager;
  137. $metadataFactoryClassName = $config->getClassMetadataFactoryName();
  138. $this->metadataFactory = new $metadataFactoryClassName;
  139. $this->metadataFactory->setEntityManager($this);
  140. $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());
  141. $this->repositoryFactory = $config->getRepositoryFactory();
  142. $this->unitOfWork = new UnitOfWork($this);
  143. $this->proxyFactory = new ProxyFactory(
  144. $this,
  145. $config->getProxyDir(),
  146. $config->getProxyNamespace(),
  147. $config->getAutoGenerateProxyClasses()
  148. );
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. public function getConnection()
  154. {
  155. return $this->conn;
  156. }
  157. /**
  158. * Gets the metadata factory used to gather the metadata of classes.
  159. *
  160. * @return \Doctrine\ORM\Mapping\ClassMetadataFactory
  161. */
  162. public function getMetadataFactory()
  163. {
  164. return $this->metadataFactory;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public function getExpressionBuilder()
  170. {
  171. if ($this->expressionBuilder === null) {
  172. $this->expressionBuilder = new Query\Expr;
  173. }
  174. return $this->expressionBuilder;
  175. }
  176. /**
  177. * {@inheritDoc}
  178. */
  179. public function beginTransaction()
  180. {
  181. $this->conn->beginTransaction();
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function transactional($func)
  187. {
  188. if (!is_callable($func)) {
  189. throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"');
  190. }
  191. $this->conn->beginTransaction();
  192. try {
  193. $return = call_user_func($func, $this);
  194. $this->flush();
  195. $this->conn->commit();
  196. return $return ?: true;
  197. } catch (Exception $e) {
  198. $this->close();
  199. $this->conn->rollback();
  200. throw $e;
  201. }
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function commit()
  207. {
  208. $this->conn->commit();
  209. }
  210. /**
  211. * {@inheritDoc}
  212. */
  213. public function rollback()
  214. {
  215. $this->conn->rollback();
  216. }
  217. /**
  218. * Returns the ORM metadata descriptor for a class.
  219. *
  220. * The class name must be the fully-qualified class name without a leading backslash
  221. * (as it is returned by get_class($obj)) or an aliased class name.
  222. *
  223. * Examples:
  224. * MyProject\Domain\User
  225. * sales:PriceRequest
  226. *
  227. * @param string $className
  228. *
  229. * @return \Doctrine\ORM\Mapping\ClassMetadata
  230. *
  231. * @internal Performance-sensitive method.
  232. */
  233. public function getClassMetadata($className)
  234. {
  235. return $this->metadataFactory->getMetadataFor($className);
  236. }
  237. /**
  238. * {@inheritDoc}
  239. */
  240. public function createQuery($dql = '')
  241. {
  242. $query = new Query($this);
  243. if ( ! empty($dql)) {
  244. $query->setDql($dql);
  245. }
  246. return $query;
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. public function createNamedQuery($name)
  252. {
  253. return $this->createQuery($this->config->getNamedQuery($name));
  254. }
  255. /**
  256. * {@inheritDoc}
  257. */
  258. public function createNativeQuery($sql, ResultSetMapping $rsm)
  259. {
  260. $query = new NativeQuery($this);
  261. $query->setSql($sql);
  262. $query->setResultSetMapping($rsm);
  263. return $query;
  264. }
  265. /**
  266. * {@inheritDoc}
  267. */
  268. public function createNamedNativeQuery($name)
  269. {
  270. list($sql, $rsm) = $this->config->getNamedNativeQuery($name);
  271. return $this->createNativeQuery($sql, $rsm);
  272. }
  273. /**
  274. * {@inheritDoc}
  275. */
  276. public function createQueryBuilder()
  277. {
  278. return new QueryBuilder($this);
  279. }
  280. /**
  281. * Flushes all changes to objects that have been queued up to now to the database.
  282. * This effectively synchronizes the in-memory state of managed objects with the
  283. * database.
  284. *
  285. * If an entity is explicitly passed to this method only this entity and
  286. * the cascade-persist semantics + scheduled inserts/removals are synchronized.
  287. *
  288. * @param null|object|array $entity
  289. *
  290. * @return void
  291. *
  292. * @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that
  293. * makes use of optimistic locking fails.
  294. */
  295. public function flush($entity = null)
  296. {
  297. $this->errorIfClosed();
  298. $this->unitOfWork->commit($entity);
  299. }
  300. /**
  301. * Finds an Entity by its identifier.
  302. *
  303. * @param string $entityName
  304. * @param mixed $id
  305. * @param integer $lockMode
  306. * @param integer|null $lockVersion
  307. *
  308. * @return object|null The entity instance or NULL if the entity can not be found.
  309. *
  310. * @throws OptimisticLockException
  311. * @throws ORMInvalidArgumentException
  312. * @throws TransactionRequiredException
  313. * @throws ORMException
  314. */
  315. public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion = null)
  316. {
  317. $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
  318. if (is_object($id) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($id))) {
  319. $id = $this->unitOfWork->getSingleIdentifierValue($id);
  320. if ($id === null) {
  321. throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
  322. }
  323. }
  324. if ( ! is_array($id)) {
  325. $id = array($class->identifier[0] => $id);
  326. }
  327. $sortedId = array();
  328. foreach ($class->identifier as $identifier) {
  329. if ( ! isset($id[$identifier])) {
  330. throw ORMException::missingIdentifierField($class->name, $identifier);
  331. }
  332. $sortedId[$identifier] = $id[$identifier];
  333. }
  334. $unitOfWork = $this->getUnitOfWork();
  335. // Check identity map first
  336. if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) {
  337. if ( ! ($entity instanceof $class->name)) {
  338. return null;
  339. }
  340. switch ($lockMode) {
  341. case LockMode::OPTIMISTIC:
  342. $this->lock($entity, $lockMode, $lockVersion);
  343. break;
  344. case LockMode::PESSIMISTIC_READ:
  345. case LockMode::PESSIMISTIC_WRITE:
  346. $persister = $unitOfWork->getEntityPersister($class->name);
  347. $persister->refresh($sortedId, $entity, $lockMode);
  348. break;
  349. }
  350. return $entity; // Hit!
  351. }
  352. $persister = $unitOfWork->getEntityPersister($class->name);
  353. switch ($lockMode) {
  354. case LockMode::NONE:
  355. return $persister->load($sortedId);
  356. case LockMode::OPTIMISTIC:
  357. if ( ! $class->isVersioned) {
  358. throw OptimisticLockException::notVersioned($class->name);
  359. }
  360. $entity = $persister->load($sortedId);
  361. $unitOfWork->lock($entity, $lockMode, $lockVersion);
  362. return $entity;
  363. default:
  364. if ( ! $this->getConnection()->isTransactionActive()) {
  365. throw TransactionRequiredException::transactionRequired();
  366. }
  367. return $persister->load($sortedId, null, null, array(), $lockMode);
  368. }
  369. }
  370. /**
  371. * {@inheritDoc}
  372. */
  373. public function getReference($entityName, $id)
  374. {
  375. $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
  376. if ( ! is_array($id)) {
  377. $id = array($class->identifier[0] => $id);
  378. }
  379. $sortedId = array();
  380. foreach ($class->identifier as $identifier) {
  381. if ( ! isset($id[$identifier])) {
  382. throw ORMException::missingIdentifierField($class->name, $identifier);
  383. }
  384. $sortedId[$identifier] = $id[$identifier];
  385. }
  386. // Check identity map first, if its already in there just return it.
  387. if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) {
  388. return ($entity instanceof $class->name) ? $entity : null;
  389. }
  390. if ($class->subClasses) {
  391. return $this->find($entityName, $sortedId);
  392. }
  393. if ( ! is_array($sortedId)) {
  394. $sortedId = array($class->identifier[0] => $sortedId);
  395. }
  396. $entity = $this->proxyFactory->getProxy($class->name, $sortedId);
  397. $this->unitOfWork->registerManaged($entity, $sortedId, array());
  398. return $entity;
  399. }
  400. /**
  401. * {@inheritDoc}
  402. */
  403. public function getPartialReference($entityName, $identifier)
  404. {
  405. $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));
  406. // Check identity map first, if its already in there just return it.
  407. if (($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) !== false) {
  408. return ($entity instanceof $class->name) ? $entity : null;
  409. }
  410. if ( ! is_array($identifier)) {
  411. $identifier = array($class->identifier[0] => $identifier);
  412. }
  413. $entity = $class->newInstance();
  414. $class->setIdentifierValues($entity, $identifier);
  415. $this->unitOfWork->registerManaged($entity, $identifier, array());
  416. $this->unitOfWork->markReadOnly($entity);
  417. return $entity;
  418. }
  419. /**
  420. * Clears the EntityManager. All entities that are currently managed
  421. * by this EntityManager become detached.
  422. *
  423. * @param string|null $entityName if given, only entities of this type will get detached
  424. *
  425. * @return void
  426. */
  427. public function clear($entityName = null)
  428. {
  429. $this->unitOfWork->clear($entityName);
  430. }
  431. /**
  432. * {@inheritDoc}
  433. */
  434. public function close()
  435. {
  436. $this->clear();
  437. $this->closed = true;
  438. }
  439. /**
  440. * Tells the EntityManager to make an instance managed and persistent.
  441. *
  442. * The entity will be entered into the database at or before transaction
  443. * commit or as a result of the flush operation.
  444. *
  445. * NOTE: The persist operation always considers entities that are not yet known to
  446. * this EntityManager as NEW. Do not pass detached entities to the persist operation.
  447. *
  448. * @param object $entity The instance to make managed and persistent.
  449. *
  450. * @return void
  451. *
  452. * @throws ORMInvalidArgumentException
  453. */
  454. public function persist($entity)
  455. {
  456. if ( ! is_object($entity)) {
  457. throw ORMInvalidArgumentException::invalidObject('EntityManager#persist()' , $entity);
  458. }
  459. $this->errorIfClosed();
  460. $this->unitOfWork->persist($entity);
  461. }
  462. /**
  463. * Removes an entity instance.
  464. *
  465. * A removed entity will be removed from the database at or before transaction commit
  466. * or as a result of the flush operation.
  467. *
  468. * @param object $entity The entity instance to remove.
  469. *
  470. * @return void
  471. *
  472. * @throws ORMInvalidArgumentException
  473. */
  474. public function remove($entity)
  475. {
  476. if ( ! is_object($entity)) {
  477. throw ORMInvalidArgumentException::invalidObject('EntityManager#remove()' , $entity);
  478. }
  479. $this->errorIfClosed();
  480. $this->unitOfWork->remove($entity);
  481. }
  482. /**
  483. * Refreshes the persistent state of an entity from the database,
  484. * overriding any local changes that have not yet been persisted.
  485. *
  486. * @param object $entity The entity to refresh.
  487. *
  488. * @return void
  489. *
  490. * @throws ORMInvalidArgumentException
  491. */
  492. public function refresh($entity)
  493. {
  494. if ( ! is_object($entity)) {
  495. throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()' , $entity);
  496. }
  497. $this->errorIfClosed();
  498. $this->unitOfWork->refresh($entity);
  499. }
  500. /**
  501. * Detaches an entity from the EntityManager, causing a managed entity to
  502. * become detached. Unflushed changes made to the entity if any
  503. * (including removal of the entity), will not be synchronized to the database.
  504. * Entities which previously referenced the detached entity will continue to
  505. * reference it.
  506. *
  507. * @param object $entity The entity to detach.
  508. *
  509. * @return void
  510. *
  511. * @throws ORMInvalidArgumentException
  512. */
  513. public function detach($entity)
  514. {
  515. if ( ! is_object($entity)) {
  516. throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()' , $entity);
  517. }
  518. $this->unitOfWork->detach($entity);
  519. }
  520. /**
  521. * Merges the state of a detached entity into the persistence context
  522. * of this EntityManager and returns the managed copy of the entity.
  523. * The entity passed to merge will not become associated/managed with this EntityManager.
  524. *
  525. * @param object $entity The detached entity to merge into the persistence context.
  526. *
  527. * @return object The managed copy of the entity.
  528. *
  529. * @throws ORMInvalidArgumentException
  530. */
  531. public function merge($entity)
  532. {
  533. if ( ! is_object($entity)) {
  534. throw ORMInvalidArgumentException::invalidObject('EntityManager#merge()' , $entity);
  535. }
  536. $this->errorIfClosed();
  537. return $this->unitOfWork->merge($entity);
  538. }
  539. /**
  540. * {@inheritDoc}
  541. *
  542. * @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e:
  543. * Fatal error: Maximum function nesting level of '100' reached, aborting!
  544. */
  545. public function copy($entity, $deep = false)
  546. {
  547. throw new \BadMethodCallException("Not implemented.");
  548. }
  549. /**
  550. * {@inheritDoc}
  551. */
  552. public function lock($entity, $lockMode, $lockVersion = null)
  553. {
  554. $this->unitOfWork->lock($entity, $lockMode, $lockVersion);
  555. }
  556. /**
  557. * Gets the repository for an entity class.
  558. *
  559. * @param string $entityName The name of the entity.
  560. *
  561. * @return \Doctrine\ORM\EntityRepository The repository class.
  562. */
  563. public function getRepository($entityName)
  564. {
  565. return $this->repositoryFactory->getRepository($this, $entityName);
  566. }
  567. /**
  568. * Determines whether an entity instance is managed in this EntityManager.
  569. *
  570. * @param object $entity
  571. *
  572. * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise.
  573. */
  574. public function contains($entity)
  575. {
  576. return $this->unitOfWork->isScheduledForInsert($entity)
  577. || $this->unitOfWork->isInIdentityMap($entity)
  578. && ! $this->unitOfWork->isScheduledForDelete($entity);
  579. }
  580. /**
  581. * {@inheritDoc}
  582. */
  583. public function getEventManager()
  584. {
  585. return $this->eventManager;
  586. }
  587. /**
  588. * {@inheritDoc}
  589. */
  590. public function getConfiguration()
  591. {
  592. return $this->config;
  593. }
  594. /**
  595. * Throws an exception if the EntityManager is closed or currently not active.
  596. *
  597. * @return void
  598. *
  599. * @throws ORMException If the EntityManager is closed.
  600. */
  601. private function errorIfClosed()
  602. {
  603. if ($this->closed) {
  604. throw ORMException::entityManagerClosed();
  605. }
  606. }
  607. /**
  608. * {@inheritDoc}
  609. */
  610. public function isOpen()
  611. {
  612. return (!$this->closed);
  613. }
  614. /**
  615. * {@inheritDoc}
  616. */
  617. public function getUnitOfWork()
  618. {
  619. return $this->unitOfWork;
  620. }
  621. /**
  622. * {@inheritDoc}
  623. */
  624. public function getHydrator($hydrationMode)
  625. {
  626. return $this->newHydrator($hydrationMode);
  627. }
  628. /**
  629. * {@inheritDoc}
  630. */
  631. public function newHydrator($hydrationMode)
  632. {
  633. switch ($hydrationMode) {
  634. case Query::HYDRATE_OBJECT:
  635. return new Internal\Hydration\ObjectHydrator($this);
  636. case Query::HYDRATE_ARRAY:
  637. return new Internal\Hydration\ArrayHydrator($this);
  638. case Query::HYDRATE_SCALAR:
  639. return new Internal\Hydration\ScalarHydrator($this);
  640. case Query::HYDRATE_SINGLE_SCALAR:
  641. return new Internal\Hydration\SingleScalarHydrator($this);
  642. case Query::HYDRATE_SIMPLEOBJECT:
  643. return new Internal\Hydration\SimpleObjectHydrator($this);
  644. default:
  645. if (($class = $this->config->getCustomHydrationMode($hydrationMode)) !== null) {
  646. return new $class($this);
  647. }
  648. }
  649. throw ORMException::invalidHydrationMode($hydrationMode);
  650. }
  651. /**
  652. * {@inheritDoc}
  653. */
  654. public function getProxyFactory()
  655. {
  656. return $this->proxyFactory;
  657. }
  658. /**
  659. * {@inheritDoc}
  660. */
  661. public function initializeObject($obj)
  662. {
  663. $this->unitOfWork->initializeObject($obj);
  664. }
  665. /**
  666. * Factory method to create EntityManager instances.
  667. *
  668. * @param mixed $conn An array with the connection parameters or an existing Connection instance.
  669. * @param Configuration $config The Configuration instance to use.
  670. * @param EventManager $eventManager The EventManager instance to use.
  671. *
  672. * @return EntityManager The created EntityManager.
  673. *
  674. * @throws \InvalidArgumentException
  675. * @throws ORMException
  676. */
  677. public static function create($conn, Configuration $config, EventManager $eventManager = null)
  678. {
  679. if ( ! $config->getMetadataDriverImpl()) {
  680. throw ORMException::missingMappingDriverImpl();
  681. }
  682. switch (true) {
  683. case (is_array($conn)):
  684. $conn = \Doctrine\DBAL\DriverManager::getConnection(
  685. $conn, $config, ($eventManager ?: new EventManager())
  686. );
  687. break;
  688. case ($conn instanceof Connection):
  689. if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
  690. throw ORMException::mismatchedEventManager();
  691. }
  692. break;
  693. default:
  694. throw new \InvalidArgumentException("Invalid argument: " . $conn);
  695. }
  696. return new EntityManager($conn, $config, $conn->getEventManager());
  697. }
  698. /**
  699. * {@inheritDoc}
  700. */
  701. public function getFilters()
  702. {
  703. if (null === $this->filterCollection) {
  704. $this->filterCollection = new FilterCollection($this);
  705. }
  706. return $this->filterCollection;
  707. }
  708. /**
  709. * {@inheritDoc}
  710. */
  711. public function isFiltersStateClean()
  712. {
  713. return null === $this->filterCollection || $this->filterCollection->isClean();
  714. }
  715. /**
  716. * {@inheritDoc}
  717. */
  718. public function hasFilters()
  719. {
  720. return null !== $this->filterCollection;
  721. }
  722. }