1
There are two parts to this question.

1. How can we add data to the 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 the database, i am doing this. the 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 , 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 the database and how can i find an exact errors list? right now the error is messageManager. but there is no way where i can see the error. 
  • where is $messageManager; ? you are using its method but have not added it in your constructor – Naveed Asim Sep 25 '18 at 19:07
  • yes sir i have written it in the question ... i know the error is $messageManager but there is no way to see the error log ? – Flutterer Sep 25 '18 at 19:08
  • dev mode throws exception when there is error. isn't that working for you ? – Naveed Asim Sep 25 '18 at 19:11
  • it shows error when i remove the private $messageMassanger .. but right now it is not showing any error . but as we all know this is an error . – Flutterer Sep 25 '18 at 19:12
2

If developer mode is active then errors should be thrown in browser directly.

For debugging it is always good to use xdebug.

Of course you can use custom logging:

__construct(
   \Psr\Log\LoggerInterface $logger
) {
   $this->logger = $logger;
}

And then in the code:

$this->logger->info('message');

For the message manager here is info how to use it properly: https://magento.stackexchange.com/a/88118/72246

You've asked about adding to database without using object manager. Yes you should never use objectmanager directly. But to answer to that question please describe what do you want to save to database, so I can help.

  • thankyou for the knowledge . what is "ofc" and what is "ind" ? and second thing you can see in the template file i have attached . there is a simple input type text . i want to take data from there and add it to some table in my database . – Flutterer Sep 25 '18 at 19:25

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.