I'm getting an error everytime I send my form in order to store data in a custom table I created. This is the error:
{"0":"M\u00e9todo no v\u00e1lido Transom\\Emall\\Model\\StoreOwner::create","1":"<pre>#1 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm->execute() called at [generated\/code\/Transom\/Emall\/Controller\/Index\/SendStoreOwnerForm\/Interceptor.php:24]\n#2 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor->execute() called at [vendor\/magento\/framework\/App\/Action\/Action.php:108]\n#3 Magento\\Framework\\App\\Action\\Action->dispatch(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#4 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor->___callParent('dispatch', array(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#)) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#5 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#6 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor->___callPlugins('dispatch', array(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#), array(array('designLoader', 'customerNotifica...', 'catalog_app_acti...', 'tax-app-action-d...', 'weee-app-action-...', 'storeCheck', 'contextPlugin', 'customer-app-act...'))) called at [generated\/code\/Transom\/Emall\/Controller\/Index\/SendStoreOwnerForm\/Interceptor.php:39]\n#7 Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor->dispatch(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/App\/FrontController.php:159]\n#8 Magento\\Framework\\App\\FrontController->processRequest(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#, &Transom\\Emall\\Controller\\Index\\SendStoreOwnerForm\\Interceptor#0000000051f3c5dd0000000073cc3fa5#) called at [vendor\/magento\/framework\/App\/FrontController.php:99]\n#9 Magento\\Framework\\App\\FrontController->dispatch(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:58]\n#10 Magento\\Framework\\App\\FrontController\\Interceptor->___callParent('dispatch', array(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#)) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:138]\n#11 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/module-store\/App\/FrontController\/Plugin\/RequestPreprocessor.php:94]\n#12 Magento\\Store\\App\\FrontController\\Plugin\\RequestPreprocessor->aroundDispatch(&Magento\\Framework\\App\\FrontController\\Interceptor#0000000051f3c7a50000000073cc3fa5#, &Closure#0000000051f3c7830000000073cc3fa5#, &Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#13 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/module-page-cache\/Model\/App\/FrontController\/BuiltinPlugin.php:73]\n#14 Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin->aroundDispatch(&Magento\\Framework\\App\\FrontController\\Interceptor#0000000051f3c7a50000000073cc3fa5#, &Closure#0000000051f3c7830000000073cc3fa5#, &Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:135]\n#15 Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/Interception\/Interceptor.php:153]\n#16 Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins('dispatch', array(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#), NULL) called at [generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php:26]\n#17 Magento\\Framework\\App\\FrontController\\Interceptor->dispatch(&Magento\\Framework\\App\\Request\\Http#0000000051f3c66b0000000073cc3fa5#) called at [vendor\/magento\/framework\/App\/Http.php:136]\n#18 Magento\\Framework\\App\\Http->launch() called at [generated\/code\/Magento\/Framework\/App\/Http\/Interceptor.php:24]\n#19 Magento\\Framework\\App\\Http\\Interceptor->launch() called at [vendor\/magento\/framework\/App\/Bootstrap.php:258]\n#20 Magento\\Framework\\App\\Bootstrap->run(&Magento\\Framework\\App\\Http\\Interceptor#0000000051f3c6240000000073cc3fa5#) called at [slp\/tienda1\/index.php:50]\n<\/pre>","url":"\/slp\/tienda1\/storeowner\/Index\/SendStoreOwnerForm\/","script_name":"\/slp\/tienda1\/index.php"}
I don ́t get it. This are my files:
Model
<?php
namespace Transom\Emall\Model;
class StoreOwner extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
{
 const CACHE_TAG = 'store_owner_emall';
 protected $_cacheTag = 'store_owner_emall';
 protected $_eventPrefix = 'store_owner_emall';
 public function _construct()
 {
 $this->_init("Transom\Emall\Model\ResourceModel\StoreOwner");
 }
 public function getIdentities()
 {
 return [self::CACHE_TAG . '_' . $this->getId()];
 }
 public function getDefaultValues()
 {
 $values = [];
 return $values;
 }
}
ResourceModel:
<?php
namespace Transom\Emall\Model\ResourceModel;
class StoreOwner 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("store_owner_emall", "store_owner_id");
 }
}
Collection:
<?php
namespace Transom\Emall\Model\ResourceModel\StoreOwner;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
 protected $_idFieldName = 'store_owner_id';
 protected $_eventPrefix = 'store_owner_emall_collection';
 protected $_eventObject = 'post_collection';
 /**
 * Define resource model
 *
 * @return void
 */
 protected function _construct()
 {
 $this->_init('Transom\Emall\Model\StoreOwner', 'Transom\Emall\Model\ResourceModel\StoreOwner');
 }
}
Custom table:
<?php
namespace Transom\Emall\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
{
 public function install(SchemaSetupInterface $setup, 
 ModuleContextInterface $context)
 {
 $installer = $setup;
 $installer->startSetup();
 $table = $installer->getConnection()
 ->newTable($installer->getTable('store_owner_emall'))
 ->addColumn(
 'store_owner_id',
 \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
 null,
 ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
 'Store Owner Id'
 )
 ->addColumn(
 'customer_name',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['unsigned' => true],
 'Customer'
 )
 ->addColumn(
 'business',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Business Name'
 )
 ->addColumn(
 'business_line',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Business Line'
 )
 ->addColumn(
 'website_address',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Website Address'
 )
 ->addColumn(
 'facebook',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Facebook'
 )
 ->addColumn(
 'instagram',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Instagram'
 )
 ->addColumn(
 'email',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Email'
 )
 ->addColumn(
 'phone_number',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Phone Number'
 )
 ->addColumn(
 'business_phone_number',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Business Phone Number'
 )
 ->addColumn(
 'zip_code',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Zip Code'
 )
 ->addColumn(
 'state',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'State'
 )
 ->addColumn(
 'city',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'City'
 )
 ->addColumn(
 'suburb',
 \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
 null,
 ['nullable' => true],
 'Suburb'
 )
 ->addColumn(
 'created_at',
 \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
 null,
 ['nullable' => false],
 'Created At'
 )
 ->addIndex(
 $installer->getIdxName('store_owner_emall', ['store_owner_id']),
 ['store_owner_id']
 )
 ->setComment('Store Owner Landingpage');
 $installer->getConnection()->createTable($table);
 $installer->endSetup();
 }
}
And Finally this is what I'm doing in order to store data in the controller:
 <?php
