cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 Class in Module

SOLVED

Magento 2 Class in Module

Hello

 

Good morning i'm here for help again ( I almost finish promise ), I'm trying to adapt a Prestashop module for Magento 2 but i'm missing some explanation for the class. The module himself have some class to use for example :

 

class myclass extends ObjectModel
{

    public $toreplace;
    public $replaceby;

    public static $definition = array(
        'table' => 'mytable',
        'primary' => 'id',
        'multilang' => false,
        'fields' => array(
            'toreplace' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => '255'),
            'replaceby' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => '255')
        )
    );

    public static function getAll()
    {
        return Db::getInstance()->executeS('SELECT * FROM `' . _DB_PREFIX_ . 'mytable`');
    }
}

but i don't get how i'm suppose to adapt that i think it's with the model but every documentation i found about it only talk about what is it use for or talk aout function construct and other can someone explain me or redirect me to a good post about it pls.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 Class in Module

MyModule/AdminPage/Model/Contact.php

 

 

<?php

namespace MyModule\AdminPage\Model;
use Magento\Framework\Model\AbstractModel;
class Contact extends AbstractModel
{
	/**
     * Define resource model
     */
   protected function _construct()
    {
        $this->_init('MyModule\AdminPage\Model\ResourceModel\Contact');
    }	
	
}

then i have i have MyModule/AdminPage/Model/ResourceModel/Contact.php

 

Make sure id as primary key.

 

Controller file:

 

 $contact = $this->_objectManager->create('MyModule\AdminPage\Model\Contact');
$data =[];
$data['toreplace']="test1";
$data['replaceby']="test1";
$contact->setData($data);
$contact->save()

Make sure var/generation removed and cache flush.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

View solution in original post

6 REPLIES 6

Re: Magento 2 Class in Module

pls help i tried every tutorial but i'm really blocked since 7 hour on it everything i trie don't success i have my basic controller (i will use function calling the class later)

<?php
      namespace Mymodule\AdminPage\Controller\Adminhtml\Nettoyage;

      class Index extends \Magento\Backend\App\Action
      {

        /**
         * Constructor
         *
         * @param \Magento\Backend\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
         */
        public function __construct(
            \Magento\Backend\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        ) {
             parent::__construct($context);
             $this->resultPageFactory = $resultPageFactory;
        }
        
        public function execute()
        {
            return  $resultPage = $this->resultPageFactory->create();
        }
      }
    ?>

i'm searching to connect it to my class just above i think i have to convert it to a Model but i'm really stuck nothing i trie work.

Re: Magento 2 Class in Module

@Antoine553 do you want to save data into db?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento 2 Class in Module

@Antoine553 

 

please check below link to create model and that use.

 

https://www.pierrefay.com/magento2-training/create-magento2-model-database.html


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento 2 Class in Module

ok i tried the link you send me so i have MyModule/AdminPage/Model/Contact.php

<?php

namespace MyModule\AdminPage\Model;

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

/**
 * Contact Model
 *
 * @author      Pierre FAY
 */
class Contact extends AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $_dateTime;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\MyModule\AdminPage\Model\ResourceModel\Contact::class);
    }
    
}

then i have i have MyModule/AdminPage/Model/ResourceModel/Contact.php

<?php
namespace MyModule\AdminPage\Model\ResourceModel;

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

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

then i have i have MyModule/AdminPage/Model/ResourceModel/Contact/Collection.php

<?php

namespace MyModule\AdminPage\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('MyModule\AdminPage\Model\Contact', 'MyModule\AdminPage\Model\ResourceModel\Contact');
    }
}

and finnaly my controller :

<?php
      namespace MyModule\AdminPage\Controller\Adminhtml\Nettoyage;
      
      use Magento\Framework\App\Action\Action;

      class Index extends \Magento\Backend\App\Action
      {
        
        public function execute()
        {
            $contact = $this->_objectManager->create('MyModule\AdminPage\Model\Contact');
	        $contact->setId(2);
	        $contact->setToreplace('test1');
	        $contact->setReplaceby('test2');
	        $contact->save();
	        die('test');
        }
      }
    ?>

 and here is my database

Capture d’écran 2017-07-04 à 16.17.22.png

pls help i'm losing hope i'm only redirect to a blank page with "test" write in the middle but without my new data in the table

Re: Magento 2 Class in Module

MyModule/AdminPage/Model/Contact.php

 

 

<?php

namespace MyModule\AdminPage\Model;
use Magento\Framework\Model\AbstractModel;
class Contact extends AbstractModel
{
	/**
     * Define resource model
     */
   protected function _construct()
    {
        $this->_init('MyModule\AdminPage\Model\ResourceModel\Contact');
    }	
	
}

then i have i have MyModule/AdminPage/Model/ResourceModel/Contact.php

 

Make sure id as primary key.

 

Controller file:

 

 $contact = $this->_objectManager->create('MyModule\AdminPage\Model\Contact');
$data =[];
$data['toreplace']="test1";
$data['replaceby']="test1";
$contact->setData($data);
$contact->save()

Make sure var/generation removed and cache flush.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento 2 Class in Module

After a 50th try i saw a double / at one place and the primary key was "Id" and not "id" that was making everything failed very sorry for having bored you with that it all work now thanks you all.