0

i'm doing update "students" on magento 2.2.5
and i'm stuck in how to get student data by id
i saw they use repository to load by id, but when i come in this file

C:\xampp\htdocs\magento\vendor\magento\module-catalog\Model\ProductRepository.php

they using factory to load by id
C:\xampp\htdocs\magento\vendor\magento\module-catalog\Model\ProductRepository.php


here is my students table in mysql:
enter image description here but with what i search for, the load() method in factory is cant be use anymore.
so i wanna ask how to how to get student data by id


my code for now:

<?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;
}
}
  • as per your question you need to use Join for getting students data from your products. – Dharmendra Jadav Jul 20 '18 at 7:02
  • sorry i dont follow , can u explain it more clearly – fudu Jul 20 '18 at 7:04
  • could you please give your students table detail so i can explain you more in detail. – Dharmendra Jadav Jul 20 '18 at 7:09
  • oh okay, i have update it on my post , please take a look, and thanks for reading my post – fudu Jul 20 '18 at 7:11
  • in your table where is the product id ? – Dharmendra Jadav Jul 20 '18 at 7:13
0

Please try this.

<?php

namespace Fudu\HelloWorld\Controller\Adminhtml\Student;

use Magento\Backend\App\Action;
use Fudu\HelloWorld\Model\ResourceModel\Student\CollectionFactory;
use \Magento\Framework\Exception\NoSuchEntityException;


class Update extends Action
{
    /**
     * @var StudentsFactory
     */
    protected $studentsFactory;

   public function __construct(
       Action\Context $context,
       \Fudu\HelloWorld\Model\ResourceModel\Student\CollectionFactory $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;
}
}

I have made function in helper for demo. Please use as per your need.

Or you use this.

$students = $objectManager->create('<vendor>\<module>\Model\ResourceModel\Student\Collection')->addFieldToFilter('<your_id>', 1);

Please do not use object manager create instant of your collection. I have giving just example for getting data by id.

  • but what about the return ? and u still use the factory as i said on the post. i dont want to use the factory,because it deprecated – fudu Jul 20 '18 at 7:33
  • objectmanager is recommended not to use for now – fudu Jul 20 '18 at 7:34
  • You can use model directly ( \<vendor>\<module\Model\Student $_students) if you not use factory – Dharmendra Jadav Jul 20 '18 at 7:34
  • well, i'm following you with factory, but it throw an error: 1 exception(s): Exception #0 (InvalidArgumentException): Invalid return type Exception #0 (InvalidArgumentException): Invalid return type #0 C:\xampp\htdocs\magento\generated\code\Magento\Framework\App\Http\Interceptor.php(24): Magento\Framework\App\Http->launch() #1 C:\xampp\htdocs\magento\vendor\magento\framework\App\Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch() #2 C:\xampp\htdocs\magento\index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor)) #3 {main} – fudu Jul 20 '18 at 7:36
  • i put my code on the post, please take a look, thanks you – fudu Jul 20 '18 at 7:39

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.