0

I have created a custom module having two tables named helloworld_post and helloworld_tag and from front-end side I want to save fields values to these 2 tables. However I have created two respective models as well but its only working for helloworld_post not for helloworld_tag. Can anyone help me please?

Below is my code :

Post model - app/code/CompanyName/HelloWorld/Model/Post.php

namespace CompanyName\HelloWorld\Model;

class Post extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{

const CACHE_TAG = 'helloworld_post';
protected $_cacheTag = 'helloworld_post';
protected $_eventPrefix = 'helloworld_post';
protected function _construct()
{
    $this->_init('CompanyName\HelloWorld\Model\ResourceModel\Post');
}
public function getIdentities()
{
    return [self::CACHE_TAG . '_' . $this->getId()];
}
public function getDefaultValues()
{
    $values = [];
    return $values;
}
}

Post resource model -app/code/CompanyName/HelloWorld/Model/ResourceModel/Post.php

namespace CompanyName\HelloWorld\Model\ResourceModel;

class Post extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{   

public function __construct(
    \Magento\Framework\Model\ResourceModel\Db\Context $context
)
{
    parent::__construct($context);
}   
protected function _construct()
{
    $this->_init('CompanyName_helloworld_post', 'post_id');
}   
}

Tag model - app/code/CompanyName/HelloWorld/Model/Tag.php

namespace CompanyName\HelloWorld\Model;

class Tag extends \Magento\Framework\Model\AbstractModel 
implements   \Magento\Framework\DataObject\IdentityInterface
{

const CACHE_TAG = 'helloworld_tag';
protected $_cacheTag = 'helloworld_tag';
protected $_eventPrefix = 'helloworld_tag';
protected function _construct()
{
    $this->_init('CompanyName\HelloWorld\Model\ResourceModel\Tag');
}
public function getIdentities()
{
    return [self::CACHE_TAG . '_' . $this->getId()];
}
public function getDefaultValues()
{
    $values = [];
    return $values;
}
}

Tag resource model - app/code/CompanyName/HelloWorld/Model/ResourceModel/Tag.php

namespace CompanyName\HelloWorld\Model\ResourceModel;

class Tag extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{   

public function __construct(
    \Magento\Framework\Model\ResourceModel\Db\Context $context
)
{
    parent::__construct($context);
}   
protected function _construct()
{
    $this->_init('helloworld_tag', 'tag_id');
}   
}

Controller - app/code/CompanyName/HelloWorld/Controller/Index/Display.php

namespace CompanyName\HelloWorld\Controller\Index;

class Display extends \Magento\Framework\App\Action\Action {

protected $_pageFactory;
protected $_postFactory;
protected $_tagFactory;

public function __construct(
\Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \CompanyName\HelloWorld\Model\PostFactory $postFactory,
        \CompanyName\HelloWorld\Model\TagFactory $tagFactory
) {
    $this->_pageFactory = $pageFactory;
    $this->_postFactory = $postFactory;
    $this->_tagFactory = $tagFactory;
    return parent::__construct($context);
}      

public function execute() { 
    $model = $this->_postFactory->create();
    $model2 = $this->_tagFactory->create();
    $post = $this->getRequest()->getPostValue();

    if (isset($post) && !empty($post)) {
      //Save data for first table
       $model->setData($post['party']);
       $model->setName($post['party']['name']);
       $model->save();
        //Save data for second table
        $model2->setData($post['party']);
        $model2->setName($post['party']['address']);
        $model2->save();  
    } 
     return $this->_pageFactory->create();
 }

 }

Thanks.

1
  • Have you checked the Magento logs? Mar 7, 2018 at 12:50

1 Answer 1

0

Replaced __construct method with following code and deleted var/generation/.It's working fine.

Controller - app/code/CompanyName/HelloWorld/Controller/Index/Display.php

public function __construct(
    \Magento\Framework\App\Action\Context $context,            
    \CompanyName\HelloWorld\Model\PostFactory $postFactory,      
    \CompanyName\HelloWorld\Model\TagFactory $tagFactory,
    \Magento\Framework\View\Result\PageFactory $pageFactory

) {
    $this->_pageFactory = $pageFactory;
    $this->_postFactory = $postFactory;
    $this->_tagFactory = $tagFactory;
    return parent::__construct($context);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.