1: <?php
2:
3: namespace PHPixie\ORM\Steps\Step\Query\Insert\Batch\Data;
4:
5: class Unique extends \PHPixie\ORM\Steps\Step\Query\Insert\Batch\Data
6: {
7: protected $dataStep;
8: protected $selectQuery;
9:
10: public function __construct($dataStep, $selectQuery)
11: {
12: $this->dataStep = $dataStep;
13: $this->selectQuery = $selectQuery;
14: }
15:
16: public function fields()
17: {
18: return $this->dataStep->fields();
19: }
20:
21: public function execute()
22: {
23: $fields = $this->fields();
24: $data = $this->dataStep->data();
25: $this->selectQuery->fields($fields);
26:
27: foreach($fields as $key => $field) {
28: $values = array();
29: foreach($data as $dataRow){
30: $values[]=$dataRow[$key];
31: }
32: $this->selectQuery->addInOperatorCondition($field, $values);
33: }
34:
35: $existingItems = $this->selectQuery->execute()->getFields($fields);
36:
37: $existing = array();
38: foreach($existingItems as $existingItem) {
39: $existingValues = array();
40: foreach($fields as $key => $field)
41: $existingValues[$key]=$existingItem[$field];
42: $existing[]= $existingValues;
43: }
44:
45: $filteredProduct = array();
46: foreach($data as $dataRow) {
47: if(!in_array($dataRow, $existing, true)) {
48: $filteredProduct[] = $dataRow;
49: }
50: }
51:
52: $this->data = $filteredProduct;
53: }
54:
55: public function usedConnections()
56: {
57: return array($this->selectQuery->connection());
58: }
59: }