cancel
Showing results for 
Search instead for 
Did you mean: 

Select All in Block for Controller

SOLVED

Select All in Block for Controller

Hello sorry for my noobisme promise i almost finish.

so i have mi controller

<?php
      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()
        {
// i tested these line of code and they worked so i keep them just in case
/*$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(); } } ?>

my block

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

use Magento\Backend\Block\Template;

class Contactslist extends \Magento\Backend\Block\Template
{
	
	public function mycustom(){
    	    echo "test";
	}
}

I have to put some function in my controller who are supposed to depend from my class. The most important function is the function getAll() but even if i can interact with my database in my controller it seems i can't seems my block

here is the function (from Prestashop)

public static function getAll()
    {
        return Db::getInstance()->executeS('SELECT * FROM "my table"');
    }

//so i tried every test i can to interact with my database but i can't
12 REPLIES

Re: Select All in Block for Controller

in fact i'm trying to follow the tutorial but in the admin side so my Block should look like that but it gave me 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\Contact $contact,
        \Magento\Framework\App\ResourceConnection $resource,
        array $data = []
    ) {
        $this->_contact = $contact;
        $this->_resource = $resource;

        parent::__construct(
            $context,
            $data
        );
    }
	
	public function mycustom(){
    	echo "test";
	}
}

Re: Select All in Block for Controller

Re: Select All in Block for Controller

i tried but i have an error like:

There has been an error processing your request

Exception printing is disabled by default for security reasons.

Error log record number: 1048031972430

 

<?php
      namespace Mymodule\AdminPage\Controller\Adminhtml\Nettoyage;

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

        /*
         * 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,
            \Mymodule\AdminPage\Model\ContactFactory $modelContactFactory
        ) {
             parent::__construct($context);
             $this->resultPageFactory = $resultPageFactory;
             $this->_modelContactFactory = $modelContactFactory;
        }
        
        public function execute()
        {
            /*$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();
        }
      }
    ?>

 

Re: Select All in Block for Controller

@Antoine553, are you looking for getAll() method in your Block class? Or You want Model in your controller file.

 

Thanks.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Select All in Block for Controller

I'm trying to make the function getAll() in my block so my other function in my controller can use it

Re: Select All in Block for Controller

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

use Magento\Backend\Block\Template;

class Contactslist extends Template
{
	
	public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Mymodule\AdminPage\Model\ContactFactory $contactFactory,
        array $data = []
    ) {
        $this->contactFactory = $contactFactory;
        parent::__construct(
            $context,
            $data
        );
    }
	
	public function mycustom(){
    	echo "test";
$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(); } } }

Try with above code in your Block file and remove var/generation folder.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Select All in Block for Controller

After trying that

<?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,
        array $data = []
    ) {
        $this->contactFactory = $contactFactory;
        
        parent::__construct(
            $context,
            $data
        );
    }
	
	public function mycustom(){
    	echo "test";
	}
	
}

i have an error :

There has been an error processing your request

Exception printing is disabled by default for security reasons.

Error log record number: 403320463966

Re: Select All in Block for Controller

please show your error log from var/report  without error i cant say why you have display error.

 

Update 

Based on your report issue on controller file,

So can you please share your code which you have tried in your module.

 

Thanks.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Select All in Block for Controller

a:4:{i:0;s:293:"Warning: include(/home/mademonstration/public_html/newmagento3/var/generation/Mymodule/AdminPage/Controller/Adminhtml/Nettoyage/index/Interceptor.php): failed to open stream: No such file or directory in /home/mademonstration/public_html/newmagento3/vendor/composer/ClassLoader.php on line 412";i:1;s:4027:"#0 /home/mademonstration/public_html/newmagento3/vendor/composer/ClassLoader.php(412): Magento\Framework\App\ErrorHandler->handler(2, 'include(/home/m...', '/home/mademonst...', 412, Array)
#1 /home/mademonstration/public_html/newmagento3/vendor/composer/ClassLoader.php(412): Composer\Autoload\includeFile()
#2 /home/mademonstration/public_html/newmagento3/vendor/composer/ClassLoader.php(301): Composer\Autoload\includeFile('/home/mademonst...')
#3 [internal function]: Composer\Autoload\ClassLoader->loadClass('Mymodule\\AdminP...')
#4 [internal function]: spl_autoload_call('Mymodule\\AdminP...')
#5 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Code/Reader/ClassReader.php(19): ReflectionClass->__construct('Mymodule\\AdminP...')
#6 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/ObjectManager/Definition/Runtime.php(44): Magento\Framework\Code\Reader\ClassReader->getConstructor('Mymodule\\AdminP...')
#7 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(71): Magento\Framework\ObjectManager\Definition\Runtime->getParameters('Mymodule\\AdminP...')
#8 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/ObjectManager/ObjectManager.php(57): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Mymodule\\AdminP...', Array)
#9 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/ActionFactory.php(40): Magento\Framework\ObjectManager\ObjectManager->create('Mymodule\\AdminP...')
#10 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/Router/Base.php(300): Magento\Framework\App\ActionFactory->create('Mymodule\\AdminP...')
#11 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/Router/Base.php(161): Magento\Framework\App\Router\Base->matchAction(Object(Magento\Framework\App\Request\Http), Array)
#12 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/FrontController.php(50): Magento\Framework\App\Router\Base->match(Object(Magento\Framework\App\Request\Http))
#13 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Interception/Interceptor.php(74): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#14 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Interception/Chain/Chain.php(70): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#15 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\Interception\Chain\Chain->invokeNext('Magento\\Framewo...', 'dispatch', Object(Magento\Framework\App\FrontController\Interceptor), Array, 'install')
#16 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Module/Plugin/DbStatusValidator.php(69): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#17 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/Interception/Interceptor.php(142): Magento\Framework\Module\Plugin\DbStatusValidator->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#18 /home/mademonstration/public_html/newmagento3/var/generation/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, Array)
#19 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#20 /home/mademonstration/public_html/newmagento3/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#21 /home/mademonstration/public_html/newmagento3/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#22 {main}";s:3:"url";s:121:"/newmagento3/admin_1k6h0m/adminpage/nettoyage/index/key/1b263dc66622a22c88b26b0b6a96a5f0fdda5e7d9641fbd6ea451cfe1601c49a/";s:11:"script_name";s:22:"/newmagento3/index.php";}