namespace Transom\Emall\Controller\Index;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Action\Action;
 use Magento\Framework\App\Action\Context;
 use Magento\Framework\View\Result\PageFactory;
 use Magento\Framework\Filesystem;
 use Magento\Framework\App\Filesystem\DirectoryList;
 use Magento\Framework\Controller\ResultFactory; 
use Psr\Log\LoggerInterface;
use Transom\Emall\Model\StoreOwner;
use Transom\Pakke\Logger\Logger;
class SendStoreOwnerForm extends Action
{
 
 /**
 * @var TransportBuilder
 */
 protected $transportBuilder;
 /**
 * @var LoggerInterface
 */
 protected $logger;
 /**
 * @var PageFactory
 */
 protected $resultPageFactory;
 /**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
 protected $productRepository;
 /**
 * @var \Magento\Product\Model\ProductFactory
 */
 protected $productFactory;
 /**
 * @var \Magento\CatalogInventory\Api\StockRegistryInterface
 */
 protected $stockRegistry;
 /**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
 private $storeManager;
 private $post;
 /**
 * @var Data
 */
 protected $customerSession;
 protected $_storeOwner;
 protected $_logger;
 /**
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository ,
 * @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
 * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
 * @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
 * @param Filesystem $filesystem
 * @param \Magento\Framework\App\Action\Context $context
 * @param PageFactory $pageFactory
 * @param LoggerInterface $logger
 * @param TransportBuilder $transportBuilder
 * @param Data $customerSession
 * @param StoreOwner $storeOwner
 */
 public function __construct(
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
 \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
 \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
 \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
 \Magento\Framework\Filesystem $filesystem,
 Context $context, 
 PageFactory $pageFactory,
 LoggerInterface $logger,
 TransportBuilder $transportBuilder,
 StoreOwner $storeOwner,
 Logger $loggerPakke
 ) {
 $this->logger = $logger;
 $this->transportBuilder = $transportBuilder;
 $this->storeManager = $storeManager;
 $this->productRepository = $productRepository;
 $this->productFactory = $productFactory;
 $this->resultPageFactory = $pageFactory;
 $this->stockRegistry = $stockRegistry;
 $this->_storeOwner = $storeOwner;
 $this->_logger = $loggerPakke;
 parent::__construct($context);
 }
 
