1) SQL (optional) — create table manually
CREATE TABLE `vendor_for_you_item` (
`entity_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`description` TEXT NULL,
`is_active` SMALLINT(1) NOT NULL DEFAULT 1,
`created_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`entity_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
2) Module registration
File: app/code/Vendor/ForYou/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_ForYou',
__DIR__
);
3) module.xml
File: app/code/Vendor/ForYou/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="Vendor_ForYou" setup_version="1.0.0"/>
</config>
4) db_schema.xml (Magento 2 recommended)
File: app/code/Vendor/ForYou/etc/db_schema.xml
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="vendor_for_you_item" resource="default" engine="innodb" comment="Vendor ForYou items">
<column xsi:type="int" name="entity_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
<column xsi:type="varchar" name="title" nullable="false" length="255" comment="Title"/>
<column xsi:type="text" name="description" nullable="true" comment="Description"/>
<column xsi:type="smallint" name="is_active" nullable="false" default="1" comment="Is Active"/>
<column xsi:type="timestamp" name="created_at" nullable="true" on_update="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
<column xsi:type="timestamp" name="updated_at" nullable="true" on_update="true" comment="Updated At"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="entity_id"/>
</constraint>
</table>
</schema>
5) Model
File: app/code/Vendor/ForYou/Model/ForYou.php
<?php
namespace Vendor\ForYou\Model;
use Magento\Framework\Model\AbstractModel;
class ForYou extends AbstractModel
{
protected function _construct()
{
$this->_init(\Vendor\ForYou\Model\ResourceModel\ForYou::class);
}
}
6) ResourceModel
File: app/code/Vendor/ForYou/Model/ResourceModel/ForYou.php
<?php
namespace Vendor\ForYou\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class ForYou extends AbstractDb
{
protected function _construct()
{
$this->_init('vendor_for_you_item', 'entity_id');
}
}
7) Collection
File: app/code/Vendor/ForYou/Model/ResourceModel/ForYou/Collection.php
<?php
namespace Vendor\ForYou\Model\ResourceModel\ForYou;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Vendor\ForYou\Model\ForYou as Model;
use Vendor\ForYou\Model\ResourceModel\ForYou as ResourceModel;
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init(Model::class, ResourceModel::class);
}
}
8) Example adminhtml Controller to create record (usage)
File: app/code/Vendor/ForYou/Controller/Adminhtml/Item/Save.php
<?php
namespace Vendor\ForYou\Controller\Adminhtml\Item;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Vendor\ForYou\Model\ForYouFactory;
class Save extends Action
{
protected $forYouFactory;
public function __construct(Context $context, ForYouFactory $forYouFactory)
{
parent::__construct($context);
$this->forYouFactory = $forYouFactory;
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
if (!$data) {
$this->_redirect('*/*/');
return;
}
try {
$model = $this->forYouFactory->create();
$model->setData($data);
$model->save();
$this->messageManager->addSuccessMessage(__('Saved.'));
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
$this->_redirect('*/*/');
}
}
9) di.xml (optional) — no mapping required for model factory but shown for example
File: app/code/Vendor/ForYou/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Vendor\ForYou\Model\ForYou">
<arguments><!-- example: additional constructor args if needed --> </arguments>
</type>
</config>
10) ACL, menu and admin routes (minimal stubs)
Admin route file: app/code/Vendor/ForYou/etc/adminhtml/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="foryou" frontName="foryou">
<module name="Vendor_ForYou"/>
</route>
</router>
</config>
ACL stub: app/code/Vendor/ForYou/etc/acl.xml
<?xml version="1.0"?>
<acl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<resources>
<resource id="Magento_Backend::admin">
<resource id="Vendor_ForYou::manage" title="Manage ForYou" />
</resource>
</resources>
</acl>
11) composer.json (optional)
File: app/code/Vendor/ForYou/composer.json
{
"name": "vendor/module-foryou",
"description": "Vendor ForYou module",
"type": "magento2-module",
"version": "1.0.0",
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Vendor\\ForYou\\": ""
}
}
}
12) Post-install commands
php bin/magento module:enable Vendor_ForYou
php bin/magento setup:upgrade
php bin/magento cache:flush
# If in production:
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
13) Quick usage examples (PHP snippets)
Get a model by ID:
$forYou = $this->forYouFactory->create()->load($id);
echo $forYou->getTitle();
Create new record:
$model = $this->forYouFactory->create();
$model->setTitle('Hello');
$model->setDescription('Sample');
$model->save();
Get collection:
$collection = $this->forYouCollectionFactory->create();
$collection->addFieldToFilter('is_active', 1);
foreach ($collection as $item) {
echo $item->getTitle();
}
Notes
- Place files under app/code/Vendor/ForYou/ preserving paths shown.
- Use db_schema.xml for Magento 2.3+; it manages schema via setup:upgrade.
- If you prefer declarative schema not to run on dev, you can run the SQL manually.
- Factories and repositories are generated. Run setup:di:compile when needed.