Passed
Pull Request — master (#78)
by Jean-Christophe
12:38
created

BulkInserts::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Ubiquity\orm\bulk;
4
5
use Ubiquity\orm\OrmUtils;
6
7
/**
8
 * Ubiquity\orm\bulk$BulkInserts
9
 * This class is part of Ubiquity
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 *
14
 */
15
class BulkInserts extends AbstractBulks {
16
17
	public function __construct($className) {
18
		parent::__construct ( $className );
19
		if (($key = \array_search ( $this->pkName, $this->fields )) !== false) {
20
			unset ( $this->fields [$key] );
21
		}
22
		$this->insertFields = \implode ( ',', $this->getQuotedKeys ( $this->fields, $this->db->quote ) );
23
	}
24
25
	public function addInstance($instance, $id = null) {
26
		$this->updateInstanceRest ( $instance );
27
		unset ( $instance->_rest [$this->pkName] );
28
		$this->instances [] = $instance;
29
	}
30
31
	public function createSQL() {
32
		$quote = $this->db->quote;
33
		$tableName = OrmUtils::getTableName ( $this->class );
34
		$fieldCount = \count ( $this->fields );
35
		$parameters = [ ];
36
		$values = [ ];
37
		$modelFields = '(' . \implode ( ',', \array_fill ( 0, $fieldCount, '?' ) ) . ')';
38
		foreach ( $this->instances as $instance ) {
39
			$parameters = \array_merge ( $parameters, \array_values ( $instance->_rest ) );
40
			$values [] = $modelFields;
41
		}
42
		$this->parameters = $parameters;
43
		return "INSERT INTO {$quote}{$tableName}{$quote} (" . $this->insertFields . ') VALUES ' . \implode ( ',', $values );
44
	}
45
}
46
47