I am building a module that requires a custom table and insert. I have created a controller to test the insert, but I am getting an error. Here is what I have and the error:
Veratad/AgeVerification/Model/DataExample.php
<?php
namespace Veratad\AgeVerification\Model;
class DataExample extends \Magento\Framework\Model\AbstractModel{
public function _construct(){
$this->_init("veratad_details", "user_id");
}
}
Veratad/AgeVerification/Model/ResourceModel/DataExample.php
namespace Veratad\AgeVerification\Model;
class DataExample extends \Magento\Framework\Model\AbstractModel{
public function _construct(){
$this->_init("Veratad\AgeVerification\Model\ResourceModel\DataExample");
}
}
Veratad/AgeVerification/Model/ResourceModel/Collection.php
namespace Veratad\AgeVerification\Model\ResourceModel\DataExample;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection{
public function _construct(){
$this->_init("Veratad\AgeVerification\Model\DataExample","Veratad\AgeVerification\Model\ResourceModel\DataExample");
}
}
Veratad/AgeVerification/Controller/Index/Index.php
namespace Veratad\AgeVerification\Controller\Index;
use Veratad\AgeVerification\Model\DataExampleFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action{
protected $_dataExample;
protected $resultRedirect;
public function __construct(\Magento\Framework\App\Action\Context $context,
\Veratad\AgeVerification\Model\DataExampleFactory $dataExample,
\Magento\Framework\Controller\ResultFactory $result){
parent::__construct($context);
$this->_dataExample = $dataExample;
$this->resultRedirect = $result;
}
public function execute(){
$resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
$model = $this->_dataExample->create();
$model->addData([
"user_id" => '123456',
"action" => 'PASS'
]);
$saveData = $model->save();
if($saveData){
$this->messageManager->addSuccess( __('Insert Record Successfully !') );
}
return $resultRedirect;
}
}
Error Message when Controller is Run
{
"0": "Class veratad_details does not exist",
"1": "#0 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Code\/Reader\/ClassReader.php(19): ReflectionClass->__construct('veratad_details')\n#1 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/Definition\/Runtime.php(44): Magento\\Framework\\Code\\Reader\\ClassReader->getConstructor('veratad_details')\n#2 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/Factory\/Dynamic\/Developer.php(48): Magento\\Framework\\ObjectManager\\Definition\\Runtime->getParameters('veratad_details')\n#3 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/ObjectManager.php(70): Magento\\Framework\\ObjectManager\\Factory\\Dynamic\\Developer->create('veratad_details')\n#4 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Model\/AbstractModel.php(477): Magento\\Framework\\ObjectManager\\ObjectManager->get('veratad_details')\n#5 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Model\/AbstractModel.php(215): Magento\\Framework\\Model\\AbstractModel->_getResource()\n#6 \/home\/z3hjb5ti7gx1\/public_html\/magento\/app\/code\/Veratad\/AgeVerification\/Model\/DataExample.php(8): Magento\\Framework\\Model\\AbstractModel->_init('veratad_details', 'id')\n#7 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Model\/AbstractModel.php(194): Veratad\\AgeVerification\\Model\\DataExample->_construct()\n#8 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/Factory\/AbstractFactory.php(111): Magento\\Framework\\Model\\AbstractModel->__construct(Object(Magento\\Framework\\Model\\Context), Object(Magento\\Framework\\Registry), NULL, NULL, Array)\n#9 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/Factory\/Dynamic\/Developer.php(66): Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->createObject('Veratad\\\\AgeVeri...', Array)\n#10 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/ObjectManager\/ObjectManager.php(56): Magento\\Framework\\ObjectManager\\Factory\\Dynamic\\Developer->create('Veratad\\\\AgeVeri...', Array)\n#11 \/home\/z3hjb5ti7gx1\/public_html\/magento\/generated\/code\/Veratad\/AgeVerification\/Model\/DataExampleFactory.php(43): Magento\\Framework\\ObjectManager\\ObjectManager->create('\\\\Veratad\\\\AgeVer...', Array)\n#12 \/home\/z3hjb5ti7gx1\/public_html\/magento\/app\/code\/Veratad\/AgeVerification\/Controller\/Index\/Index.php(21): Veratad\\AgeVerification\\Model\\DataExampleFactory->create()\n#13 \/home\/z3hjb5ti7gx1\/public_html\/magento\/generated\/code\/Veratad\/AgeVerification\/Controller\/Index\/Index\/Interceptor.php(24): Veratad\\AgeVerification\\Controller\\Index\\Index->execute()\n#14 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/App\/Action\/Action.php(107): Veratad\\AgeVerification\\Controller\\Index\\Index\\Interceptor->execute()\n#15 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Framework\\App\\Action\\Action->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#16 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Veratad\\AgeVerification\\Controller\\Index\\Index\\Interceptor->___callParent('dispatch', Array)\n#17 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Veratad\\AgeVerification\\Controller\\Index\\Index\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#18 \/home\/z3hjb5ti7gx1\/public_html\/magento\/generated\/code\/Veratad\/AgeVerification\/Controller\/Index\/Index\/Interceptor.php(39): Veratad\\AgeVerification\\Controller\\Index\\Index\\Interceptor->___callPlugins('dispatch', Array, Array)\n#19 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/App\/FrontController.php(55): Veratad\\AgeVerification\\Controller\\Index\\Index\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#20 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Framework\\App\\FrontController->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#21 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Framework\\App\\FrontController\\Interceptor->___callParent('dispatch', Array)\n#22 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/module-store\/App\/FrontController\/Plugin\/RequestPreprocessor.php(94): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#23 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\Store\\App\\FrontController\\Plugin\\RequestPreprocessor->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n#24 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/module-page-cache\/Model\/App\/FrontController\/BuiltinPlugin.php(73): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#25 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n#26 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#27 \/home\/z3hjb5ti7gx1\/public_html\/magento\/generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php(26): Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins('dispatch', Array, NULL)\n#28 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Framework\\App\\FrontController\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#29 \/home\/z3hjb5ti7gx1\/public_html\/magento\/generated\/code\/Magento\/Framework\/App\/Http\/Interceptor.php(24): Magento\\Framework\\App\\Http->launch()\n#30 \/home\/z3hjb5ti7gx1\/public_html\/magento\/vendor\/magento\/framework\/App\/Bootstrap.php(256): Magento\\Framework\\App\\Http\\Interceptor->launch()\n#31 \/home\/z3hjb5ti7gx1\/public_html\/magento\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#32 {main}",
"url": "\/magento\/ageverification\/?SID=phgg2flq3hk86pk1d5u9b3fr83",
"script_name": "\/magento\/index.php"
}
Here is the table set up
<?php
namespace Veratad\AgeVerification\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
/**
* Create table 'veratad_details'
*/
$table = $setup->getConnection()
->newTable($setup->getTable('veratad_details'))
->addColumn(
'user_id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'User ID'
)
->addColumn(
'action',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
255,
['nullable' => false, 'default' => ''],
'Veratad Result'
)->setComment("Veratad Details Table");
$setup->getConnection()->createTable($table);
}
}
I have confirmed that the table does exist.