0

I'm having a hard time making Magento2 create my model tables. This is my code so far:

app\code\Werules\Chatbot\Model\IncomingMessages.php

<?php

namespace Werules\Chatbot\Model;

use Magento\Cron\Exception;
use Magento\Framework\Model\AbstractModel;

class IncomingMessages extends AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $_dateTime;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Werules\Chatbot\Model\ResourceModel\IncomingMessages::class);
    }

}

app\code\Werules\Chatbot\Model\ResourceModel\Collection.php

<?php

namespace Werules\Chatbot\Model\ResourceModel\Contact;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

/**
 * Contact Resource Model Collection
 *
 * @author      Pierre FAY
 */
class Collection extends AbstractCollection
{
    /**
     * Initialize resource collection
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('Werules\Chatbot\Model\IncomingMessages', 'Werules\Chatbot\Model\ResourceModel\IncomingMessages');
    }
}

app\code\Werules\Chatbot\Model\ResourceModel\IncomingMessages.php

<?php

namespace Werules\Chatbot\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class IncomingMessages extends AbstractDb
{
    /**
     * Initialize resource
     *
     * @return void
     */
    public function _construct()
    {
        $this->_init('chatbot_incoming_messages', 'id');
    }
}

app\code\Werules\Chatbot\Setup\InstallSchema.php

<?php

namespace Werules\Chatbot\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws \Zend_Db_Exception
     */

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /**
         * Create table 'chatbot_incoming_messages'
         */

        if (!$setup->getConnection()->isTableExists($setup->getTable('chatbot_incoming_messages'))) {
            $table = $setup->getConnection()
                ->newTable($setup->getTable('chatbot_incoming_messages'))
                ->addColumn(
                    'id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Message ID'
                )
                ->addColumn(
                    'message_content',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    500,
                    ['nullable' => false],
                    'Content'
                )
                ->addColumn(
                    'is_processed',
                    \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
                    1,
                    ['nullable' => false, 'default' => '0'],
                    'Status'
                )
                ->addColumn(
                    'created_at',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    [],
                    'Created at'
                )
                ->addColumn(
                    'updated_at',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    [],
                    'Updated at'
                )
                ->setComment('Incoming Messages Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');

            $setup->getConnection()->createTable($table);
        }
        $setup->endSetup();
    }
}

app\code\Werules\Chatbot\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Werules_Chatbot" setup_version="1.0.1" schema_version="1.0.1">
    </module>
</config>

What am I doing wrong? Full code can be found here: https://github.com/blopa/Magento-Chatbot/pull/82/files

2

I tried running your InstallSchema.php and it works fine.

I checked my Database and the table was created. If your table is not created maybe because you have run this CLI: (php bin/magento setup:upgrade) before.

I suggest you go to your db and please open the table ‘setup_module’, find and remove a row has module equals to ‘Werules_Chatbot’. After this, run the command again to install the table.

Here's a link to the reference I got https://www.mageplaza.com/magento-2-module-development/how-to-create-crud-model-magento-2.html

  • That was exactly what was happening. Thank you very much :) – Pablo Nov 2 '17 at 22:06

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.