Skip to content

Commit 5deaa05

Browse files
committed
Model ve Database insert yöntemi tek boyutlu dizileri kabul eder, çok boyutlu diziler için createBatch ya da insertBatch yöntemi eklendi.
1 parent ae92d77 commit 5deaa05

File tree

4 files changed

+107
-34
lines changed

4 files changed

+107
-34
lines changed

src/Database.php

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -415,29 +415,49 @@ public function create(array $set)
415415
if($isCreatedField){
416416
$data[$this->_credentials['createdField']] = $createdFieldParameterName;
417417
}
418-
}else{
419-
$i = 0;
420-
foreach ($set as $row) {
421-
$data[$i] = [];
422-
$this->_validation->setData($row);
423-
foreach ($row as $column => $value) {
424-
if($this->_validation->validation($column, null) === FALSE){
425-
$this->_errors[] = $this->_validation->getError();
426-
return false;
427-
}
428-
$data[$i][$column] = $value;
429-
}
430-
if(empty($data[$i])){
431-
continue;
432-
}
433-
if($isCreatedField){
434-
$data[$i][$this->_credentials['createdField']] = $createdFieldParameterName;
418+
}
419+
420+
$res = $this->query($this->_insertQuery($data));
421+
$this->reset();
422+
return $res->numRows() > 0;
423+
}
424+
425+
/**
426+
* @param array $set
427+
* @return bool
428+
*/
429+
public function createBatch(array $set)
430+
{
431+
if($this->_credentials['writable'] === FALSE){
432+
throw new WritableException('');
433+
}
434+
$isCreatedField = !empty($this->_credentials['createdField']);
435+
if($isCreatedField){
436+
$createdFieldParameterName = Parameters::add($this->_credentials['createdField'], \date($this->_credentials['timestampFormat']));
437+
}
438+
$data = [];
439+
$i = 0;
440+
foreach ($set as $row) {
441+
$data[$i] = [];
442+
$this->_validation->setData($row);
443+
foreach ($row as $column => $value) {
444+
if($this->_validation->validation($column, null) === FALSE){
445+
$this->_errors[] = $this->_validation->getError();
446+
return false;
435447
}
436-
++$i;
448+
$data[$i][$column] = $value;
449+
}
450+
if(empty($data[$i])){
451+
continue;
437452
}
453+
if($isCreatedField){
454+
$data[$i][$this->_credentials['createdField']] = $createdFieldParameterName;
455+
}
456+
++$i;
438457
}
439-
$res = $this->query($this->_insertQuery($data));
458+
$res = $this->query($this->_insertBatchQuery($data));
440459
$this->reset();
460+
441461
return $res->numRows() > 0;
442462
}
443463

@@ -636,21 +656,28 @@ public function _insertQuery(array $data): string
636656
$columns = [];
637657
$values = [];
638658

639-
if(\count($data) === \count($data, \COUNT_RECURSIVE)){
640-
foreach ($data as $column => $value) {
641-
$column = \trim($column);
642-
if($this->_credentials['allowedFields'] !== null && !\in_array($column, $this->_credentials['allowedFields'])){
643-
continue;
644-
}
645-
$columns[] = $column;
646-
$values[] = Helper::isSQLParameterOrFunction($value) ? $value : Parameters::add($column, $value);
647-
}
648-
if(empty($columns)){
649-
return '';
659+
foreach ($data as $column => $value) {
660+
$column = \trim($column);
661+
if($this->_credentials['allowedFields'] !== null && !\in_array($column, $this->_credentials['allowedFields'])){
662+
continue;
650663
}
651-
return $sql
652-
. ' (' . \implode(', ', $columns) . ') VALUES (' . \implode(', ', $values) . ');';
664+
$columns[] = $column;
665+
$values[] = Helper::isSQLParameterOrFunction($value) ? $value : Parameters::add($column, $value);
666+
}
667+
if(empty($columns)){
668+
return '';
653669
}
670+
return $sql
671+
. ' (' . \implode(', ', $columns) . ') VALUES (' . \implode(', ', $values) . ');';
672+
}
673+
674+
public function _insertBatchQuery($data): string
675+
{
676+
$sql = 'INSERT INTO'
677+
. ' '
678+
. (empty($this->_STRUCTURE['table']) ? $this->getSchema() : end($this->_STRUCTURE['table']));
679+
$columns = [];
680+
$values = [];
654681

655682
foreach ($data as &$row) {
656683
$value = [];
@@ -676,6 +703,7 @@ public function _insertQuery(array $data): string
676703
}
677704
$multiValues[] = '(' . \implode(', ', $value) . ')';
678705
}
706+
679707
return $sql . ' (' . \implode(', ', $columns) . ') VALUES '
680708
. \implode(', ', $multiValues) . ';';
681709
}

src/Facade/DB.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @method static int insertId()
4545
* @method static Result get(?string $table = null)
4646
* @method static bool create(array $set)
47+
* @method static bool createBatch(array $set)
4748
* @method static Result read(array $selector = [], array $conditions = [], array $parameters = [])
4849
* @method static Result readOne(array $selector = [], array $conditions = [], array $parameters = [])
4950
* @method static bool update(array $set)

src/Model.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ final public function create(array $set)
245245
return $this->insert($set);
246246
}
247247

248+
/**
249+
* @param array $set
250+
* @return array|false
251+
*/
252+
final public function createBatch(array $set)
253+
{
254+
return $this->insertBatch($set);
255+
}
256+
248257
/**
249258
* @param array $set
250259
* @return array|false
@@ -268,6 +277,41 @@ final public function insert(array $set)
268277
return $this->isCallbacksFunction('afterInsert') ? $this->callbacksFunctionHandler($data, 'afterInsert') : true;
269278
}
270279

280+
/**
281+
* @param array $set
282+
* @return array|false
283+
*/
284+
final public function insertBatch(array $set)
285+
{
286+
if($this->isUpdatable() === FALSE){
287+
throw new UpdatableException('"' . \get_called_class() . '" is not a updatable model.');
288+
}
289+
290+
if($this->isCallbacksFunction('beforeInsert', 'afterInsert')){
291+
foreach ($set as &$data) {
292+
$data = $this->callbacksFunctionHandler($data, 'beforeInsert');
293+
if($data === FALSE){
294+
return false;
295+
}
296+
}
297+
}
298+
299+
if(parent::createBatch($set) === FALSE){
300+
return false;
301+
}
302+
303+
if($this->isCallbacksFunction('afterInsert')){
304+
foreach ($set as &$row) {
305+
$row = $this->callbacksFunctionHandler($row, 'afterInsert');
306+
if($row === FALSE){
307+
return false;
308+
}
309+
}
310+
}
311+
312+
return $set;
313+
}
314+
271315
/**
272316
* @param Entity $entity
273317
* @return array|bool

tests/QueryBuilderUnitTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function testInsertStatementBuild()
210210
$this->db->reset();
211211
}
212212

213-
public function testMultiInsertStatementBuild()
213+
public function testInsertBatchStatementBuild()
214214
{
215215
Parameters::reset();
216216
$this->db->from('post');
@@ -230,7 +230,7 @@ public function testMultiInsertStatementBuild()
230230
];
231231

232232
$expected = 'INSERT INTO post (title, content, author, status) VALUES (:title, :content, :author, :status), (:title_1, :content_1, NULL, :status_1);';
233-
$this->assertEquals($expected, $this->db->_insertQuery($data));
233+
$this->assertEquals($expected, $this->db->_insertBatchQuery($data));
234234
$this->db->reset();
235235
}
236236

0 commit comments

Comments
 (0)