Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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();

Hope it will help.

share|improve this answer
    
Thanks worked for me !!! – Asim Munshi Nov 11 at 7:20

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;
    }
share|improve this answer

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;
}
share|improve this answer
1  
Which is the purpose of $om = \Magento\Framework\App\ObjectManager::getInstance();? Magento strictly discouraged to use Object Manager directly. – Khoa TruongDinh Nov 11 at 6:50
1  
remove this code you are not using it. $om = \Magento\Framework\App\ObjectManager::getInstance(); – Qaisar Satti Nov 11 at 6:51
1  
i have removed it. missed to remove, anyways Thanks. – Ronak Chauhan Nov 11 at 6:52

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()
share|improve this answer
    
Magento strictly discouraged to use Object Manager directly so avoid that. magento.stackexchange.com/questions/117098/… – Qaisar Satti Nov 11 at 7:10
up vote 0 down vote accepted

For me following code working fine guys:

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());
}

}

Thanks for your support.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.