Skip to content

Commit 094339a

Browse files
committed
Table::insertOrIgnore
1 parent 0a76c38 commit 094339a

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Why another PHP ORM? In writing minimal and fast websites, it was determined tha
88
## Features
99
- **Active Records** A fully type checked object interface and implement basic CRUD functionality.
1010
- **Active Tables** Full table operations (select, update, insert and delete) including support for where, having, limits, ordering, grouping, joins and unions.
11-
- **Data Cursors** Cursors implement **iterable** and **Countable** eliminating the need for full arrays read into memory.
11+
- **Data Cursors** Cursors implement **iterable** and **Countable** eliminating the need to read full arrays into memory.
1212
- **Validation** Fully customizable and translatable backend validation.
1313
- **Virtual Fields** Supports get and set semantics for any custom or calculated field such as Carbon dates.
1414
- **Migrations** Simple migrations offer atomic up and down migrations.

src/PHPFUI/ORM/Table.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -703,15 +703,29 @@ public function getWhereCondition() : \PHPFUI\ORM\Condition
703703
return $this->whereCondition;
704704
}
705705

706+
/**
707+
* Inserts current data into table or ignores duplicate key if found
708+
*
709+
* @param array<\PHPFUI\ORM\Record> $records
710+
*/
711+
public function insertOrIgnore(array $records) : bool
712+
{
713+
return $this->insert($records, 'ignore ');
714+
}
715+
706716
/**
707717
* Mass insertion. Does not use a transaction, so surround by a transaction if needed
708718
*
709719
* @param array<\PHPFUI\ORM\Record> $records
710720
*/
711-
public function insert(array $records) : bool
721+
public function insert(array $records, string $ignore = '') : bool
712722
{
723+
if (empty($records))
724+
{
725+
return false;
726+
}
713727
$tableName = $this->getTableName();
714-
$sql = "insert into `{$tableName}` (";
728+
$sql = "insert {$ignore}into `{$tableName}` (";
715729

716730
$fields = \array_keys($this->getFields());
717731
$comma = '';

0 commit comments

Comments
 (0)