You have mentioned a tutorial that explains CRUD operation without a model, but it is not good practice using object manager.
let me explain the crud operation with the model, it is very simple. This is a very important concept in Magento.
For example, Let's consider the below table structure

For CRUD operation you need to create three things
1. Model
In Model, you need to initialize the resource model
app/code/<vendor>/<module>/Model/Example.php
<?php
namespace <vendor>\<module>\Model;
use Magento\Framework\Model\AbstractModel;
class Example extends AbstractModel
{
/**
* Define resource model
*/
protected function _construct()
{
$this->_init('<vendor>\<module>\Model\ResourceModel\Example');
}
}
2. Resource Model
In the Resource model, you need to initialize the table name and primary key.
app/code/<vendor>/<module>/Model/ResourceModel/Example.php
<?php
namespace <vendor>\<module>\Model\Resource;
class Example extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Define the main table
*/
protected function _construct()
{
$this->_init('custom_table_name', 'id'); //here id is the primary key of custom table
}
}
3. Collection
In the collection, you need to define the Model and Resource Model.
app/code/<vendor>/<module>/Model/ResourceModel/Example/Collection.php
<?php
namespace <vendor>\<module>\Model\ResourceModel\Example;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Define model & resource model
*/
protected function _construct()
{
$this->_init(
'<vendor>\<module>\Model\Example',
'<vendor>\<module>\Model\ResourceModel\Example'
);
}
}
That's it, now you can able to fetch table values in any block using dependency injection.
For eg:
Get and Set Data in Block
Kindly note, we pass <module>\<vendor>\Model\ExampleFactory
in the constructor, but none of the files will be found in that location
In Magento 2, each CRUD model has a corresponding factory class. All factory class names are the name of the model class appended with the word “Factory”.Since our model class is named
<module>/<vendor>/Model/Example
this means our factory class is named <module>/<vendor>/Model/ExampleFactory
………………….
………………….
protected $_exampleFactory;
public function __construct( <vendor>\<module>\Model\ExampleFactory $db)
{
$this->_exampleFactory = $db;
}
public function anyMethodYouWant()
{
//set value
$this->_exampleFactory->create()->setData(array('event_name' => 'xyz', 'event_imgurl' => 'xyz',...............))->save();
//get value
$data=$this->_exampleFactory->create()->getCollection();
foreach ($data as $d )
{
echo $d->getEventImgurl(); //table field event_imgurl
echo $d->getEventName(); //table field event_name
}
}
after that, you can call this block method in your template file
<?php $block->anyMethodYouWant(); ?>