Passed
Push — dev_2x ( fb9ebe...5f9618 )
by Adrian
02:22
created

Insert::revert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Action;
5
6
use Sirius\Orm\Entity\StateEnum;
7
use Sirius\Orm\Helpers\Arr;
8
9
class Insert extends Update
10
{
11
    protected $entityId;
12
    protected $entityState;
13
14
    protected $extraColumns = [];
15
16
    protected function execute()
17
    {
18
        $this->entityId    = $this->entityHydrator->getPk($this->entity);
19
        $this->entityState = $this->entity->getState();
20
21
        $connection = $this->mapper->getWriteConnection();
22
23
        $columns = array_merge(
24
            $this->entityHydrator->extract($this->entity),
25
            $this->extraColumns,
26
            $this->mapper->getConfig()->getGuards()
27
        );
28
        $columns = Arr::except($columns, $this->mapper->getConfig()->getPrimaryKey());
29
30
        $insertSql = new \Sirius\Sql\Insert($connection);
31
        $insertSql->into($this->mapper->getConfig()->getTable())
32
                  ->columns($columns);
33
        $insertSql->perform();
34
35
        /**
36
         * We need to set the ID of the entity here because
37
         * other actions in the stack might need it
38
         * For example, on one-to-many relations when persisting the "parent",
39
         * the actions that persist the "children" have to know about the parent's ID
40
         */
41
        $this->entityHydrator->setPk($this->entity, (int) $connection->lastInsertId());
42
        $this->entity->setState(StateEnum::SYNCHRONIZED);
43
    }
44
}
45