3

In Magento 1.9 i used this code to get the database table values.

$model = Mage::getModel('task_creation/details');

   $collections = $model->getCollection();

foreach($collections as $collection)
{
  print_r($collection->getdata(''));
}

Now I am using Magento 2.1, what's the code I need to use for it. I am new to Magento 2.

5 Answers 5

4

For that you first create the Model So in my case Module name is News

app/code/FME/News/Model/News.php

<?php
namespace FME\News\Model;

class News extends \Magento\Framework\Model\AbstractModel implements NewsInterface, \Magento\Framework\DataObject\IdentityInterface
{
    const CACHE_TAG = 'id';
    const ENABLED = 1;
    const DISABLED = 0;
    const COMPLETE = 0;
    const LEFT = 1;
    const RIGHT = 2;


    protected function _construct()
    {
        $this->_init('FME\News\Model\ResourceModel\News');
    }

    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->getId()];
    }



}

Then create interface for model app/code/FME/News/Model/NewsInterface.php

<?php
namespace FME\News\Model;
interface NewsInterface 
{

}

then create resource model

app/code/FME/News/Model/ResourceModel/News.php

 <?php
namespace FME\News\Model\ResourceModel;

class News extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected function _construct()
    {
        $this->_init('fme_news','id');
    }

}

Then create collection app/code/FME/News/Model/ResourceModel/News/Collection.php

<?php
namespace FME\News\Model\ResourceModel\News;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected $_idFieldName = 'id';
    protected function _construct()
    {
        $this->_init('FME\News\Model\News','FME\News\Model\ResourceModel\News');
    }
}

Now get the collection

protected $newscollectionFactory;
    public function __construct(....
\FME\News\Model\ResourceModel\News\CollectionFactory $newscollectionFactory
    ....)
    {
        ....
        $this->newscollectionFactory = $newscollectionFactory;
            .....
    }
    public function getNews()
    {


        $newsCollection = $this->newscollectionFactory->create();
        return $newsCollection;
    }
2

In Magento 2 If you have created your model correctly, your Magento will generate factory method for it.

So use factory methode like this:

private $_taskCollectionFactory;

public function __construct(
    ...
    \Task\Creation\Model\ResourceModel\details\CollectionFactory $collectionFactory,
    ...
) {
    $this->_taskCollectionFactory = $collectionFactory;
   ...
}

public function getTaskCollection(){

    $collection = $this->_taskCollectionFactory->create();
    $collection->addAttributeToSelect('*')

    return $collection;
}
3
  • 1
    Which is the purpose of $om = \Magento\Framework\App\ObjectManager::getInstance();? Magento strictly discouraged to use Object Manager directly. Commented Nov 11, 2016 at 6:50
  • 1
    remove this code you are not using it. $om = \Magento\Framework\App\ObjectManager::getInstance(); Commented Nov 11, 2016 at 6:51
  • 1
    i have removed it. missed to remove, anyways Thanks. Commented Nov 11, 2016 at 6:52
2

In Magento2 you can create file on your root and past below code in it.

$hostname = "yourhost";
$username = "youruser";
$password = "yourpwd";
$database="yourdb";
ini_set('display_errors', 1);
$connection = mysqli_connect($hostname, $username, $password, $database);
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
require 'app/bootstrap.php';
$opt['group'] = 'default';
$opt['standaloneProcessStarted'] = '0';
$params = $_SERVER;
/* you can out your store id here */
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
$params[Store::CUSTOM_ENTRY_POINT_PARAM] = true;

/* create application */
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
/* the applocation 
/** @var \Magento\Framework\App\Cron $app */

$app = $bootstrap->createApplication('Magento\Framework\App\Cron', ['parameters' => $opt]);
//$bootstrap->run($app);

$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$appState = $objectManagerr->get("Magento\Framework\App\State");
$appState->setAreaCode("global");
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categoryy = $categoryFactory->create()
            ->addAttributeToSelect('*');
$category = $objectManagerr->get('Magento\Catalog\Model\CategoryFactory')->create()->getCollection()
        ->addAttributeToFilter('url_key','werbeartikel-kategorien')
        ->addFieldToSelect('name')
        ->getFirstItem();
0
1

For me following code working fine guys:

<?php

namespace Inchoo\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Inchoo\Helloworld\Model\PostFactory;

class View extends Action
{
    /**
     * @var \Tutorial\SimpleNews\Model\NewsFactory
     */
    protected $_modelPostFactory;

    /**
     * @param Context $context
     * @param NewsFactory $modelNewsFactory
     */
    public function __construct(
        Context $context,
        PostFactory $modelPostFactory
    ) {
        parent::__construct($context);
        $this->_modelPostFactory = $modelPostFactory;
    }

    public function execute()
    {
        /**
         * When Magento get your model, it will generate a Factory class
         * for your model at var/generaton folder and we can get your
         * model by this way
         */
        $postModel = $this->_modelPostFactory->create();

        // Load the item with ID is 1
        $item = $postModel->load(1);
        var_dump($item->getData());

        // Get news collection
        $postCollection = $postModel->getCollection();
        // Load all data of collection
        var_dump($postCollection->getData());
    }
}
0

You can create any model object with the help of Object Manager object and do whatever you want like:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$collection = $objectManager->get('VendorName\ModuleName\Model\ClassName')
            ->getCollection()
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.