Issues (9)

src/Create.php (1 issue)

1
<?php
2
3
namespace Willry\QueryBuilder;
4
5
6
class Create extends Base
7
{
8
    protected function mountQuery(): void
9
    {
10
        $columns = implode(", ", array_keys($this->fields));
11
        $values = implode(',', array_fill(0, count($this->fields), '?'));
12
13
        $this->query = "INSERT INTO {$this->entity} ({$columns}) VALUES ({$values})";
14
    }
15
16
    /**
17
     * @param array $data
18
     * @return Create
19
     */
20
    public function create(array $data): static
21
    {
22
        $this->fields = $data;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @param array $data
29
     */
30
    public function exec(): ?int
31
    {
32
        try {
33
            $this->mountQuery();
34
35
            $stmt = $this->db->prepare($this->query);
36
37
            QueryHelpers::bind($stmt, $this->flatBindings());
38
39
            $stmt->execute();
40
41
            return $this->db->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->lastInsertId() returns the type string which is incompatible with the type-hinted return integer|null.
Loading history...
42
        } catch (\PDOException $exception) {
43
            $this->handleError($exception);
44
        }
45
    }
46
}
47