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.

有帮助吗?

解决方案 4

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

其他提示

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

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

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

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()
许可以下: CC-BY-SA归因
scroll top