Here am added import custom upload drop down to magento backend succesfully.
while am importing data its not getting insert into database, its showing Please enter a correct entity model. error
class CustomImport extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
const ID = 'p_id';
const STORE = 'store_id';
const SDATE = 'pstart_date';
const EDATE = 'pend_date';
const TABLE_Entity = 'retailinsights_promostoremapp'; //table name mayBE
/**
* Validation failure message template definitions
*
* @var array
*/
protected $_messageTemplates = [
ValidatorInterface::ERROR_MESSAGE_IS_EMPTY => 'Message is empty',
];
protected $_permanentAttributes = [self::ID];
/**
* If we should check column names
*
* @var bool
*/
protected $needColumnCheck = true;
/**
* Valid column names
*
* @array
*/
protected $validColumnNames = [
self::ID,
self::STORE,
self::SDATE,
self::EDATE,
// self::MESSAGE,
// self::DATE,
];
/**
* 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
) {
$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;
}
public function getValidColumnNames()
{
return $this->validColumnNames;
}
/**
* Entity type code getter.
*
* @return string
*/
public function getEntityTypeCode()
{
return 'messages';
}
/**
* 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;
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
/**
* Create Advanced message data from raw data.
*
* @throws \Exception
* @return bool Result of operation.
*/
protected function _importData()
{
$this->saveEntity();
return true;
}
/**
* Save Message
*
* @return $this
*/
public function saveEntity()
{
$this->saveAndReplaceEntity();
return $this;
}
/**
* Save and replace data message
*
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
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_TITLE_IS_EMPTY, $rowNum);
continue;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}
$rowTtile= $rowData[self::ID];
$listTitle[] = $rowTtile;
$entityList[$rowTtile][] = [
self::ID => $rowData[self::ID],
self::STORE => $rowData[self::STORE],
self::SDATE => $rowData[self::SDATE],
self::EDATE => $rowData[self::EDATE],
// self::MESSAGE => $rowData[self::MESSAGE],
// self::DATE => $rowData[self::DATE],
];
}
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;
}
/**
* Save message to customtable.
*
* @param array $priceData
* @param string $table
* @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::ID,
self::STORE,
self::SDATE,
self::EDATE,
// self::MESSAGE,
// self::DATE,
]);
}
}
return $this;
}
this is my table retailinsights_promostoremapp
columns are p_id,store_id,rule_id,pstart_date,pend_date . thank you in advance
1 Answer 1
we have to define column headers in .csv
same as in database table, either we keep it empty data in .csv
but we must enter as header.