|
1 | | -<?php |
2 | | - |
3 | | -namespace ZF\Doctrine\ORM\DataValidation\Controller; |
4 | | - |
5 | | -use Zend\Mvc\Controller\AbstractActionController; |
6 | | -use Zend\View\Model\ViewModel; |
7 | | -use Zend\Console\Request as ConsoleRequest; |
8 | | -use Zend\Console\Adapter\AdapterInterface as Console; |
9 | | -use Zend\Console\ColorInterface as Color; |
10 | | -use Zend\Console\Prompt; |
11 | | -use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader; |
12 | | -use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
13 | | -use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
14 | | -use RuntimeException; |
15 | | -use NoteVault\Entity; |
16 | | - |
17 | | -class ForeignKeyController extends AbstractActionController |
18 | | -{ |
19 | | - public function relationshipAction() |
20 | | - { |
21 | | - // Make sure that we are running in a console and the user has not tricked our |
22 | | - // application into running this action from a public web server. |
23 | | - $request = $this->getRequest(); |
24 | | - if (!$request instanceof ConsoleRequest) { |
25 | | - throw new RuntimeException('You can only use this action from a console.'); |
26 | | - } |
27 | | - |
28 | | - $console = $this->getServiceLocator()->get('console'); |
29 | | - |
30 | | - $objectManagerAlias = $this->params()->fromRoute('object-manager'); |
31 | | - $objectManager = $this->getServiceLocator()->get($objectManagerAlias); |
32 | | - |
33 | | - $console->write( |
34 | | - 'Count' |
35 | | - . "\t" |
36 | | - . 'Distinct Values' |
37 | | - . "\t" |
38 | | - . 'Child Field' |
39 | | - . "\t" |
40 | | - . 'Child Entity' |
41 | | - . "\t" |
42 | | - . 'Parent Entity' |
43 | | - . "\t" |
44 | | - . 'SQL' |
45 | | - . "\n" |
46 | | - ); |
47 | | - |
48 | | - $allMetadata = $objectManager->getMetadataFactory()->getAllMetadata(); |
49 | | - foreach ($allMetadata as $metadata) { |
50 | | - |
51 | | - foreach ($metadata->getAssociationMappings() as $mapping) { |
52 | | - if ($mapping['type'] == 2) { |
53 | | - $queryBuilder = $objectManager->createQueryBuilder(); |
54 | | - $queryBuilder2 = $objectManager->createQueryBuilder(); |
55 | | - |
56 | | - $queryBuilder->select( |
57 | | - "count(child) as ct, count(distinct child." |
58 | | - . $mapping['fieldName'] |
59 | | - . ") as dst, '" |
60 | | - . $mapping['fieldName'] |
61 | | - . "' as childField, '" |
62 | | - . $metadata->getName() |
63 | | - . "' as childEntity, '" |
64 | | - . $mapping['targetEntity'] |
65 | | - . "' as parentEntity" |
66 | | - ) |
67 | | - ->from($metadata->getName(), 'child') |
68 | | - ->andWhere($queryBuilder->expr()->not( |
69 | | - $queryBuilder->expr()->exists( |
70 | | - $queryBuilder2->select('parent') |
71 | | - ->from($mapping['targetEntity'], 'parent') |
72 | | - ->andWhere($queryBuilder2->expr()->eq( |
73 | | - 'child.' . $mapping['fieldName'], |
74 | | - 'parent' |
75 | | - )) |
76 | | - ->getQuery() |
77 | | - ->getDql() |
78 | | - ) |
79 | | - ) |
80 | | - ) |
81 | | - ; |
82 | | - |
83 | | - // Do not query columns which are nullable and are null |
84 | | - $childFieldMapping = $objectManager->getMetadataFactory() |
85 | | - ->getMetadataFor($metadata->getName()) |
86 | | - ->getAssociationMapping($mapping['fieldName']) |
87 | | - ; |
88 | | - |
89 | | - if (! isset($childFieldMapping['joinColumns'][0]['nullable']) |
90 | | - || ! $childFieldMapping['joinColumns'][0]['nullable']) { |
91 | | - |
92 | | - $queryBuilder->andWhere($queryBuilder->expr()->isNotNull('child.' . $mapping['fieldName'])); |
93 | | - } |
94 | | - |
95 | | - $result = $queryBuilder->getQuery()->getOneOrNullResult(); |
96 | | - |
97 | | - if ($result['ct']) { |
98 | | - $childMapping = $objectManager->getMetadataFactory() |
99 | | - ->getMetadataFor($metadata->getName()); |
100 | | - |
101 | | - $console->write( |
102 | | - $result['ct'] |
103 | | - . "\t" |
104 | | - . $result['dst'] |
105 | | - . "\t" |
106 | | - . $result['childField'] |
107 | | - . "\t" |
108 | | - . $result['childEntity'] |
109 | | - . "\t" |
110 | | - . $result['parentEntity'] |
111 | | - . "\t" |
112 | | - . $queryBuilder->getQuery()->getSql() |
113 | | - . "\n" |
114 | | - ); |
115 | | - } |
116 | | - } |
117 | | - } |
118 | | - } |
119 | | - } |
120 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ZF\Doctrine\ORM\DataValidation\Controller; |
| 4 | + |
| 5 | +use Zend\Mvc\Controller\AbstractActionController; |
| 6 | +use Zend\View\Model\ViewModel; |
| 7 | +use Zend\Console\Request as ConsoleRequest; |
| 8 | +use Zend\Console\Adapter\AdapterInterface as Console; |
| 9 | +use Zend\Console\ColorInterface as Color; |
| 10 | +use Zend\Console\Prompt; |
| 11 | +use DoctrineDataFixtureModule\Loader\ServiceLocatorAwareLoader; |
| 12 | +use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
| 13 | +use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
| 14 | +use RuntimeException; |
| 15 | +use NoteVault\Entity; |
| 16 | + |
| 17 | +class ForeignKeyController extends AbstractActionController |
| 18 | +{ |
| 19 | + public function relationshipAction() |
| 20 | + { |
| 21 | + // Make sure that we are running in a console and the user has not tricked our |
| 22 | + // application into running this action from a public web server. |
| 23 | + $request = $this->getRequest(); |
| 24 | + if (!$request instanceof ConsoleRequest) { |
| 25 | + throw new RuntimeException('You can only use this action from a console.'); |
| 26 | + } |
| 27 | + |
| 28 | + $console = $this->getServiceLocator()->get('console'); |
| 29 | + |
| 30 | + $objectManagerAlias = $this->params()->fromRoute('object-manager'); |
| 31 | + $objectManager = $this->getServiceLocator()->get($objectManagerAlias); |
| 32 | + |
| 33 | + $console->write( |
| 34 | + 'Count' |
| 35 | + . "\t" |
| 36 | + . 'Distinct Values' |
| 37 | + . "\t" |
| 38 | + . 'Child Field' |
| 39 | + . "\t" |
| 40 | + . 'Child Entity' |
| 41 | + . "\t" |
| 42 | + . 'Parent Entity' |
| 43 | + . "\t" |
| 44 | + . 'SQL' |
| 45 | + . "\n" |
| 46 | + ); |
| 47 | + |
| 48 | + $allMetadata = $objectManager->getMetadataFactory()->getAllMetadata(); |
| 49 | + foreach ($allMetadata as $metadata) { |
| 50 | + |
| 51 | + foreach ($metadata->getAssociationMappings() as $mapping) { |
| 52 | + if ($mapping['type'] == 2) { |
| 53 | + $queryBuilder = $objectManager->createQueryBuilder(); |
| 54 | + $queryBuilder2 = $objectManager->createQueryBuilder(); |
| 55 | + |
| 56 | + $queryBuilder |
| 57 | + ->select( |
| 58 | + "count(child) as ct, count(distinct child." |
| 59 | + . $mapping['fieldName'] |
| 60 | + . ") as dst, '" |
| 61 | + . $mapping['fieldName'] |
| 62 | + . "' as childField, '" |
| 63 | + . $metadata->getName() |
| 64 | + . "' as childEntity, '" |
| 65 | + . $mapping['targetEntity'] |
| 66 | + . "' as parentEntity" |
| 67 | + ) |
| 68 | + ->from($metadata->getName(), 'child') |
| 69 | + ->andWhere($queryBuilder->expr()->not( |
| 70 | + $queryBuilder->expr()->exists( |
| 71 | + $queryBuilder2 |
| 72 | + ->select('parent') |
| 73 | + ->from($mapping['targetEntity'], 'parent') |
| 74 | + ->andWhere($queryBuilder2->expr()->eq( |
| 75 | + 'child.' . $mapping['fieldName'], |
| 76 | + 'parent' |
| 77 | + )) |
| 78 | + ->getQuery() |
| 79 | + ->getDql() |
| 80 | + ) |
| 81 | + )) |
| 82 | + ; |
| 83 | + |
| 84 | + // Do not query columns which are nullable and are null |
| 85 | + $childFieldMapping = $objectManager->getMetadataFactory() |
| 86 | + ->getMetadataFor($metadata->getName()) |
| 87 | + ->getAssociationMapping($mapping['fieldName']) |
| 88 | + ; |
| 89 | + |
| 90 | + if (! isset($childFieldMapping['joinColumns'][0]['nullable']) |
| 91 | + || ! $childFieldMapping['joinColumns'][0]['nullable']) { |
| 92 | + |
| 93 | + $queryBuilder->andWhere($queryBuilder->expr()->isNotNull('child.' . $mapping['fieldName'])); |
| 94 | + } |
| 95 | + |
| 96 | + $result = $queryBuilder->getQuery()->getOneOrNullResult(); |
| 97 | + |
| 98 | + if ($result['ct']) { |
| 99 | + $childMapping = $objectManager->getMetadataFactory() |
| 100 | + ->getMetadataFor($metadata->getName()); |
| 101 | + |
| 102 | + $console->write( |
| 103 | + $result['ct'] |
| 104 | + . "\t" |
| 105 | + . $result['dst'] |
| 106 | + . "\t" |
| 107 | + . $result['childField'] |
| 108 | + . "\t" |
| 109 | + . $result['childEntity'] |
| 110 | + . "\t" |
| 111 | + . $result['parentEntity'] |
| 112 | + . "\t" |
| 113 | + . $queryBuilder->getQuery()->getSql() |
| 114 | + . "\n" |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments