Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Magento 2.1 staging support #156

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6242545
Staging Support - Added configurations for configuring staging mode.
Jan 18, 2017
56423bc
Staging Support - Added simple staging support to products (without c…
Jan 19, 2017
6d6d3de
Staging Support - Added simple staging support for categories.
Jan 19, 2017
e6d49af
Staging Support - Fixed issue with import not working without staging…
Jan 20, 2017
20e1665
Performance - Improved performance for configurable imports by doing …
Jan 23, 2017
edc92bb
Staging Support - Fixed issue with import braking created/updated dat…
Jan 23, 2017
5f7aa15
Staging Support - Fixed issue with created/updated dates & added upda…
Jan 23, 2017
3702afb
Staging Support - Added support to update relations in all stages.
Jan 24, 2017
dbba672
Staging Support - Added support to update configurations & medias in …
Jan 24, 2017
8a6aa75
Staging Support - Created a Staging module & centralized staging conf…
Jan 24, 2017
7ca995b
Staging Support - Moved staging logic in Pimgento_Staging module.
Jan 24, 2017
5313cd4
Staging Support - Fixed issue with last version not always being the …
Jan 24, 2017
0e1826d
Staging Support - Finalized configurable super_attribute labels not b…
Jan 25, 2017
aab0225
Staging Support - Moved product related staging logic into a specific…
Jan 25, 2017
db716a1
Staging Support - Added support for full staging mode on products imp…
Jan 26, 2017
cde8bf4
Staging Support - Added missing checks & changed the staging title.
Jan 26, 2017
0fec6a4
Merge branch 'staging_support' into performance_configurable
Jan 31, 2017
2b9d683
Removed commented code for configurable imports & fixed a few CS issues.
Feb 24, 2017
fe6b502
Merge remote-tracking branch 'dnd/master' into performance_configurable
Apr 3, 2017
e7c1bf5
Fixed issue with value set.
Apr 3, 2017
58cbfce
PR - resolving conflicts
Sep 27, 2017
586f6e1
Merge branch 'performance_configurable_merged'
Sep 27, 2017
671b091
Fixing category import to use '_row_id' in tmp_table instead of 'row_id'
Sep 28, 2017
3a50e30
Medi - Fixing missing class import
Sep 28, 2017
b8e8536
Product - changing confusing comment
Sep 28, 2017
e0586cb
Product - fixing wrong table name in full import
Sep 28, 2017
ce4d093
Merge branch 'master' of https://github.com/Agence-DnD/PIMGento-2
Nov 20, 2017
ddb9a7b
Fix - missing create and update column for full import to work
Nov 22, 2017
d74bca2
Doc - updating import doc
Nov 22, 2017
355be71
Fix - integrating previous optimisation
Nov 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions Category/Model/Factory/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use \Pimgento\Import\Model\Factory;
use \Pimgento\Entities\Model\Entities;
use \Pimgento\Import\Helper\Config as helperConfig;
use \Pimgento\Staging\Helper\Config as StagingConfigHelper;
use \Pimgento\Staging\Helper\Import as StagingHelper;
use \Pimgento\Import\Helper\UrlRewrite as urlRewriteHelper;
use \Magento\Framework\Event\ManagerInterface;
use \Magento\Catalog\Model\Category;
Expand Down Expand Up @@ -38,6 +40,16 @@ class Import extends Factory
*/
protected $_urlRewriteHelper;

/**
* @var StagingHelper
*/
protected $stagingHelper;

/**
* @var StagingConfigHelper
*/
protected $stagingConfigHelper;

/**
* @param \Pimgento\Entities\Model\Entities $entities
* @param \Pimgento\Import\Helper\Config $helperConfig
Expand All @@ -47,6 +59,8 @@ class Import extends Factory
* @param \Magento\Catalog\Model\Category $category
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param urlRewriteHelper $urlRewriteHelper
* @param StagingConfigHelper $stagingConfigHelper
* @param StagingHelper $stagingHelper
* @param array $data
*/
public function __construct(
Expand All @@ -58,6 +72,8 @@ public function __construct(
Category $category,
TypeListInterface $cacheTypeList,
urlRewriteHelper $urlRewriteHelper,
StagingConfigHelper $stagingConfigHelper,
StagingHelper $stagingHelper,
array $data = []
)
{
Expand All @@ -66,6 +82,8 @@ public function __construct(
$this->_category = $category;
$this->_cacheTypeList = $cacheTypeList;
$this->_urlRewriteHelper = $urlRewriteHelper;
$this->stagingConfigHelper = $stagingConfigHelper;
$this->stagingHelper = $stagingHelper;
}

/**
Expand All @@ -84,6 +102,17 @@ public function createTable()
}
}

/**
* Add required columns
*/
public function addRequiredData()
{
$connection = $this->_entities->getResource()->getConnection();
$tmpTable = $this->_entities->getTableName($this->getCode());

$this->stagingHelper->addRequiredData($connection, $tmpTable);
}

/**
* Insert data into temporary table
*/
Expand All @@ -103,7 +132,26 @@ public function insertData()
*/
public function matchEntity()
{
$this->_entities->matchEntity($this->getCode(), 'code', 'catalog_category_entity', 'entity_id');
if ($this->stagingConfigHelper->isCatalogStagingModulesEnabled()) {
/**
* When using staging module entity id's are not the primary key of the catalog_product_entity
* table anymore. The new primary keys is row_id. Before we get information on the row_id, we still
* need to get the entiy_id of the products to be imported. We are therefore going to use a different
* table built for this purpose in magento.
*/
$this->_entities->matchEntity($this->getCode(), 'code', 'sequence_catalog_category', 'sequence_value');

// Once the entitie id's are matched we can match the row ids.
$this->stagingHelper->matchEntityRows(
$this->_entities,
'catalog_category_entity',
$this->getCode(),
StagingConfigHelper::STAGING_MODE_LAST
);

} else {
$this->_entities->matchEntity($this->getCode(), 'code', 'catalog_category_entity', 'entity_id');
}
}

/**
Expand Down Expand Up @@ -281,29 +329,37 @@ public function createEntities()
'children_count' => new Expr('0'),
);

$columnIdentifier = $this->_entities->getColumnIdentifier($table);

if ($columnIdentifier == 'row_id') {
$values['row_id'] = '_entity_id';
if ($this->stagingConfigHelper->isCatalogStagingModulesEnabled()) {
$values['created_in'] = new Expr(1);
$values['updated_in'] = new Expr(VersionManager::MAX_VERSION);
$values['row_id'] = '_row_id';
}

$this->stagingHelper->createEntitiesBefore($connection, 'sequence_catalog_category', $tmpTable);

$parents = $connection->select()->from($tmpTable, $values);
$connection->query(
$connection->insertFromSelect(
$parents, $table, array_keys($values), 1
$parents,
$table,
array_keys($values),
1
)
);

$this->stagingHelper->createEntitiesAfter(
$connection,
'catalog_category_entity',
$tmpTable,
StagingConfigHelper::STAGING_MODE_LAST
);

$values = array(
'created_at' => new Expr('now()')
);
$connection->update($table, $values, 'created_at IS NULL');

if ($columnIdentifier == 'row_id') {
$values = [
'created_in' => new Expr(1),
'updated_in' => new Expr(VersionManager::MAX_VERSION),
];
if ($this->stagingConfigHelper->isCatalogStagingModulesEnabled()) {
$connection->update($table, $values, 'created_in = 0 AND updated_in = 0');
}
}
Expand Down Expand Up @@ -342,7 +398,8 @@ public function setValues()
$resource->getTable('catalog_category_entity'),
$values,
3,
$store['store_id']
$store['store_id'],
1
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions Category/Observer/AddPimgentoImportObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ protected function getStepsDefinition()
'comment' => __('Create temporary table'),
'method' => 'createTable',
),
array(
'comment' => __('Add product required data'),
'method' => 'addRequiredData',
),
array(
'comment' => __('Fill temporary table'),
'method' => 'insertData',
Expand Down
1 change: 1 addition & 0 deletions Category/etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<sequence>
<module name="Pimgento_Entities"/>
<module name="Pimgento_Import"/>
<module name="Pimgento_Staging"/>
</sequence>
</module>
</config>
9 changes: 8 additions & 1 deletion Entities/Model/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ public function matchEntity($tableSuffix, $pimKey, $entityTable, $entityKey, $pr
public function setValues($tableSuffix, $entityTable, $values, $entityTypeId, $storeId, $mode = 1)
{
$this->_getResource()
->setValues($this->getTableName($tableSuffix), $entityTable, $values, $entityTypeId, $storeId, $mode);
->setValues(
$this->getTableName($tableSuffix),
$entityTable,
$values,
$entityTypeId,
$storeId,
$mode
);

return $this;
}
Expand Down
57 changes: 53 additions & 4 deletions Entities/Model/ResourceModel/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function matchEntity($tableName, $pimKey, $entityTable, $entityKey, $impo
public function setValues($tableName, $entityTable, $values, $entityTypeId, $storeId, $mode = 1)
{
$connection = $this->getConnection();

foreach ($values as $code => $value) {
if (($attribute = $this->getAttribute($code, $entityTypeId))) {
if ($attribute['backend_type'] !== 'static') {
Expand All @@ -396,13 +396,13 @@ public function setValues($tableName, $entityTable, $values, $entityTypeId, $sto
array(
'attribute_id' => new Expr($attribute['attribute_id']),
'store_id' => new Expr($storeId),
$identifier => '_entity_id',
$identifier => '_' . $identifier,
'value' => $value,
)
);

if ($columnExists) {
$select->where('`' . $columnName . '` <> ?', self::IGNORE_VALUE);
$select->where('TRIM(`' . $columnName . '`) <> ?', self::IGNORE_VALUE);
}

$insert = $connection->insertFromSelect(
Expand All @@ -421,7 +421,9 @@ public function setValues($tableName, $entityTable, $values, $entityTypeId, $sto
'value = ?' => '0000-00-00 00:00:00'
);
$connection->update(
$this->getTable($entityTable . '_' . $backendType), $values, $where
$this->getTable($entityTable . '_' . $backendType),
$values,
$where
);
}
}
Expand All @@ -431,6 +433,53 @@ public function setValues($tableName, $entityTable, $values, $entityTypeId, $sto
return $this;
}

/**
* Update the values for an entity in all it's stages.
*
* @param $tableName
* @param $entityTable
* @param $entityTypeId
* @param $attributeCode
*/
public function updateAllStageValues($tableName, $entityTable, $entityTypeId, $attributeCode, $joinCondition = 't._row_id != e.row_id')
{
$connection = $this->getConnection();

if (($attribute = $this->getAttribute($attributeCode, $entityTypeId))) {
if ($attribute['backend_type'] !== 'static') {
$backendType = $attribute['backend_type'];
$attributeTable = $connection->getTableName($entityTable . '_' . $backendType);

$select = $connection->select()
->from(
['e' =>$entityTable],
[]
)->joinInner(
['t' => $tableName],
"t._entity_id = e.entity_id AND $joinCondition",
[]
)->joinInner(
['u' => $attributeTable],
'u.row_id = t._row_id AND attribute_id = ' . $attribute['attribute_id'],
[]
);

$select->columns(['u.attribute_id', 'u.store_id', 'e.row_id', 'u.value']);

$select->setPart('disable_staging_preview', true);

$insert = $connection->insertFromSelect(
$select,
$attributeTable,
array('attribute_id', 'store_id', 'row_id', 'value'),
1
);

$connection->query($insert);
}
}
}

/**
* Copy column to an other
*
Expand Down
39 changes: 35 additions & 4 deletions Product/Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@
use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\Filesystem;
use \Pimgento\Staging\Helper\Config as StagingConfigHelper;

class Config extends AbstractHelper
{
/**
* Constants to configuration profile.
*/
const CONFIG_PROFILE = 'product';

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;

/**
* @var \Pimgento\Import\Helper\Config
*/
protected $stagingConfigHelper;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager
)
{
StoreManagerInterface $storeManager,
StagingConfigHelper $statingConfigHelper
) {
$this->_storeManager = $storeManager;
$this->stagingConfigHelper = $statingConfigHelper;

parent::__construct($context);
}

Expand Down Expand Up @@ -72,4 +83,24 @@ public function getDefaultWebsiteId()
return $this->_storeManager->getStore()->getWebsiteId();
}

/**
* Get import staging mode to use.
*
* @return mixed|string
*/
public function getImportStagingMode()
{

return $this->stagingConfigHelper->getImportStagingMode(self::CONFIG_PROFILE);
}

/**
* Check if current configuration asks import to be in full staging mode or not.
*
* @return bool
*/
public function isImportInFullStagingMode()
{
return $this->getImportStagingMode() == StagingConfigHelper::STAGING_MODE_FULL;
}
}
Loading