PersistentCollection.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 Doctrine\ORM\Mapping\ClassMetadata;
  21. use Doctrine\Common\Collections\Collection;
  22. use Doctrine\Common\Collections\ArrayCollection;
  23. use Doctrine\Common\Collections\Selectable;
  24. use Doctrine\Common\Collections\Criteria;
  25. use Closure;
  26. /**
  27. * A PersistentCollection represents a collection of elements that have persistent state.
  28. *
  29. * Collections of entities represent only the associations (links) to those entities.
  30. * That means, if the collection is part of a many-many mapping and you remove
  31. * entities from the collection, only the links in the relation table are removed (on flush).
  32. * Similarly, if you remove entities from a collection that is part of a one-many
  33. * mapping this will only result in the nulling out of the foreign keys on flush.
  34. *
  35. * @since 2.0
  36. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  37. * @author Roman Borschel <roman@code-factory.org>
  38. * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
  39. * @author Stefano Rodriguez <stefano.rodriguez@fubles.com>
  40. * @todo Design for inheritance to allow custom implementations?
  41. */
  42. final class PersistentCollection implements Collection, Selectable
  43. {
  44. /**
  45. * A snapshot of the collection at the moment it was fetched from the database.
  46. * This is used to create a diff of the collection at commit time.
  47. *
  48. * @var array
  49. */
  50. private $snapshot = array();
  51. /**
  52. * The entity that owns this collection.
  53. *
  54. * @var object
  55. */
  56. private $owner;
  57. /**
  58. * The association mapping the collection belongs to.
  59. * This is currently either a OneToManyMapping or a ManyToManyMapping.
  60. *
  61. * @var array
  62. */
  63. private $association;
  64. /**
  65. * The EntityManager that manages the persistence of the collection.
  66. *
  67. * @var \Doctrine\ORM\EntityManager
  68. */
  69. private $em;
  70. /**
  71. * The name of the field on the target entities that points to the owner
  72. * of the collection. This is only set if the association is bi-directional.
  73. *
  74. * @var string
  75. */
  76. private $backRefFieldName;
  77. /**
  78. * The class descriptor of the collection's entity type.
  79. *
  80. * @var ClassMetadata
  81. */
  82. private $typeClass;
  83. /**
  84. * Whether the collection is dirty and needs to be synchronized with the database
  85. * when the UnitOfWork that manages its persistent state commits.
  86. *
  87. * @var boolean
  88. */
  89. private $isDirty = false;
  90. /**
  91. * Whether the collection has already been initialized.
  92. *
  93. * @var boolean
  94. */
  95. private $initialized = true;
  96. /**
  97. * The wrapped Collection instance.
  98. *
  99. * @var Collection
  100. */
  101. private $coll;
  102. /**
  103. * Creates a new persistent collection.
  104. *
  105. * @param EntityManager $em The EntityManager the collection will be associated with.
  106. * @param ClassMetadata $class The class descriptor of the entity type of this collection.
  107. * @param array $coll The collection elements.
  108. */
  109. public function __construct(EntityManager $em, $class, $coll)
  110. {
  111. $this->coll = $coll;
  112. $this->em = $em;
  113. $this->typeClass = $class;
  114. }
  115. /**
  116. * INTERNAL:
  117. * Sets the collection's owning entity together with the AssociationMapping that
  118. * describes the association between the owner and the elements of the collection.
  119. *
  120. * @param object $entity
  121. * @param array $assoc
  122. *
  123. * @return void
  124. */
  125. public function setOwner($entity, array $assoc)
  126. {
  127. $this->owner = $entity;
  128. $this->association = $assoc;
  129. $this->backRefFieldName = $assoc['inversedBy'] ?: $assoc['mappedBy'];
  130. }
  131. /**
  132. * INTERNAL:
  133. * Gets the collection owner.
  134. *
  135. * @return object
  136. */
  137. public function getOwner()
  138. {
  139. return $this->owner;
  140. }
  141. /**
  142. * @return Mapping\ClassMetadata
  143. */
  144. public function getTypeClass()
  145. {
  146. return $this->typeClass;
  147. }
  148. /**
  149. * INTERNAL:
  150. * Adds an element to a collection during hydration. This will automatically
  151. * complete bidirectional associations in the case of a one-to-many association.
  152. *
  153. * @param mixed $element The element to add.
  154. *
  155. * @return void
  156. */
  157. public function hydrateAdd($element)
  158. {
  159. $this->coll->add($element);
  160. // If _backRefFieldName is set and its a one-to-many association,
  161. // we need to set the back reference.
  162. if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
  163. // Set back reference to owner
  164. $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
  165. $element, $this->owner
  166. );
  167. $this->em->getUnitOfWork()->setOriginalEntityProperty(
  168. spl_object_hash($element), $this->backRefFieldName, $this->owner
  169. );
  170. }
  171. }
  172. /**
  173. * INTERNAL:
  174. * Sets a keyed element in the collection during hydration.
  175. *
  176. * @param mixed $key The key to set.
  177. * @param mixed $element The element to set.
  178. *
  179. * @return void
  180. */
  181. public function hydrateSet($key, $element)
  182. {
  183. $this->coll->set($key, $element);
  184. // If _backRefFieldName is set, then the association is bidirectional
  185. // and we need to set the back reference.
  186. if ($this->backRefFieldName && $this->association['type'] === ClassMetadata::ONE_TO_MANY) {
  187. // Set back reference to owner
  188. $this->typeClass->reflFields[$this->backRefFieldName]->setValue(
  189. $element, $this->owner
  190. );
  191. }
  192. }
  193. /**
  194. * Initializes the collection by loading its contents from the database
  195. * if the collection is not yet initialized.
  196. *
  197. * @return void
  198. */
  199. public function initialize()
  200. {
  201. if ($this->initialized || ! $this->association) {
  202. return;
  203. }
  204. // Has NEW objects added through add(). Remember them.
  205. $newObjects = array();
  206. if ($this->isDirty) {
  207. $newObjects = $this->coll->toArray();
  208. }
  209. $this->coll->clear();
  210. $this->em->getUnitOfWork()->loadCollection($this);
  211. $this->takeSnapshot();
  212. // Reattach NEW objects added through add(), if any.
  213. if ($newObjects) {
  214. foreach ($newObjects as $obj) {
  215. $this->coll->add($obj);
  216. }
  217. $this->isDirty = true;
  218. }
  219. $this->initialized = true;
  220. }
  221. /**
  222. * INTERNAL:
  223. * Tells this collection to take a snapshot of its current state.
  224. *
  225. * @return void
  226. */
  227. public function takeSnapshot()
  228. {
  229. $this->snapshot = $this->coll->toArray();
  230. $this->isDirty = false;
  231. }
  232. /**
  233. * INTERNAL:
  234. * Returns the last snapshot of the elements in the collection.
  235. *
  236. * @return array The last snapshot of the elements.
  237. */
  238. public function getSnapshot()
  239. {
  240. return $this->snapshot;
  241. }
  242. /**
  243. * INTERNAL:
  244. * getDeleteDiff
  245. *
  246. * @return array
  247. */
  248. public function getDeleteDiff()
  249. {
  250. return array_udiff_assoc(
  251. $this->snapshot,
  252. $this->coll->toArray(),
  253. function($a, $b) { return $a === $b ? 0 : 1; }
  254. );
  255. }
  256. /**
  257. * INTERNAL:
  258. * getInsertDiff
  259. *
  260. * @return array
  261. */
  262. public function getInsertDiff()
  263. {
  264. return array_udiff_assoc(
  265. $this->coll->toArray(),
  266. $this->snapshot,
  267. function($a, $b) { return $a === $b ? 0 : 1; }
  268. );
  269. }
  270. /**
  271. * INTERNAL: Gets the association mapping of the collection.
  272. *
  273. * @return array
  274. */
  275. public function getMapping()
  276. {
  277. return $this->association;
  278. }
  279. /**
  280. * Marks this collection as changed/dirty.
  281. *
  282. * @return void
  283. */
  284. private function changed()
  285. {
  286. if ($this->isDirty) {
  287. return;
  288. }
  289. $this->isDirty = true;
  290. if ($this->association !== null &&
  291. $this->association['isOwningSide'] &&
  292. $this->association['type'] === ClassMetadata::MANY_TO_MANY &&
  293. $this->owner &&
  294. $this->em->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) {
  295. $this->em->getUnitOfWork()->scheduleForDirtyCheck($this->owner);
  296. }
  297. }
  298. /**
  299. * Gets a boolean flag indicating whether this collection is dirty which means
  300. * its state needs to be synchronized with the database.
  301. *
  302. * @return boolean TRUE if the collection is dirty, FALSE otherwise.
  303. */
  304. public function isDirty()
  305. {
  306. return $this->isDirty;
  307. }
  308. /**
  309. * Sets a boolean flag, indicating whether this collection is dirty.
  310. *
  311. * @param boolean $dirty Whether the collection should be marked dirty or not.
  312. *
  313. * @return void
  314. */
  315. public function setDirty($dirty)
  316. {
  317. $this->isDirty = $dirty;
  318. }
  319. /**
  320. * Sets the initialized flag of the collection, forcing it into that state.
  321. *
  322. * @param boolean $bool
  323. *
  324. * @return void
  325. */
  326. public function setInitialized($bool)
  327. {
  328. $this->initialized = $bool;
  329. }
  330. /**
  331. * Checks whether this collection has been initialized.
  332. *
  333. * @return boolean
  334. */
  335. public function isInitialized()
  336. {
  337. return $this->initialized;
  338. }
  339. /**
  340. * {@inheritdoc}
  341. */
  342. public function first()
  343. {
  344. $this->initialize();
  345. return $this->coll->first();
  346. }
  347. /**
  348. * {@inheritdoc}
  349. */
  350. public function last()
  351. {
  352. $this->initialize();
  353. return $this->coll->last();
  354. }
  355. /**
  356. * {@inheritdoc}
  357. */
  358. public function remove($key)
  359. {
  360. // TODO: If the keys are persistent as well (not yet implemented)
  361. // and the collection is not initialized and orphanRemoval is
  362. // not used we can issue a straight SQL delete/update on the
  363. // association (table). Without initializing the collection.
  364. $this->initialize();
  365. $removed = $this->coll->remove($key);
  366. if ( ! $removed) {
  367. return $removed;
  368. }
  369. $this->changed();
  370. if ($this->association !== null &&
  371. $this->association['type'] & ClassMetadata::TO_MANY &&
  372. $this->owner &&
  373. $this->association['orphanRemoval']) {
  374. $this->em->getUnitOfWork()->scheduleOrphanRemoval($removed);
  375. }
  376. return $removed;
  377. }
  378. /**
  379. * {@inheritdoc}
  380. */
  381. public function removeElement($element)
  382. {
  383. if ( ! $this->initialized && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY) {
  384. if ($this->coll->contains($element)) {
  385. return $this->coll->removeElement($element);
  386. }
  387. $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
  388. if ($persister->removeElement($this, $element)) {
  389. return $element;
  390. }
  391. return null;
  392. }
  393. $this->initialize();
  394. $removed = $this->coll->removeElement($element);
  395. if ( ! $removed) {
  396. return $removed;
  397. }
  398. $this->changed();
  399. if ($this->association !== null &&
  400. $this->association['type'] & ClassMetadata::TO_MANY &&
  401. $this->owner &&
  402. $this->association['orphanRemoval']) {
  403. $this->em->getUnitOfWork()->scheduleOrphanRemoval($element);
  404. }
  405. return $removed;
  406. }
  407. /**
  408. * {@inheritdoc}
  409. */
  410. public function containsKey($key)
  411. {
  412. $this->initialize();
  413. return $this->coll->containsKey($key);
  414. }
  415. /**
  416. * {@inheritdoc}
  417. */
  418. public function contains($element)
  419. {
  420. if ( ! $this->initialized && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY) {
  421. $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
  422. return $this->coll->contains($element) || $persister->contains($this, $element);
  423. }
  424. $this->initialize();
  425. return $this->coll->contains($element);
  426. }
  427. /**
  428. * {@inheritdoc}
  429. */
  430. public function exists(Closure $p)
  431. {
  432. $this->initialize();
  433. return $this->coll->exists($p);
  434. }
  435. /**
  436. * {@inheritdoc}
  437. */
  438. public function indexOf($element)
  439. {
  440. $this->initialize();
  441. return $this->coll->indexOf($element);
  442. }
  443. /**
  444. * {@inheritdoc}
  445. */
  446. public function get($key)
  447. {
  448. if ( ! $this->initialized
  449. && $this->association['type'] === Mapping\ClassMetadataInfo::ONE_TO_MANY
  450. && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY
  451. && isset($this->association['indexBy'])
  452. ) {
  453. if (!$this->typeClass->isIdentifierComposite && $this->typeClass->isIdentifier($this->association['indexBy'])) {
  454. return $this->em->find($this->typeClass->name, $key);
  455. }
  456. return $this->em->getUnitOfWork()->getCollectionPersister($this->association)->get($this, $key);
  457. }
  458. $this->initialize();
  459. return $this->coll->get($key);
  460. }
  461. /**
  462. * {@inheritdoc}
  463. */
  464. public function getKeys()
  465. {
  466. $this->initialize();
  467. return $this->coll->getKeys();
  468. }
  469. /**
  470. * {@inheritdoc}
  471. */
  472. public function getValues()
  473. {
  474. $this->initialize();
  475. return $this->coll->getValues();
  476. }
  477. /**
  478. * {@inheritdoc}
  479. */
  480. public function count()
  481. {
  482. if ( ! $this->initialized && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY) {
  483. $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
  484. return $persister->count($this) + ($this->isDirty ? $this->coll->count() : 0);
  485. }
  486. $this->initialize();
  487. return $this->coll->count();
  488. }
  489. /**
  490. * {@inheritdoc}
  491. */
  492. public function set($key, $value)
  493. {
  494. $this->initialize();
  495. $this->coll->set($key, $value);
  496. $this->changed();
  497. }
  498. /**
  499. * {@inheritdoc}
  500. */
  501. public function add($value)
  502. {
  503. $this->coll->add($value);
  504. $this->changed();
  505. return true;
  506. }
  507. /**
  508. * {@inheritdoc}
  509. */
  510. public function isEmpty()
  511. {
  512. $this->initialize();
  513. return $this->coll->isEmpty();
  514. }
  515. /**
  516. * {@inheritdoc}
  517. */
  518. public function getIterator()
  519. {
  520. $this->initialize();
  521. return $this->coll->getIterator();
  522. }
  523. /**
  524. * {@inheritdoc}
  525. */
  526. public function map(Closure $func)
  527. {
  528. $this->initialize();
  529. return $this->coll->map($func);
  530. }
  531. /**
  532. * {@inheritdoc}
  533. */
  534. public function filter(Closure $p)
  535. {
  536. $this->initialize();
  537. return $this->coll->filter($p);
  538. }
  539. /**
  540. * {@inheritdoc}
  541. */
  542. public function forAll(Closure $p)
  543. {
  544. $this->initialize();
  545. return $this->coll->forAll($p);
  546. }
  547. /**
  548. * {@inheritdoc}
  549. */
  550. public function partition(Closure $p)
  551. {
  552. $this->initialize();
  553. return $this->coll->partition($p);
  554. }
  555. /**
  556. * {@inheritdoc}
  557. */
  558. public function toArray()
  559. {
  560. $this->initialize();
  561. return $this->coll->toArray();
  562. }
  563. /**
  564. * {@inheritdoc}
  565. */
  566. public function clear()
  567. {
  568. if ($this->initialized && $this->isEmpty()) {
  569. return;
  570. }
  571. $uow = $this->em->getUnitOfWork();
  572. if ($this->association['type'] & ClassMetadata::TO_MANY &&
  573. $this->association['orphanRemoval'] &&
  574. $this->owner) {
  575. // we need to initialize here, as orphan removal acts like implicit cascadeRemove,
  576. // hence for event listeners we need the objects in memory.
  577. $this->initialize();
  578. foreach ($this->coll as $element) {
  579. $uow->scheduleOrphanRemoval($element);
  580. }
  581. }
  582. $this->coll->clear();
  583. $this->initialized = true; // direct call, {@link initialize()} is too expensive
  584. if ($this->association['isOwningSide'] && $this->owner) {
  585. $this->changed();
  586. $uow->scheduleCollectionDeletion($this);
  587. $this->takeSnapshot();
  588. }
  589. }
  590. /**
  591. * Called by PHP when this collection is serialized. Ensures that only the
  592. * elements are properly serialized.
  593. *
  594. * @return array
  595. *
  596. * @internal Tried to implement Serializable first but that did not work well
  597. * with circular references. This solution seems simpler and works well.
  598. */
  599. public function __sleep()
  600. {
  601. return array('coll', 'initialized');
  602. }
  603. /* ArrayAccess implementation */
  604. /**
  605. * {@inheritdoc}
  606. */
  607. public function offsetExists($offset)
  608. {
  609. return $this->containsKey($offset);
  610. }
  611. /**
  612. * {@inheritdoc}
  613. */
  614. public function offsetGet($offset)
  615. {
  616. return $this->get($offset);
  617. }
  618. /**
  619. * {@inheritdoc}
  620. */
  621. public function offsetSet($offset, $value)
  622. {
  623. if ( ! isset($offset)) {
  624. return $this->add($value);
  625. }
  626. return $this->set($offset, $value);
  627. }
  628. /**
  629. * {@inheritdoc}
  630. */
  631. public function offsetUnset($offset)
  632. {
  633. return $this->remove($offset);
  634. }
  635. /**
  636. * {@inheritdoc}
  637. */
  638. public function key()
  639. {
  640. $this->initialize();
  641. return $this->coll->key();
  642. }
  643. /**
  644. * {@inheritdoc}
  645. */
  646. public function current()
  647. {
  648. $this->initialize();
  649. return $this->coll->current();
  650. }
  651. /**
  652. * {@inheritdoc}
  653. */
  654. public function next()
  655. {
  656. $this->initialize();
  657. return $this->coll->next();
  658. }
  659. /**
  660. * Retrieves the wrapped Collection instance.
  661. *
  662. * @return \Doctrine\Common\Collections\Collection
  663. */
  664. public function unwrap()
  665. {
  666. return $this->coll;
  667. }
  668. /**
  669. * Extracts a slice of $length elements starting at position $offset from the Collection.
  670. *
  671. * If $length is null it returns all elements from $offset to the end of the Collection.
  672. * Keys have to be preserved by this method. Calling this method will only return the
  673. * selected slice and NOT change the elements contained in the collection slice is called on.
  674. *
  675. * @param int $offset
  676. * @param int|null $length
  677. *
  678. * @return array
  679. */
  680. public function slice($offset, $length = null)
  681. {
  682. if ( ! $this->initialized && ! $this->isDirty && $this->association['fetch'] === Mapping\ClassMetadataInfo::FETCH_EXTRA_LAZY) {
  683. $persister = $this->em->getUnitOfWork()->getCollectionPersister($this->association);
  684. return $persister->slice($this, $offset, $length);
  685. }
  686. $this->initialize();
  687. return $this->coll->slice($offset, $length);
  688. }
  689. /**
  690. * Cleans up internal state of cloned persistent collection.
  691. *
  692. * The following problems have to be prevented:
  693. * 1. Added entities are added to old PC
  694. * 2. New collection is not dirty, if reused on other entity nothing
  695. * changes.
  696. * 3. Snapshot leads to invalid diffs being generated.
  697. * 4. Lazy loading grabs entities from old owner object.
  698. * 5. New collection is connected to old owner and leads to duplicate keys.
  699. *
  700. * @return void
  701. */
  702. public function __clone()
  703. {
  704. if (is_object($this->coll)) {
  705. $this->coll = clone $this->coll;
  706. }
  707. $this->initialize();
  708. $this->owner = null;
  709. $this->snapshot = array();
  710. $this->changed();
  711. }
  712. /**
  713. * Selects all elements from a selectable that match the expression and
  714. * return a new collection containing these elements.
  715. *
  716. * @param \Doctrine\Common\Collections\Criteria $criteria
  717. *
  718. * @return Collection
  719. *
  720. * @throws \RuntimeException
  721. */
  722. public function matching(Criteria $criteria)
  723. {
  724. if ($this->isDirty) {
  725. $this->initialize();
  726. }
  727. if ($this->initialized) {
  728. return $this->coll->matching($criteria);
  729. }
  730. if ($this->association['type'] !== ClassMetadata::ONE_TO_MANY) {
  731. throw new \RuntimeException("Matching Criteria on PersistentCollection only works on OneToMany associations at the moment.");
  732. }
  733. $builder = Criteria::expr();
  734. $ownerExpression = $builder->eq($this->backRefFieldName, $this->owner);
  735. $expression = $criteria->getWhereExpression();
  736. $expression = $expression ? $builder->andX($expression, $ownerExpression) : $ownerExpression;
  737. $criteria->where($expression);
  738. $persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);
  739. return new ArrayCollection($persister->loadCriteria($criteria));
  740. }
  741. }