 public function execute()
 {
 $post = $this->getRequest()->getPostValue();
 $model = $this->_storeOwner->create();
 $formData = array(
 'name'=>$post["name"],
 'business'=>$post["business"],
 'business_line'=>$post["business_line"],
 'website_address'=>$post["web_address"],
 'facebook'=>$post["facebook"],
 'instagram'=>$post["instagram"],
 'email'=>$post["email"],
 'phone_number'=>$post["telephone"],
 'business_phone_number'=>$post["telephone_business"],
 'zip_code'=>$post["zip_code"],
 'state'=>$post["state"],
 'city'=>$post["city"],
 'suburb'=>$post["suburb"]
 );
 /*Send email to admin*/
 
 $receiverInfo = [
 'name' => 'Admin',
 'email' => '[email protected]'
 ];
 $emails = ['[email protected]']; //users that will receive the email
 $store = $this->storeManager->getStore();
 $transport = $this->transportBuilder->setTemplateIdentifier(
 'Transom_Emall_Store_Owner'
 )->setTemplateOptions(
 ['area' => 'frontend', 'store' => $store->getId()]
 )->addTo(
 $emails, $receiverInfo['name']
 )->setTemplateVars(
 $formData
 )->setFrom(
 'general'
 )->getTransport();
 try {
 // Send an email
 $transport->sendMessage();
 $model->addData([
 'name'=>$post["name"],
 'business'=>$post["business"],
 'business_line'=>$post["business_line"],
 'website_address'=>$post["web_address"],
 'facebook'=>$post["facebook"],
 'instagram'=>$post["instagram"],
 'email'=>$post["email"],
 'phone_number'=>$post["telephone"],
 'business_phone_number'=>$post["telephone_business"],
 'zip_code'=>$post["zip_code"],
 'state'=>$post["state"],
 'city'=>$post["city"],
 'suburb'=>$post["suburb"]
 ]);
 $modelSaved = $model->save();
 if($modelSaved){
 $this->logger->info("Model was saved!!");
 
 }
 else{
 $this->logger->info("Model was not saved!!");
 }
 
 } catch (\Exception $e) {
 // Write a log message whenever get errors
 $this->logger->critical($e->getMessage());
 }
 
 }
}
I don't get what is wrong whit this, greetings!
- 
 Share the code of entire controllerDhiren Vasoya– Dhiren Vasoya2020年10月01日 17:54:16 +00:00Commented Oct 1, 2020 at 17:54
- 
 @DhirenVasoya done. please checkOscar Vazquez– Oscar Vazquez2020年10月01日 18:34:24 +00:00Commented Oct 1, 2020 at 18:34
1 Answer 1
As I can see you already created custom model for managing or interacting with custom database table.
Model class is : \Transom\Emall\Model\StoreOwner
So, use this class as factory class in your controller's __construct method as per below.
protected $_storeOwner;
public function __construct(\Transom\Emall\Model\StoreOwnerFactory $storeowner)
{
 $this->_storeOwner = $storeowner;
}
public function execute()
{
 // create a payload array of your data where key consist column name and value consist value for that columns
 $modelPayload['col1'] = $val1;
 $modelPayload['col2'] = $val2;
 $modelPayload['col3'] = $val3;
 $modelPayload['col4'] = $val4;
 
 $stobj = $_storeOwner->create();
 $stobj = $stobj->setData($modelPayload)->save();
}
Explore related questions
See similar questions with these tags.