0

I am trying to insert data into the EAV tables, but my main entity table isn't saving any data but the value tables data is inserted. My Entity Type code is vinay_employee, my table vinay_employee_entity has three columns entity_id employee_name and employee_email. Data is not inserting into vinay_employee_entity when i am calling save() function but other values tables like vinay_employee_entity_datetime get their data.

Model/EmployeeData.php

namespace Vinay\Employee\Model;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Pricing\SaleableInterface;
use Vinay\Employee\Api\Data\EmployeeInterface;
use Magento\Framework\Api\AttributeValueFactory;

class EmployeeData extends \Magento\Framework\Model\AbstractModel{

    const ENTITY = "vinay_employee";

    protected function _construct() {
        $this->_init('Vinay\Employee\Model\ResourceModel\EmployeeData');
    }
}

ResourceModel/EmployeeData.php

namespace Vinay\Employee\Model\ResourceModel;

class EmployeeData extends \Magento\Eav\Model\Entity\AbstractEntity{
    
    protected function _construct()
    {
        $this->_read = 'vinay_employee_read';
        $this->_write = 'vinay_employee_write';
    }
    
    public function getEntityType() {
        if(empty($this->_type)) {
            $this->setType(\Vinay\Employee\Model\EmployeeData::ENTITY);
        }

        return parent::getEntityType();
    } 
}

Controller/Adminhtml/Emp/Save.php

namespace Vinay\Employee\Controller\Adminhtml\Emp;

class Save extends \Vinay\Employee\Controller\Adminhtml\Emp\AbstractAction {

    //put your code here

    private $connection;
    private $employeeDataFactory;
    private $employee, $employeeDataFactoryResource, $registry;

    public function __construct(
            \Magento\Backend\App\Action\Context $context,
            \Magento\Framework\Registry $registry,
            \Magento\Framework\App\ResourceConnection $resourceConnection,
            \Vinay\Employee\Model\ResourceModel\EmployeeDataFactory $employeeDataFactoryResource,
            \Vinay\Employee\Model\EmployeeDataFactory $employeeDataFactory) {
        $this->connection = $resourceConnection->getConnection();
        parent::__construct($context);
        $this->employee = $employeeDataFactory;
        $this->registry= $registry;
        $this->employeeDataFactoryResource = $employeeDataFactoryResource;
    }

    public function execute() {
        $postObj = $this->getRequest()->getPostValue();
        echo "<pre>";
        $employeeModel = $this->employee->create(["employee_name", "employee_email"]);
        $result = $employeeModel->setData($postObj)->save();
        print_r($result->getData());
        die();
        $this->employeeDataFactoryResource->create()->save($employeeModel);
        return $this->_redirect("employee/emp/index");
    }
}

$result

Array
(
    [form_key] => HSR4TCVPyzs6r6Wi
    [employee_name] => vinn
    [employee_email] => viin
    [employee_address] => ddsdds
    [is_permanent] => 0
    [joined_at] => 2021-04-15 10:02:35
    [vinaykharayat11] => ssdsd
    [parent_id] => 0
    [created_at] => 2021-04-15 04:32:37
    [updated_at] => 2021-04-15 04:32:37
    [entity_id] => 37
)

1 Answer 1

0

I realized that my main entity table i.e vinay_employee_entity attributes i.e. employee_name employee_email should also be in eav_attribute table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.