1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* @author Alex Kuperwood <[email protected]> |
5
|
|
|
* @copyright 2025 Alex Kuperwood |
6
|
|
|
* @license https://opensource.org/license/mit The MIT License |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Kuperwood\Eav\Model; |
11
|
|
|
|
12
|
|
|
use Kuperwood\Eav\Enum\_ATTR; |
13
|
|
|
use PDO; |
14
|
|
|
|
15
|
|
|
class AttributeModel extends Model |
16
|
|
|
{ |
17
|
1 |
|
public function __construct() |
18
|
|
|
{ |
19
|
1 |
|
$this->setTable(_ATTR::table()); |
20
|
1 |
|
$this->setPrimaryKey(_ATTR::ID); |
21
|
|
|
} |
22
|
|
|
|
23
|
1 |
|
public function create(array $data) : int |
24
|
|
|
{ |
25
|
1 |
|
$conn = $this->db(); |
26
|
|
|
|
27
|
|
|
// Use sprintf to directly create column names and placeholders |
28
|
1 |
|
$sql = sprintf( |
29
|
1 |
|
"INSERT INTO %s (%s) VALUES (%s)", |
30
|
1 |
|
$this->getTable(), |
31
|
1 |
|
implode(', ', [ |
32
|
1 |
|
_ATTR::DOMAIN_ID, |
33
|
1 |
|
_ATTR::NAME, |
34
|
1 |
|
_ATTR::TYPE, |
35
|
1 |
|
_ATTR::STRATEGY, |
36
|
1 |
|
_ATTR::SOURCE, |
37
|
1 |
|
_ATTR::DEFAULT_VALUE, |
38
|
1 |
|
_ATTR::DESCRIPTION |
39
|
1 |
|
]), |
40
|
1 |
|
implode(', ', [ |
41
|
1 |
|
sprintf(':%s', _ATTR::DOMAIN_ID), |
42
|
1 |
|
sprintf(':%s', _ATTR::NAME), |
43
|
1 |
|
sprintf(':%s', _ATTR::TYPE), |
44
|
1 |
|
sprintf(':%s', _ATTR::STRATEGY), |
45
|
1 |
|
sprintf(':%s', _ATTR::SOURCE), |
46
|
1 |
|
sprintf(':%s', _ATTR::DEFAULT_VALUE), |
47
|
1 |
|
sprintf(':%s', _ATTR::DESCRIPTION) |
48
|
1 |
|
]) |
49
|
1 |
|
); |
50
|
|
|
|
51
|
|
|
// Prepare the PDO statement |
52
|
1 |
|
$stmt = $conn->prepare($sql); |
53
|
|
|
|
54
|
|
|
// Bind the parameters using the names directly |
55
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::DOMAIN_ID), $data[_ATTR::DOMAIN_ID], PDO::PARAM_INT); |
56
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::NAME), $data[_ATTR::NAME]); |
57
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::TYPE), $data[_ATTR::TYPE]); |
58
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::STRATEGY), $data[_ATTR::STRATEGY]); |
59
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::SOURCE), $data[_ATTR::SOURCE]); |
60
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::DEFAULT_VALUE), $data[_ATTR::DEFAULT_VALUE]); |
61
|
1 |
|
$stmt->bindParam(sprintf(':%s', _ATTR::DESCRIPTION), $data[_ATTR::DESCRIPTION]); |
62
|
|
|
|
63
|
|
|
// Execute the query |
64
|
1 |
|
$stmt->execute(); |
65
|
|
|
|
66
|
|
|
// Return the last inserted ID |
67
|
1 |
|
return (int) $conn->lastInsertId(); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
public function findByName(string $name, int $domainKey) |
71
|
|
|
{ |
72
|
1 |
|
$conn = $this->db(); |
73
|
|
|
|
74
|
|
|
// Prepare the SQL query with placeholders |
75
|
1 |
|
$sql = sprintf( |
76
|
1 |
|
"SELECT %s FROM %s WHERE %s = :domain_key AND %s = :name", |
77
|
1 |
|
$this->getPrimaryKey(), |
78
|
1 |
|
$this->getTable(), |
79
|
1 |
|
_ATTR::DOMAIN_ID, |
80
|
1 |
|
_ATTR::NAME |
81
|
1 |
|
); |
82
|
|
|
|
83
|
|
|
// Prepare the PDO statement |
84
|
1 |
|
$stmt = $conn->prepare($sql); |
85
|
|
|
|
86
|
|
|
// Bind the parameters |
87
|
1 |
|
$stmt->bindParam(':domain_key', $domainKey, PDO::PARAM_INT); |
88
|
1 |
|
$stmt->bindParam(':name', $name); |
89
|
|
|
|
90
|
|
|
// Execute the query |
91
|
1 |
|
$stmt->execute(); |
92
|
|
|
|
93
|
|
|
// Fetch the result |
94
|
1 |
|
return $stmt->fetch(PDO::FETCH_ASSOC); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|