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.
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>.
20 namespace Doctrine\ORM\Tools;
22 use Doctrine\ORM\Event\OnFlushEventArgs;
23 use Doctrine\ORM\Mapping\ClassMetadata;
24 use Doctrine\ORM\PersistentCollection;
25 use Doctrine\ORM\UnitOfWork;
28 * Use this logger to dump the identity map during the onFlush event. This is useful for debugging
29 * weird UnitOfWork behavior with complex operations.
31 class DebugUnitOfWorkListener
37 * Pass a stream and contet information for the debugging session.
39 * The stream can be php://output to print to the screen.
42 * @param string $context
44 public function __construct($file = 'php://output', $context = '')
47 $this->context = $context;
50 public function onFlush(OnFlushEventArgs $args)
52 $this->dumpIdentityMap($args->getEntityManager());
56 * Dump the contents of the identity map into a stream.
58 * @param EntityManager $em
61 public function dumpIdentityMap(EntityManager $em)
63 $uow = $em->getUnitOfWork();
64 $identityMap = $uow->getIdentityMap();
66 $fh = fopen($this->file, "x+");
67 if (count($identityMap) == 0) {
68 fwrite($fh, "Flush Operation [".$this->context."] - Empty identity map.\n");
72 fwrite($fh, "Flush Operation [".$this->context."] - Dumping identity map:\n");
73 foreach ($identityMap as $className => $map) {
74 fwrite($fh, "Class: ". $className . "\n");
75 foreach ($map as $entity) {
76 fwrite($fh, " Entity: " . $this->getIdString($entity, $uow) . " " . spl_object_hash($entity)."\n");
77 fwrite($fh, " Associations:\n");
79 $cm = $em->getClassMetadata($className);
80 foreach ($cm->associationMappings as $field => $assoc) {
81 fwrite($fh, " " . $field . " ");
82 $value = $cm->reflFields[$field]->getValue($entity);
84 if ($assoc['type'] & ClassMetadata::TO_ONE) {
85 if ($value === null) {
86 fwrite($fh, " NULL\n");
88 if ($value instanceof Proxy && !$value->__isInitialized__) {
89 fwrite($fh, "[PROXY] ");
92 fwrite($fh, $this->getIdString($value, $uow) . " " . spl_object_hash($value) . "\n");
95 $initialized = !($value instanceof PersistentCollection) || $value->isInitialized();
96 if ($value === null) {
97 fwrite($fh, " NULL\n");
98 } else if ($initialized) {
99 fwrite($fh, "[INITIALIZED] " . $this->getType($value). " " . count($value) . " elements\n");
100 foreach ($value as $obj) {
101 fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
104 fwrite($fh, "[PROXY] " . $this->getType($value) . " unknown element size\n");
105 foreach ($value->unwrap() as $obj) {
106 fwrite($fh, " " . $this->getIdString($obj, $uow) . " " . spl_object_hash($obj)."\n");
116 private function getType($var)
118 if (is_object($var)) {
119 $refl = new \ReflectionObject($var);
120 return $refl->getShortname();
122 return gettype($var);
126 private function getIdString($entity, $uow)
128 if ($uow->isInIdentityMap($entity)) {
129 $ids = $uow->getEntityIdentifier($entity);
131 foreach ($ids as $k => $v) {
132 $idstring .= $k."=".$v;
135 $idstring = "NEWOBJECT ";
138 $state = $uow->getEntityState($entity);
139 if ($state == UnitOfWork::STATE_NEW) {
140 $idstring .= " [NEW]";
141 } else if ($state == UnitOfWork::STATE_REMOVED) {
142 $idstring .= " [REMOVED]";
143 } else if ($state == UnitOfWork::STATE_MANAGED) {
144 $idstring .= " [MANAGED]";
145 } else if ($state == UnitOfwork::STATE_DETACHED) {
146 $idstring .= " [DETACHED]";