To save the entity_id in my custom table on csv import.
Vendor\CatalogImportExport\Model\Import\Product.php
class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
const SKU = 'sku';
const COL_ENTITY_ID = 'entity_id';
const TABLE_Entity = 'temp_tgs';
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = [
ValidatorInterface::ERROR_PRODUCTID_IS_EMPTY => 'TITLE is empty',
];
protected $_permanentAttributes = [self::SKU];
/**
* If we should check column names
*
* @var bool
*/
protected $needColumnCheck = true;
protected $groupFactory;
/**
* Valid column names
*
* @array
*/
protected $validColumnNames = [
// self::COL_ENTITY_ID,
self::SKU,
//self::NAME,
];
/**
* Need to log in import history
*
* @var bool
*/
protected $logInHistory = true;
protected $_validators = [];
/**
* @var \Magento\Framework\Stdlib\DateTime\DateTime
*/
protected $_connection;
protected $_resource;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
public function __construct(
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\ImportExport\Helper\Data $importExportData,
\Magento\ImportExport\Model\ResourceModel\Import\Data $importData,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
\Magento\Framework\Stdlib\StringUtils $string,
ProcessingErrorAggregatorInterface $errorAggregator,
\Magento\Customer\Model\GroupFactory $groupFactory
) {
$this->jsonHelper = $jsonHelper;
$this->_importExportData = $importExportData;
$this->_resourceHelper = $resourceHelper;
$this->_dataSourceModel = $importData;
$this->_resource = $resource;
$this->_connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
$this->errorAggregator = $errorAggregator;
$this->groupFactory = $groupFactory;
}
public function getValidColumnNames()
{
return $this->validColumnNames;
}
/**
* Entity type code getter.
*
* @return string
*/
public function getEntityTypeCode()
{
return 'my_import';
}
/**
* Row validation.
*
* @param array $rowData
* @param int $rowNum
* @return bool
*/
public function validateRow(array $rowData, $rowNum)
{
$title = false;
if (isset($this->_validatedRows[$rowNum])) {
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
$this->_validatedRows[$rowNum] = true;
// BEHAVIOR_DELETE use specific validation logic
// if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) {
if (!isset($rowData[self::SKU]) || empty($rowData[self::SKU]) ) {
$this->addRowError(ValidatorInterface::ERROR_PRODUCTID_IS_EMPTY, $rowNum);
return false;
}
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
/**
* Create Advanced price data from raw data.
*
* @throws \Exception
* @return bool Result of operation.
*/
protected function _importData()
{
if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) {
$this->deleteEntity();
} elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE == $this->getBehavior()) {
$this->replaceEntity();
} elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $this->getBehavior()) {
$this->saveEntity();
}
return true;
}
/**
* Save newsletter subscriber
*
* @return $this
*/
public function saveEntity()
{
$this->saveAndReplaceEntity();
return $this;
}
/**
* Replace newsletter subscriber
*
* @return $this
*/
public function replaceEntity()
{
$this->saveAndReplaceEntity();
return $this;
}
protected function saveAndReplaceEntity()
{
$behavior = $this->getBehavior();
$listTitle = [];
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$entityList = [];
foreach ($bunch as $rowNum => $rowData) {
if (!$this->validateRow($rowData, $rowNum)) {
$this->addRowError(ValidatorInterface::ERROR_PRODUCTID_IS_EMPTY, $rowNum);
continue;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}
$rowTtile= $rowData[self::SKU];
$listTitle[] = $rowTtile;
$entityList[$rowTtile][] = [
self::SKU => $rowData[self::SKU],
//self::COL_ENTITY_ID => $rowData[self::COL_ENTITY_ID],
];
}
if (\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE == $behavior) {
if ($listTitle) {
if ($this->deleteEntityFinish(array_unique($listTitle), self::TABLE_Entity)) {
$this->saveEntityFinish($entityList, self::TABLE_Entity);
}
}
} elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $behavior) {
$this->saveEntityFinish($entityList, self::TABLE_Entity);
}
}
return $this;
}
protected function saveEntityFinish(array $entityData, $table)
{
if ($entityData) {
$tableName = $this->_connection->getTableName($table);
$entityIn = [];
foreach ($entityData as $id => $entityRows) {
foreach ($entityRows as $row) {
$entityIn[] = $row;
}
}
if ($entityIn) {
$this->_connection->insertOnDuplicate($tableName, $entityIn,[
self::SKU,
self::COL_ENTITY_ID, //here i need my imported productids to be saved to temp_tgs table
]);
}
}
return $this;
}
My table structure:
Im importing csv with columns only SKU as of now.(Following Magentos default import file, which has no product ids)
Problem is my sku is saving but i need the product ids of the imported products to be saved.
Im getting :
Additional data: Notice: Undefined index: entity_id in Vendor\CatalogImportExport\Model\Import\Product.php on line 216
ie here: (if i uncomment the line) self::COL_ENTITY_ID => $rowData[self::COL_ENTITY_ID],