Question

So instead of update the student, this code add a new student.
I'm looking for a way that not using object manager, because the objectmanager is recommended not to use.

So when i searching for how to update a product in magento 2, i found alot of objectmanager way, but like i said, i wont use them.

I found 2 other ways, 1st way is using factory only, the 2nd way is using resource model and factory.

So here is the code :

namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

use Magento\Backend\App\Action\Context;
use Fudu\HelloWorld\Model\StudentsFactory;
use Fudu\HelloWorld\Model\ResourceModel\Students as ResourceModel;

class Update extends \Magento\Framework\App\Action\Action
{
    /**
     * @var StudentsFactory
     */
    protected $studentsFactory;

/**
 * @var ResourceModel
 */
protected $resourceModel;

public function __construct(
    Context $context,
    StudentsFactory $studentsFactory,
    ResourceModel $resourceModel
) {
    $this->resourceModel = $resourceModel;
    $this->studentsFactory = $studentsFactory;

    parent::__construct($context);
}
/**
 * Save action
 *
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @return \Magento\Framework\Controller\ResultInterface
 */
public function execute()
{
    $data = $this->getRequest()->getPostValue();

    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this->resultRedirectFactory->create();
//
        if ($data) {
            try{
                /** @var \Fudu\HelloWorld\Model\Students $model */

            $id = $this->getRequest()->getParam('id');

//                The 1st way :
                $model = $this->studentsFactory->create()->load($id);
                $model->setData($data);
                $model->save();

//                  The 2nd way :
//                $model = $this->studentsFactory->create();
//                $resource = $this->resourceModel->load($model,$id);
//                $model->setData($data);
//                $resource->save($model);


            $this->messageManager->addSuccessMessage(__('Update Student Successfully.'));

            // Redirect to your form page (or anywhere you want...)
            $resultRedirect->setPath('students/student/index');

            return $resultRedirect;
        }
        catch (\Exception $e) {
            $this->messageManager->addErrorMessage($e->getMessage());
        }

    }
}
}
Was it helpful?

Solution

$model->setData($data); this line reset your data. If there id field present $data variable then your code working fine. Otherwise you need to set id for update. Check following code:


$id = $this->getRequest()->getParam('id');
$model = $this->studentsFactory->create()->load($id);
if ($model->getId()) {
   $model->setData($data);
   $model->setId($id);
} else {
    $model->setData($data);
}

$model->save();

OTHER TIPS

Try to use this below code :

namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

use Magento\Backend\App\Action\Context;
use Fudu\HelloWorld\Model\StudentsFactory;

class Update extends \Magento\Framework\App\Action\Action
{
    /**
     * @var StudentsFactory
     */
    protected $studentsFactory;


    public function __construct(
        Context $context,
        StudentsFactory $studentsFactory
    ) {
        $this->studentsFactory = $studentsFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */

            if ($data) {
                try {
                $model = $this->studentsFactory->create();
                $model->setData($data);
                $model->save();

                $this->messageManager->addSuccessMessage(__('Add data Successfully.'));
                $resultRedirect->setPath('students/student/index');
                return $resultRedirect;
            }
            catch (\Exception $e) {
                $this->messageManager->addErrorMessage($e->getMessage());
            }

        }
    }
}

Remove generation and check it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top