3

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());
        }

    }
}
}
3

$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();
  • yes thanks you, +1 vote for you my friend – fudu Jul 21 '18 at 4:49
2

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.

  • doesn't work though .. @@ the way of @Sohel Rana up there is work . But thanks you for help. – fudu Jul 21 '18 at 4:48
  • I thought Your question title is about add record. So, I upload that answer. – Rohan Hapani Jul 21 '18 at 5:17
  • LOL :D sorry because confusing you. – fudu Jul 21 '18 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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