|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the eav package. |
|
4
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
|
5
|
|
|
* @copyright 2023 Aleksandr Drobotik |
|
6
|
|
|
* @license https://opensource.org/license/mit The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Drobotik\Eav\Model; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Exception; |
|
13
|
|
|
use Drobotik\Eav\Enum\_ATTR; |
|
14
|
|
|
use Drobotik\Eav\Enum\_PIVOT; |
|
15
|
|
|
use Drobotik\Eav\Enum\_SET; |
|
16
|
|
|
|
|
17
|
|
|
class AttributeSetModel extends Model |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->setTable(_SET::table()); |
|
22
|
|
|
$this->setPrimaryKey(_SET::ID->column()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function create(array $data) : int |
|
26
|
|
|
{ |
|
27
|
1 |
|
$conn = $this->db(); |
|
28
|
1 |
|
$conn->createQueryBuilder() |
|
29
|
1 |
|
->insert($this->getTable()) |
|
30
|
1 |
|
->values([ |
|
31
|
1 |
|
_SET::DOMAIN_ID->column() => '?', |
|
32
|
1 |
|
_SET::NAME->column() => '?' |
|
33
|
1 |
|
]) |
|
34
|
1 |
|
->setParameter(0, $data[_SET::DOMAIN_ID->column()]) |
|
35
|
1 |
|
->setParameter(1, $data[_SET::NAME->column()]) |
|
36
|
1 |
|
->executeQuery(); |
|
37
|
1 |
|
return (int) $conn->lastInsertId(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function findAttributes(int $domainKey, int $setKey = null) : array |
|
44
|
|
|
{ |
|
45
|
1 |
|
$query = $this->db() |
|
46
|
1 |
|
->createQueryBuilder() |
|
47
|
1 |
|
->select('a.*') |
|
48
|
1 |
|
->from(_ATTR::table(), 'a') |
|
49
|
1 |
|
->innerJoin('a', _PIVOT::table(), 'p', |
|
50
|
1 |
|
sprintf('a.%s = p.%s', _ATTR::ID->column(), _PIVOT::ATTR_ID->column()) |
|
51
|
1 |
|
) |
|
52
|
1 |
|
->where(sprintf('p.%s = ?', _PIVOT::DOMAIN_ID->column())) |
|
53
|
1 |
|
->setParameter(0, $domainKey); |
|
54
|
|
|
|
|
55
|
1 |
|
if(!is_null($setKey)) |
|
56
|
1 |
|
$query = $query |
|
57
|
1 |
|
->andWhere(sprintf('p.%s = ?', _PIVOT::SET_ID->column())) |
|
58
|
1 |
|
->setParameter(1, $setKey); |
|
59
|
|
|
|
|
60
|
1 |
|
return $query |
|
61
|
1 |
|
->executeQuery() |
|
62
|
1 |
|
->fetchAllAssociative(); |
|
63
|
|
|
} |
|
64
|
|
|
} |