Skip to content

Instantly share code, notes, and snippets.

@Dan0sz
Last active October 21, 2018 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dan0sz/04cd90b4725c2453e127098f20d3a18c to your computer and use it in GitHub Desktop.
Save Dan0sz/04cd90b4725c2453e127098f20d3a18c to your computer and use it in GitHub Desktop.
How to Write Data to Custom Database Table and Create a Custom Collection in Magento 2
<?php
namespace DaanvdB\ProductQty\Block;
use Magento\Framework\View\Element\Template;
use DaanvdB\ProductQty\Model\DataFactory;
class Data extends Template {
protected $dataFactory;
public function __construct(
Template\Context $context,
DataFactory $dataFactory
) {
$this->dataFactory = $dataFactory;
parent::__construct(
$context
);
}
public function showData() {
$collection = $this->dataFactory->create()->getCollection();
$data = $collection->getData();
return $data;
}
}
<?php
namespace DaanvdB\ProductQty\Model;
use Magento\Framework\Model\AbstractModel;
class Data extends AbstractModel {
public function _construct() {
$this->_init( "\DaanvdB\ProductQty\Model\ResourceModel\Data" );
}
}
<?php
namespace DaanvdB\ProductQty\Model;
use DaanvdB\ProductQty\Model\ResourceModel\Data;
class Import {
protected $resourceData;
public function __construct(
Data $resourceData
) {
$this->resourceData = $resourceData;
}
public function importData() {
$connection = $this->resourceData->getConnection();
$data = [
'test_sku_1' => 10,
'test_sku_2' => 20,
'test_sku_3' => 15
];
try {
$connection->beginTransaction();
$connection->insertMultiple( $this->resourceData->getTable( 'daanvdb_product_qty' ), $data );
$connection->commit();
} catch ( \Exception $e ) {
return $e;
}
return true;
}
}
<?php
namespace DaanvdB\ProductQty\Model\ResourceModel\Data;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
class Collection extends AbstractCollection {
public function _construct() {
$this->_init( "DaanvdB\ProductQty\Model\Data", "DaanvdB\ProductQty\Model\ResourceModel\Data" );
}
}
<?php
namespace DaanvdB\ProductQty\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Data extends AbstractDb
{
public function _construct()
{
$this->_init('daanvdb_product_qty', 'sku');
}
}
<?php
namespace DaanvdB\ProductQty\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeSchema implements UpgradeSchemaInterface {
public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) {
$setup->startSetup();
// Create table and columns.
$table = $setup->getConnection()->newTable(
$setup->getTable( 'daanvdb_product_qty' )
)->addColumn(
'sku',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
[
'nullable' => false,
'primary' => false
],
'Product SKU'
)->addColumn(
'qty',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
[
'nullable' => false,
'primary' => false
]
);
// UPDATE OCT 4TH, 2018: And if you want to add a Foreign Key Constraint
$setup->addForeignKey(
$setup->getFkName( 'daanvdb_product_qty', 'quote_id', 'other_table', 'other_column' ),
'quote_id',
$setup->getTable( 'other_table' ),
'other_column',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE //onDelete
);
$setup->getConnection()->createTable( $table );
// And if you want to add a column to an existing table...
if ( version_compare( $context->getVersion(), '2.0' < 0 ) ) {
$table = $setup->getTable( 'daanvdb_product_qty' );
$setup->getConnection()->addColumn(
$table,
'custom_column',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
'nullable' => false,
'comment' => 'Custom Column'
]
);
}
$setup->endSetup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment