cancel
Showing results for 
Search instead for 
Did you mean: 

How to see the exact error

SOLVED

How to see the exact error

There are two parts of this question . 1 . how can we add data to database using factory not through object manager . 2 . how can we find exactly what is the error in the code (dev mode is active , var/logs all files are checked). I wrote some code i know there is an issue in the code but i am unable to see this in magento logs . for the sake of adding data to database i am doing this . module is complete and working , in template file code is .

<?php foreach($block->getItemsResults() as $items) : ?>
<p><?php echo $items->getName() ?> : <?php echo $items->getDescription(); ?> </p>
<?php endforeach; ?>
<form method="post" action="<?php echo $block->getFormData() ?>">
<label>Add Name Here</label>
<input type="text" name="name" >
<input type="submit" name="submit" value="submit">
</form>

and this is the block file .

<?php 

namespace Mastering\SampleModule\Block;
use Magento\Framework\View\Element\Template;
use Mastering\SampleModule\Model\ResourceModel\Item\Collection;
use Mastering\SampleModule\Model\ResourceModel\Item\CollectionFactory;

class Hello extends Template {
	private $cf;
	private $messageManager;
	public function __construct(Template\Context $context , CollectionFactory $collectionfactory , \Magento\Framework\Message\ManagerInterface $messageManager , array $data = [] ){
		$this->cf = $collectionfactory ;
		parent::__construct($context,$data);
		$this->messageManager=$messageManager;
		
	}
	public function getItemsResults(){
		return $this->cf->create()->getItems();
	}
	public function getFormData(){
		$post=$this->getRequest()->getPost();
		if(!empty($post)){
			$name=$post['name'];
			$this->messageManager->addSuccessManager("data saved");
		}
	}
}

this is the code . what can i do to add data to database and how caan i find exact errors list . right now error is messageManager . but there is no way where i can see the error .

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to see the exact error

Hello @samair

 

Please find your both question answer here:

 

1. Add data to database using factory: with this post sharing reference code for your help, please follow below:

 

 

<?php
namespace [Vendor_Name]\[Extension_name]\Setup;

use [Vendor_Name]\[Extension_name]\Model\[Model_Name];
use [Vendor_Name]\[Extension_name]\Model\[Model_Name]Factory;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
	private $modelFactory;
	
	public function __construct([Model_Name]Factory $modelFactory)
    {
        $this->modelFactory = $modelFactory;
    }
	
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $modeldata = [
		 	[
            	'fieldname1' => 'value1',
            	'fieldname2' => 'value2'
			],
            [
				'fieldname1' => 'value3',
            	'fieldname2' => 'value4'
			]
         ];
		 
		 foreach ($modeldata as $data) {
			$this->modelFactory->create()->setData($data)->save();
        }
    }
}

 

 

2. Error in the code: as you already in dev mode it should display error on the screen/ logs but if you still facing issue please add this code in index.php:

 

error_reporting(E_ALL);
ini_set('display_errors', 1);
or there is a place for this. In bootstrap.php
#ini_set('display_errors', 1);

Hope both problems will solve.

Manish Mittal
https://www.manishmittal.com/

View solution in original post

1 REPLY 1

Re: How to see the exact error

Hello @samair

 

Please find your both question answer here:

 

1. Add data to database using factory: with this post sharing reference code for your help, please follow below:

 

 

<?php
namespace [Vendor_Name]\[Extension_name]\Setup;

use [Vendor_Name]\[Extension_name]\Model\[Model_Name];
use [Vendor_Name]\[Extension_name]\Model\[Model_Name]Factory;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
	private $modelFactory;
	
	public function __construct([Model_Name]Factory $modelFactory)
    {
        $this->modelFactory = $modelFactory;
    }
	
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $modeldata = [
		 	[
            	'fieldname1' => 'value1',
            	'fieldname2' => 'value2'
			],
            [
				'fieldname1' => 'value3',
            	'fieldname2' => 'value4'
			]
         ];
		 
		 foreach ($modeldata as $data) {
			$this->modelFactory->create()->setData($data)->save();
        }
    }
}

 

 

2. Error in the code: as you already in dev mode it should display error on the screen/ logs but if you still facing issue please add this code in index.php:

 

error_reporting(E_ALL);
ini_set('display_errors', 1);
or there is a place for this. In bootstrap.php
#ini_set('display_errors', 1);

Hope both problems will solve.

Manish Mittal
https://www.manishmittal.com/