Skip to content

Commit eb20be7

Browse files
committed
insertOrgnore support
1 parent 0a7dabd commit eb20be7

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

src/PHPFUI/ORM/Migration.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,17 @@ protected function dropViews(array $views) : void
294294
/**
295295
* Add an index on the fields in the array.
296296
*/
297-
protected function addIndex(string $table, array $fields, string $indexType = '') : bool
297+
protected function addIndex(string $table, string | array $fields, string $indexType = '') : bool
298298
{
299-
$indexName = \implode('', $fields) . $table . 'Index';
299+
if (\is_string($fields))
300+
{
301+
$indexName = $fields;
302+
$fields = [$fields];
303+
}
304+
else
305+
{
306+
$indexName = \implode('', $fields) . $table . 'Index';
307+
}
300308

301309
if ($this->indexExists($table, $indexName))
302310
{

src/PHPFUI/ORM/PDOInstance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function executeStatement(\PDOStatement $statement, array $input = []) :
303303
{
304304
$returnValue = $statement->execute($input);
305305
}
306-
catch (\Throwable)
306+
catch (\PDOException)
307307
{
308308
$returnValue = false;
309309
}

src/PHPFUI/ORM/Record.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ public function insertOrUpdate() : int | bool
407407
return $this->privateInsert(true);
408408
}
409409

410+
/**
411+
* Inserts current data into table or ignores duplicate key if found
412+
*
413+
* @return int | bool inserted id if auto increment, true on insertion if not auto increment or false on error
414+
*/
415+
public function insertOrIgnore() : int | bool
416+
{
417+
return $this->privateInsert(false, 'ignore ');
418+
}
419+
410420
/**
411421
* Load first from SQL query
412422
*/
@@ -782,12 +792,12 @@ private function getChildTable(string $relationship) : ?\PHPFUI\ORM\Table
782792
*
783793
* @return int | bool inserted id if auto increment, true on insertion if not auto increment or false on error
784794
*/
785-
private function privateInsert(bool $updateOnDuplicate) : int | bool
795+
private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : int | bool
786796
{
787797
$this->clean();
788798
$table = static::$table;
789799

790-
$sql = "insert into `{$table}` (";
800+
$sql = "insert {$ignore}into `{$table}` (";
791801
$values = [];
792802
$whereInput = $input = [];
793803
$comma = '';
@@ -817,8 +827,9 @@ private function privateInsert(bool $updateOnDuplicate) : int | bool
817827

818828
if ($updateOnDuplicate)
819829
{
820-
$sql .= ' on duplicate key update ';
830+
$updateSql = ' on duplicate key update ';
821831
$comma = '';
832+
$inputCount = count($input);
822833

823834
foreach ($this->current as $key => $value)
824835
{
@@ -833,12 +844,20 @@ private function privateInsert(bool $updateOnDuplicate) : int | bool
833844

834845
if (! isset(static::$primaryKeys[$key]))
835846
{
836-
$sql .= $comma . '`' . $key . '` = ?';
847+
$updateSql .= $comma . '`' . $key . '` = ?';
837848
$input[] = $value;
838849
$comma = ',';
839850
}
840851
}
841852
}
853+
if (count($input) == $inputCount) // nothing to update but primary keys, ignore input
854+
{
855+
$sql = str_replace('insert into', 'insert ignore into', $sql);
856+
}
857+
else
858+
{
859+
$sql .= $updateSql;
860+
}
842861
}
843862

844863
$returnValue = \PHPFUI\ORM::execute($sql, $input);

src/PHPFUI/ORM/Record/Definition/Migration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ abstract class Migration extends \PHPFUI\ORM\Record
1515

1616
/** @var array<string, array<mixed>> */
1717
protected static array $fields = [
18-
// MYSQL_TYPE, PHP_TYPE, LENGTH, NULL, DEFAULT, KEY
19-
'migrationId' => ['int(11)', 'int', 11, false, null, true, ],
20-
'ran' => ['timestamp', 'string', 20, true, '', false, ],
18+
// MYSQL_TYPE, PHP_TYPE, LENGTH, KEY, ALLOWS_NULL, DEFAULT
19+
'migrationId' => ['int(11)', 'int', 11, true, false, ],
20+
'ran' => ['timestamp', 'string', 20, false, false, ],
2121
];
2222

2323
/** @var array<string, true> */

0 commit comments

Comments
 (0)