Skip to content

Commit b46c7d0

Browse files
author
Olexii Korshenko
committed
MAGETWO-32974: DB Foreign Keys Management on the Application Level
- implemented logic for EAV models
1 parent b13ccd7 commit b46c7d0

File tree

8 files changed

+200
-160
lines changed

8 files changed

+200
-160
lines changed

app/code/Magento/Catalog/Model/Resource/AbstractResource.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,20 @@ abstract class AbstractResource extends \Magento\Eav\Model\Entity\AbstractEntity
3838
protected $_modelFactory;
3939

4040
/**
41-
* Construct
42-
*
43-
* @param \Magento\Framework\App\Resource $resource
44-
* @param \Magento\Eav\Model\Config $eavConfig
45-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
46-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
47-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
48-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
41+
* @param \Magento\Eav\Model\Entity\Context $context
4942
* @param \Magento\Framework\Store\StoreManagerInterface $storeManager
5043
* @param \Magento\Catalog\Model\Factory $modelFactory
5144
* @param array $data
5245
*/
5346
public function __construct(
54-
\Magento\Framework\App\Resource $resource,
55-
\Magento\Eav\Model\Config $eavConfig,
56-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
57-
\Magento\Framework\Locale\FormatInterface $localeFormat,
58-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
59-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
47+
\Magento\Eav\Model\Entity\Context $context,
6048
\Magento\Framework\Store\StoreManagerInterface $storeManager,
6149
\Magento\Catalog\Model\Factory $modelFactory,
6250
$data = []
6351
) {
6452
$this->_storeManager = $storeManager;
6553
$this->_modelFactory = $modelFactory;
66-
parent::__construct(
67-
$resource,
68-
$eavConfig,
69-
$attrSetEntity,
70-
$localeFormat,
71-
$resourceHelper,
72-
$universalFactory,
73-
$data
74-
);
54+
parent::__construct($context, $data);
7555
}
7656

