2
2
3
3
namespace Mdiyakov \DoctrineSolrBundle \Command ;
4
4
5
+ use Doctrine \Bundle \DoctrineBundle \Registry ;
5
6
use Doctrine \ORM \EntityManager ;
6
7
use Doctrine \ORM \EntityRepository ;
7
8
use Symfony \Component \Console \Input \InputArgument ;
@@ -17,9 +18,9 @@ class IndexEntitiesCommand extends Command
17
18
const BUNCH_COUNT = 100 ;
18
19
19
20
/**
20
- * @var EntityManager
21
+ * @var Registry
21
22
*/
22
- private $ em ;
23
+ private $ registry ;
23
24
24
25
/**
25
26
* @var Config
@@ -44,14 +45,14 @@ class IndexEntitiesCommand extends Command
44
45
/**
45
46
* @param Config $config
46
47
* @param IndexProcessManager $indexProcessManager
47
- * @param EntityManager $em
48
+ * @param Registry $registry
48
49
*/
49
- public function __construct (Config $ config , IndexProcessManager $ indexProcessManager , EntityManager $ em )
50
+ public function __construct (Config $ config , IndexProcessManager $ indexProcessManager , Registry $ registry )
50
51
{
51
52
$ this ->config = $ config ;
52
53
$ this ->indexProcessManager = $ indexProcessManager ;
53
54
$ this ->possibleEntityTypes = array_keys ($ this ->getAssocEntitiesClasses ());
54
- $ this ->em = $ em ;
55
+ $ this ->registry = $ registry ;
55
56
56
57
parent ::__construct ();
57
58
}
@@ -105,10 +106,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
105
106
* @param string $entityClass
106
107
* @param int|null $id
107
108
* @throws \Exception
109
+ * @throws \LogicException
108
110
*/
109
111
private function indexEntityClass ($ entityClass , $ id = null )
110
112
{
111
- $ repository = $ this ->em ->getRepository ($ entityClass );
113
+ $ em = $ this ->registry ->getManagerForClass ($ entityClass );
114
+ if (!$ em instanceof EntityManager) {
115
+ throw new \LogicException ('EntityManager must be instance of EntityManager ' );
116
+ }
117
+
118
+ $ repository = $ em ->getRepository ($ entityClass );
112
119
if ($ id ) {
113
120
$ entity = $ repository ->find ($ id );
114
121
if (!$ entity ) {
@@ -118,21 +125,23 @@ private function indexEntityClass($entityClass, $id = null)
118
125
}
119
126
$ this ->processEntity ($ entity );
120
127
} else {
121
- $ this ->processRepository ($ repository );
128
+ $ this ->processRepository ($ repository, $ em );
122
129
}
123
130
}
124
131
125
132
/**
126
133
* @param EntityRepository $repository
134
+ * @param EntityManager $em
127
135
*/
128
- private function processRepository (EntityRepository $ repository )
136
+ private function processRepository (EntityRepository $ repository, EntityManager $ em )
129
137
{
130
138
$ offset = 0 ;
131
139
while ($ entities = $ repository ->findBy ([],['id ' => 'asc ' ], self ::BUNCH_COUNT , $ offset )) {
132
140
foreach ($ entities as $ entity ) {
133
141
$ this ->processEntity ($ entity );
134
142
}
135
- $ this ->em ->clear ($ repository ->getClassName ());
143
+
144
+ $ em ->clear ($ repository ->getClassName ());
136
145
$ offset += self ::BUNCH_COUNT ;
137
146
}
138
147
}
0 commit comments