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
|
|
|
|