7757
/**

app/code/Magento/Catalog/Model/Resource/Category.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,16 @@ class Category extends AbstractResource
6666
protected $_categoryTreeFactory;
6767

6868
/**
69-
* Construct
70-
*
71-
* @param \Magento\Framework\App\Resource $resource
72-
* @param \Magento\Eav\Model\Config $eavConfig
73-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
74-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
75-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
76-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
69+
* @param \Magento\Eav\Model\Entity\Context $context
7770
* @param \Magento\Framework\Store\StoreManagerInterface $storeManager
7871
* @param \Magento\Catalog\Model\Factory $modelFactory
7972
* @param \Magento\Framework\Event\ManagerInterface $eventManager
80-
* @param \Magento\Catalog\Model\Resource\Category\TreeFactory $categoryTreeFactory
81-
* @param \Magento\Catalog\Model\Resource\Category\CollectionFactory $categoryCollectionFactory
73+
* @param Category\TreeFactory $categoryTreeFactory
74+
* @param Category\CollectionFactory $categoryCollectionFactory
8275
* @param array $data
83-
*
84-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8576
*/
8677
public function __construct(
87-
\Magento\Framework\App\Resource $resource,
88-
\Magento\Eav\Model\Config $eavConfig,
89-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
90-
\Magento\Framework\Locale\FormatInterface $localeFormat,
91-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
92-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
78+
\Magento\Eav\Model\Entity\Context $context,
9379
\Magento\Framework\Store\StoreManagerInterface $storeManager,
9480
\Magento\Catalog\Model\Factory $modelFactory,
9581
\Magento\Framework\Event\ManagerInterface $eventManager,
@@ -98,12 +84,7 @@ public function __construct(
9884
$data = []
9985
) {
10086
parent::__construct(
101-
$resource,
102-
$eavConfig,
103-
$attrSetEntity,
104-
$localeFormat,
105-
$resourceHelper,
106-
$universalFactory,
87+
$context,
10788
$storeManager,
10889
$modelFactory,
10990
$data

app/code/Magento/Catalog/Model/Resource/Product.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ class Product extends AbstractResource
5757
protected $typeFactory;
5858

5959
/**
60-
* @param \Magento\Framework\App\Resource $resource
61-
* @param \Magento\Eav\Model\Config $eavConfig
62-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
63-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
64-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
65-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
60+
* @param \Magento\Eav\Model\Entity\Context $context
6661
* @param \Magento\Framework\Store\StoreManagerInterface $storeManager
6762
* @param \Magento\Catalog\Model\Factory $modelFactory
6863
* @param Category\CollectionFactory $categoryCollectionFactory
@@ -75,12 +70,7 @@ class Product extends AbstractResource
7570
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7671
*/
7772
public function __construct(
78-
\Magento\Framework\App\Resource $resource,
79-
\Magento\Eav\Model\Config $eavConfig,
80-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
81-
\Magento\Framework\Locale\FormatInterface $localeFormat,
82-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
83-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
73+
\Magento\Eav\Model\Entity\Context $context,
8474
\Magento\Framework\Store\StoreManagerInterface $storeManager,
8575
\Magento\Catalog\Model\Factory $modelFactory,
8676
\Magento\Catalog\Model\Resource\Category\CollectionFactory $categoryCollectionFactory,
@@ -96,12 +86,7 @@ public function __construct(
9686
$this->setFactory = $setFactory;
9787
$this->typeFactory = $typeFactory;
9888
parent::__construct(
99-
$resource,
100-
$eavConfig,
101-
$attrSetEntity,
102-
$localeFormat,
103-
$resourceHelper,
104-
$universalFactory,
89+
$context,
10590
$storeManager,
10691
$modelFactory,
10792
$data

app/code/Magento/Customer/Model/Resource/Address.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,20 @@ class Address extends \Magento\Eav\Model\Entity\AbstractEntity
2222
protected $_customerFactory;
2323

2424
/**
25-
* @param \Magento\Framework\App\Resource $resource
26-
* @param \Magento\Eav\Model\Config $eavConfig
27-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
28-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
29-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
30-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
25+
* @param \Magento\Eav\Model\Entity\Context $context
3126
* @param \Magento\Framework\Validator\Factory $validatorFactory
3227
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
3328
* @param array $data
3429
*/
3530
public function __construct(
36-
\Magento\Framework\App\Resource $resource,
37-
\Magento\Eav\Model\Config $eavConfig,
38-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
39-
\Magento\Framework\Locale\FormatInterface $localeFormat,
40-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
41-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
31+
\Magento\Eav\Model\Entity\Context $context,
4232
\Magento\Framework\Validator\Factory $validatorFactory,
4333
\Magento\Customer\Model\CustomerFactory $customerFactory,
4434
$data = []
4535
) {
4636
$this->_validatorFactory = $validatorFactory;
4737
$this->_customerFactory = $customerFactory;
48-
parent::__construct(
49-
$resource,
50-
$eavConfig,
51-
$attrSetEntity,
52-
$localeFormat,
53-
$resourceHelper,
54-
$universalFactory,
55-
$data
56-
);
38+
parent::__construct($context, $data);
5739
}
5840

5941
/**

app/code/Magento/Customer/Model/Resource/Customer.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,20 @@ class Customer extends \Magento\Eav\Model\Entity\AbstractEntity
3131
protected $dateTime;
3232

3333
/**
34-
* @param \Magento\Framework\App\Resource $resource
35-
* @param \Magento\Eav\Model\Config $eavConfig
36-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
37-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
38-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
39-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
34+
* @param \Magento\Eav\Model\Entity\Context $context
4035
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4136
* @param \Magento\Framework\Validator\Factory $validatorFactory
4237
* @param \Magento\Framework\Stdlib\DateTime $dateTime
4338
* @param array $data
44-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4539
*/
4640
public function __construct(
47-
\Magento\Framework\App\Resource $resource,
48-
\Magento\Eav\Model\Config $eavConfig,
49-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
50-
\Magento\Framework\Locale\FormatInterface $localeFormat,
51-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
52-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
41+
\Magento\Eav\Model\Entity\Context $context,
5342
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
5443
\Magento\Framework\Validator\Factory $validatorFactory,
5544
\Magento\Framework\Stdlib\DateTime $dateTime,
5645
$data = []
5746
) {
58-
parent::__construct(
59-
$resource,
60-
$eavConfig,
61-
$attrSetEntity,
62-
$localeFormat,
63-
$resourceHelper,
64-
$universalFactory,
65-
$data
66-
);
47+
parent::__construct($context, $data);
6748
$this->_scopeConfig = $scopeConfig;
6849
$this->_validatorFactory = $validatorFactory;
6950
$this->dateTime = $dateTime;

app/code/Magento/Eav/Model/Entity.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,12 @@ class Entity extends \Magento\Eav\Model\Entity\AbstractEntity
2626
const DEFAULT_ENTITY_ID_FIELD = 'entity_id';
2727

2828
/**
29-
* @param \Magento\Framework\App\Resource $resource
30-
* @param \Magento\Eav\Model\Config $eavConfig
31-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
32-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
33-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
34-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
29+
* @param Entity\Context $context
3530
* @param array $data
3631
*/
37-
public function __construct(
38-
\Magento\Framework\App\Resource $resource,
39-
\Magento\Eav\Model\Config $eavConfig,
40-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
41-
\Magento\Framework\Locale\FormatInterface $localeFormat,
42-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
43-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
44-
$data = []
45-
) {
46-
parent::__construct(
47-
$resource,
48-
$eavConfig,
49-
$attrSetEntity,
50-
$localeFormat,
51-
$resourceHelper,
52-
$universalFactory,
53-
$data
54-
);
55-
$this->setConnection($resource->getConnection('eav_read'));
32+
public function __construct(\Magento\Eav\Model\Entity\Context $context, $data = [])
33+
{
34+
parent::__construct($context, $data);
35+
$this->setConnection($this->_resource->getConnection('eav_read'));
5636
}
5737
}

app/code/Magento/Eav/Model/Entity/AbstractEntity.php

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Framework\App\Config\Element;
1616
use Magento\Framework\Model\AbstractModel;
1717
use Magento\Framework\Model\Exception;
18+
use Magento\Framework\Model\Resource\Db\ObjectRelationProcessor;
19+
use Magento\Framework\Model\Resource\Db\TransactionManagerInterface;
1820

1921
/**
2022
* Entity/Attribute/Model - entity abstract
@@ -191,29 +193,29 @@ abstract class AbstractEntity extends \Magento\Framework\Model\Resource\Abstract
191193
protected $_universalFactory;
192194

193195
/**
194-
* @param \Magento\Framework\App\Resource $resource
195-
* @param \Magento\Eav\Model\Config $eavConfig
196-
* @param \Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity
197-
* @param \Magento\Framework\Locale\FormatInterface $localeFormat
198-
* @param \Magento\Eav\Model\Resource\Helper $resourceHelper
199-
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
196+
* @var TransactionManagerInterface
197+
*/
198+
protected $transactionManager;
199+
200+
/**
201+
* @var ObjectRelationProcessor
202+
*/
203+
protected $objectRelationProcessor;
204+
205+
/**
206+
* @param Context $context
200207
* @param array $data
201208
*/
202-
public function __construct(
203-
\Magento\Framework\App\Resource $resource,
204-
\Magento\Eav\Model\Config $eavConfig,
205-
\Magento\Eav\Model\Entity\Attribute\Set $attrSetEntity,
206-
\Magento\Framework\Locale\FormatInterface $localeFormat,
207-
\Magento\Eav\Model\Resource\Helper $resourceHelper,
208-
\Magento\Framework\Validator\UniversalFactory $universalFactory,
209-
$data = []
210-
) {
211-
$this->_eavConfig = $eavConfig;
212-
$this->_resource = $resource;
213-
$this->_attrSetEntity = $attrSetEntity;
214-
$this->_localeFormat = $localeFormat;
215-
$this->_resourceHelper = $resourceHelper;
216-
$this->_universalFactory = $universalFactory;
209+
public function __construct(Context $context, $data = [])
210+
{
211+
$this->_eavConfig = $context->getEavConfig();
212+
$this->_resource = $context->getResource();
213+
$this->_attrSetEntity = $context->getAttributeSetEntity();
214+
$this->_localeFormat = $context->getLocaleFormat();
215+
$this->_resourceHelper = $context->getResourceHelper();
216+
$this->_universalFactory = $context->getUniversalFactory();
217+
$this->transactionManager = $context->getTransactionManager();
218+
$this->objectRelationProcessor = $context->getObjectRelationProcessor();
217219
parent::__construct();
218220
$properties = get_object_vars($this);
219221
foreach ($data as $key => $value) {
@@ -1714,19 +1716,24 @@ protected function getAttributeRow($entity, $object, $attribute)
17141716
public function delete($object)
17151717
{
17161718
try {
1717-
$this->beginTransaction();
1719+
$connection = $this->transactionManager->start($this->_getWriteAdapter());
17181720
if (is_numeric($object)) {
17191721
$id = (int) $object;
17201722
} elseif ($object instanceof \Magento\Framework\Object) {
17211723
$object->beforeDelete();
17221724
$id = (int) $object->getId();
17231725
}
1724-
17251726
$this->_beforeDelete($object);
1726-
17271727
try {
17281728
$where = [$this->getEntityIdField() . '=?' => $id];
1729-
$this->_getWriteAdapter()->delete($this->getEntityTable(), $where);
1729+
$this->objectRelationProcessor->delete(
1730+
$this->transactionManager,
1731+
$connection,
1732+
$this->getEntityTable(),
1733+
$this->_getWriteAdapter()->quoteInto($this->getEntityIdField() . '=?', $id),
1734+
[$this->getEntityIdField() => $id]
1735+
);
1736+
17301737
$this->loadAllAttributes($object);
17311738
foreach ($this->getAttributesByTable() as $table => $attributes) {
17321739
$this->_getWriteAdapter()->delete($table, $where);
@@ -1741,12 +1748,12 @@ public function delete($object)
17411748
$object->isDeleted(true);
17421749
$object->afterDelete();
17431750
}
1744-
$this->commit();
1751+
$this->transactionManager->commit();
17451752
if ($object instanceof \Magento\Framework\Object) {
17461753
$object->afterDeleteCommit();
17471754
}
17481755
} catch (\Exception $e) {
1749-
$this->rollBack();
1756+
$this->transactionManager->rollBack();
17501757
throw $e;
17511758
}
17521759
return $this;

0 commit comments

Comments
 (0)