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 Kuperwood\Eav\Enum\_PIVOT; |
14
|
|
|
use Kuperwood\Eav\Enum\_SET; |
15
|
|
|
use PDO; |
16
|
|
|
|
17
|
|
|
class AttributeSetModel extends Model |
18
|
|
|
{ |
19
|
|
|
public function __construct() |
20
|
1 |
|
{ |
21
|
|
|
$this->setTable(_SET::table()); |
22
|
1 |
|
$this->setPrimaryKey(_SET::ID); |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
public function create(array $data) : int |
26
|
1 |
|
{ |
27
|
|
|
$conn = $this->db(); |
28
|
1 |
|
$sql = sprintf( |
29
|
1 |
|
"INSERT INTO %s (%s, %s) VALUES (:domain_id, :name)", |
30
|
1 |
|
$this->getTable(), |
31
|
1 |
|
_SET::DOMAIN_ID, |
32
|
1 |
|
_SET::NAME |
33
|
1 |
|
); |
34
|
1 |
|
$stmt = $conn->prepare($sql); |
35
|
1 |
|
$stmt->bindParam(':domain_id', $data[_SET::DOMAIN_ID]); |
36
|
1 |
|
$stmt->bindParam(':name', $data[_SET::NAME]); |
37
|
1 |
|
$stmt->execute(); |
38
|
1 |
|
return (int) $conn->lastInsertId(); |
39
|
1 |
|
} |
40
|
|
|
/** |
41
|
|
|
* @throws Exception |
42
|
|
|
*/ |
43
|
|
|
public function findAttributes($domainKey, $setKey = null) : array |
44
|
1 |
|
{ |
45
|
|
|
$sql = sprintf( |
46
|
1 |
|
"SELECT a.*, p.%s FROM %s a |
47
|
1 |
|
INNER JOIN %s p ON a.%s = p.%s |
48
|
|
|
WHERE p.%s = :domain_id", |
49
|
1 |
|
_PIVOT::GROUP_ID, |
50
|
1 |
|
_ATTR::table(), |
51
|
1 |
|
_PIVOT::table(), |
52
|
1 |
|
_ATTR::ID, |
53
|
1 |
|
_PIVOT::ATTR_ID, |
54
|
|
|
_PIVOT::DOMAIN_ID |
55
|
1 |
|
); |
56
|
1 |
|
|
57
|
|
|
if (!is_null($setKey)) { |
58
|
|
|
$sql .= sprintf(" AND p.%s = :set_id", _PIVOT::SET_ID); |
59
|
1 |
|
} |
60
|
1 |
|
|
61
|
|
|
$conn = $this->db(); |
62
|
1 |
|
$stmt = $conn->prepare($sql); |
63
|
|
|
|
64
|
1 |
|
$stmt->bindParam(':domain_id', $domainKey, PDO::PARAM_INT); |
65
|
1 |
|
|
66
|
|
|
if (!is_null($setKey)) { |
67
|
|
|
$stmt->bindParam(':set_id', $setKey, PDO::PARAM_INT); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
$stmt->execute(); |
71
|
|
|
|
72
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC); |
73
|
|
|
} |
74
|
|
|
} |