1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ddeboer\DataImport\Reader; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Doctrine\ORM\Internal\Hydration\IterableResult; |
7
|
|
|
use Doctrine\ORM\Query; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Reads entities through the Doctrine ORM |
11
|
|
|
* |
12
|
|
|
* @author David de Boer <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class DoctrineReader implements CountableReader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ObjectManager |
18
|
|
|
*/ |
19
|
|
|
protected $objectManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $objectName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var IterableResult |
28
|
|
|
*/ |
29
|
|
|
protected $iterableResult; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ObjectManager $objectManager |
33
|
|
|
* @param string $objectName e.g. YourBundle:YourEntity |
34
|
|
|
*/ |
35
|
5 |
|
public function __construct(ObjectManager $objectManager, $objectName) |
36
|
|
|
{ |
37
|
5 |
|
$this->objectManager = $objectManager; |
38
|
5 |
|
$this->objectName = $objectName; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
1 |
|
public function getFields() |
45
|
|
|
{ |
46
|
1 |
|
return $this->objectManager->getClassMetadata($this->objectName) |
47
|
1 |
|
->getFieldNames(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
1 |
|
public function current() |
54
|
|
|
{ |
55
|
1 |
|
return current($this->iterableResult->current()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
1 |
|
public function next() |
62
|
|
|
{ |
63
|
1 |
|
$this->iterableResult->next(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function key() |
70
|
|
|
{ |
71
|
|
|
return $this->iterableResult->key(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function valid() |
78
|
|
|
{ |
79
|
1 |
|
return $this->iterableResult->valid(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function rewind() |
86
|
|
|
{ |
87
|
1 |
|
if (!$this->iterableResult) { |
88
|
1 |
|
$query = $this->objectManager->createQuery( |
89
|
1 |
|
sprintf('SELECT o FROM %s o', $this->objectName) |
90
|
1 |
|
); |
91
|
1 |
|
$this->iterableResult = $query->iterate([], Query::HYDRATE_ARRAY); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
1 |
|
$this->iterableResult->rewind(); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
1 |
|
public function count() |
101
|
|
|
{ |
102
|
1 |
|
$query = $this->objectManager->createQuery( |
103
|
1 |
|
sprintf('SELECT COUNT(o) FROM %s o', $this->objectName) |
104
|
1 |
|
); |
105
|
|
|
|
106
|
1 |
|
return $query->getSingleScalarResult(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|