0

i'm doing update "students" on magento 2.2.5
and i'm stuck in how to get student data by id
the error say:

1 exception(s):
Exception #0 (InvalidArgumentException): Invalid return type

here is my code:

<?php

namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

use Magento\Backend\App\Action;
use Fudu\HelloWorld\Model\StudentsFactory;
use \Magento\Framework\Exception\NoSuchEntityException;

class Update extends Action
{

/**
 * @var StudentsFactory
 */
protected $studentsFactory;

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

/**
 *  Load Student data by given Student Id
 *
 * @return $student
 * @throws NoSuchEntityException
 */

public function execute()
{

    $id = $this->getRequest()->getParam('id');
    $student = $this->studentsFactory->create();
    $student->load($id);

    return $student;
}
}
  • 1
    I think issue is with returning $student object. I am not sure but it may be throwing error because you are returning object through controller. – Dhaval Solanki Jul 20 '18 at 8:35
  • i delete the * return $student in " /** * Load Student data by given Student Id * * return $student * throws NoSuchEntityException */ " but doesn't work too – fudu Jul 20 '18 at 8:38
  • what error you are getting after removing it ? – Dhaval Solanki Jul 20 '18 at 8:42
  • the same as before – fudu Jul 20 '18 at 8:42
  • wait , did you say i cant return something from a controller? – fudu Jul 20 '18 at 8:48
2

Reason for error is you can not return Object type of Fudu\HelloWorld\Model\StudentsFactory In controller execute method.

Return type for controller should be :

  • “Json” result (return json type of data)
  • “Raw” result (plain string)
  • “Forward” result ($result->forward('noroute'); )
  • “Redirect” result ( $result->setPath('');)

You can read more on it source

  • if it worked you can accept my answer – Dhaval Solanki Jul 20 '18 at 9:05
0

@return $student to @return Fudu\HelloWorld\Model\Students

/**
 *  Load Student data by given Student Id
 *
 * @return Fudu\HelloWorld\Model\Students
 * @throws NoSuchEntityException
 */

public function execute()
{

    $id = $this->getRequest()->getParam('id');
    $student = $this->studentsFactory->create();
    $student->load($id);

    return $student;
}
  • Can you please explain what exactly you asking to change ? – Dhaval Solanki Jul 20 '18 at 8:56
  • Magento cannot defined $student type. @return can be define type (string, int, array,..) or model object with full path. – Bong Channarith Jul 20 '18 at 9:00
  • you can find return type \Magento\Framework\Controller\Result\.. for controller. – Bong Channarith Jul 20 '18 at 9:06
0

Turn out i cant use controller to return a object, and i have to use block to do that. so my block is

 public function getStudentById(){
    $id = $this->getRequest()->getParam('id');
    $student = $this->_studentsFactory->create();
    $student->load($id);
    return $student;
}

and then in my phtml file

<?php /** @var Fudu\HelloWorld\Block\Adminhtml\StudentsBlock $block */ ?>
<form action="<?php echo $block->getPostUpdateStudentsUrl() ?>" method="post"
      style="border: 1px solid black; width: 200px; height: 250px; padding-left:10px;">

    Name:<br>
    <input type="text" name="name" required value=<?php echo $block->getStudentById()->getName() ?>>
    <br>

    Class:<br>
    <input type="number" name="class_id" value=<?php echo $block->getStudentById()->getName() ?>>
    <br>

    Gender:<br>
    <input type="text" name="gender" value=<?php echo $block->getStudentById()->getName() ?>>
    <br>

    Age:<br>
    <input type="number" name="age" value=<?php echo $block->getStudentById()->getName() ?>>
    <br><br>

    <input type="submit" value="Submit">

</form>

thanks all for your help.

  • Please use the edit link on your question to add additional information. The Post Answer button should be used only for complete answers to the question. - From Review – Mohit Kumar Arora Jul 20 '18 at 9:23
  • its the final answer here – fudu Jul 20 '18 at 9:40

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.