0

i'm trying to convert a module from Prestashop to Magento 2 but i have a problem with the class. I'm following several tutorial and my whole module is based on the administration part of the website.

so i'm trying to create a function that will make a simple "SELECT * FROM 'mytable'". I can already interact with my database with my controller but i can't with my block.

here is my actual block (the comment is my attempt of adapting a tutorial to my need)

namespace Mymodule\AdminPage\Block\Adminhtml;

use Magento\Backend\Block\Template;

class Contactslist extends \Magento\Backend\Block\Template
{

    /*public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Mymodule\AdminPage\Model\Contact $contact,
        \Magento\Framework\App\ResourceConnection $resource,
        array $data = []
    ) {
        $this->_contact = $contact;
        $this->_resource = $resource;

        parent::__construct(
            $context
            $data
        );
    }*/

    public function mycustom(){
        echo "test";
    }
}

and there is my controller who's supposed to be able to use the function who's suppose to make a Select * From mytable.

  namespace Mymodule\AdminPage\Controller\Adminhtml\Nettoyage;


  class Index extends \Magento\Backend\App\Action
  {
    /**
    * @var \Magento\Framework\View\Result\PageFactory
    */
    protected $resultPageFactory;

    /*
     * Constructor
     *
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
         parent::__construct($context);
         $this->resultPageFactory = $resultPageFactory;
    }


    public function execute()
    {

      // these line are just some test i made
        /*$contact = $this->_objectManager->create('Mymodule\AdminPage\Model\Contact');
        $data =[];
        $data['toreplace']="test1";
        $data['replaceby']="test2";
        $data['id']=2;
        $contact->setData($data);
        $contact->save();

        $data = $this->_objectManager->create('Mymodule\AdminPage\Model\Contact')->getCollection();
        foreach ($data as $d )  {
            echo $d->getToreplace();
            echo $d->getReplaceby();
        }*/


        return  $resultPage = $this->resultPageFactory->create();
    }
  }

did someone see something wrong or have an advice

1

Seems like you have the model (Mymodule\AdminPage\Model\Contact) and resource model for your table. You can use the same to retrieve the data.

 public function __construct(
         \Magento\Backend\App\Action\Context $context,
         \Mymodule\AdminPage\Model\ContactFactory $contactFactory,
         \Magento\Framework\View\Result\PageFactory $resultPageFactory
     ) {
          parent::__construct($context);
          $this->contactFactory = $contactFactory;
          $this->resultPageFactory = $resultPageFactory;
     }

And you can access the collection from your function mycustom() like below

public function mycustom(){
        $collection = $this->contactFactory->create()->getCollection();
    }
  • i have an error<?php namespace Mymodule\AdminPage\Block\Adminhtml; use Magento\Backend\Block\Template; class Contactslist extends \Magento\Backend\Block\Template { public function __construct( \Magento\Backend\App\Action\Context $context, \Mymodule\AdminPage\Model\ContactFactory $ContactFactory, \Magento\Framework\View\Result\PageFactory $resultPageFactory ) { $this->ContactFactory = $ContactFactory; $this->resultPageFactory = $resultPageFactory; } public function mycustom(){ echo "test"; } } – Antoine553 Jul 6 '17 at 8:52
  • The above solution should work if you have created your module in a right direction – Pavan Kumar Jul 6 '17 at 8:58
  • 1
    I hope you have the model class. If so, it should work. Please post the error you got(not the file). – Mike Jul 6 '17 at 9:18
0

When you are extending block template your construct must match with the extended class

<?php
namespace Mymodule\AdminPage\Block\Adminhtml;

class Contactslist extends \Magento\Backend\Block\Template
{

    private $ContactFactory;

    private $resultPageFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context, //Change this class
        \Mymodule\AdminPage\Model\ContactFactory $ContactFactory,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        array $data = []
    ) {
        $this->ContactFactory = $ContactFactory;
        $this->resultPageFactory = $resultPageFactory;
    }

    public function mycustom(){
        echo "test";
    }

?>
  • Can you specify a little bit pls i didn't understand – Antoine553 Jul 6 '17 at 9:28
  • The class you extend the parameters should be same. In your case you are extending \Magento\Backend\Block\Template so in your construct you need to pass the extended class context which is \Magento\Backend\Block\Template\Context – Priyank Jul 6 '17 at 9:33
0

Add Parent Construct Here

namespace Mymodule\AdminPage\Block\Adminhtml;

use Magento\Backend\Block\Template;

 protected $ContactFactory;

    protected $resultPageFactory;

class Contactslist extends \Magento\Backend\Block\Template
{

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Mymodule\AdminPage\Model\ContactFactory $ContactFactory,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
     array $data = []
) {
    $this->ContactFactory = $ContactFactory;
    $this->resultPageFactory = $resultPageFactory;

    parent::__construct($context, $data);
}

public function mycustom(){
    echo "test";
}
}

0

I made a mix of your answer and it worked thanks you all (sorry for my bad using of the forum withe the answer and comment i'm new)

here what i get

<?php
namespace ManoMano\AdminPage\Block\Adminhtml;

use Magento\Backend\Block\Template;

class Contactslist extends \Magento\Backend\Block\Template
{

 public function __construct(
     \Magento\Backend\Block\Template\Context $context,
     \ManoMano\AdminPage\Model\ContactFactory $contactFactory,
     \Magento\Framework\View\Result\PageFactory $resultPageFactory,
     array $data = []
 ) {
      parent::__construct($context, $data);
      $this->contactFactory = $contactFactory;
      $this->resultPageFactory = $resultPageFactory;
 }

public function mycustom(){
    $data = array();
    $model = $this->contactFactory->create();
    $data['toreplace']="test1";
    $data['replaceby']="test2";
    $data['id']=2;
    $model->setData($data);
    try {
        $model->save();
    }catch (\Exception $e) {
        return $e->getMessage();
    }

     $newData = $model->getCollection();
        foreach ($newData as $d )  {
            echo $d->getToreplace();
            echo $d->getReplaceby();
        }
}   

}

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.