0

<?php $price_rules = Mage::getModel('startupready_priceRules/quantitybreaks'); $price_rules->load($priceRuleList); $price_rules->getPricingTables(); ?>

Is there any way to use model like this in magento 2?

Yudi
  • 616
  • 9
  • 27

2 Answers2

1

Let's say your model name is Namespace\Module\Model\SomeModel.
And you want a "translation" of the code in your question to M2, in one of the classes of the M2.

You need to change the constructor of that class and add a new dependency.

$protected $someModelFactory;
public function __construct(
    ....
    \Namespace\Module\Model\SomeModelFactory $someModelFactory, //notice the Factory at the end of the class name
    ....
)
{
    ....
    $this->someModelFactory = $someModelFactory;
    ....
}

Then you can call this anywhere inside a method in the class.

$priceRules = $this->someModelFactory->create();
$priceRules->load($priceRuleList);
$tables = $priceRules->getPricingTables();

You can do this for now, but the load method on the models is deprecated and is going to go away soon: (Deprecated save and load methods in Abstract Model).

Marius
  • 197,939
  • 53
  • 422
  • 830
1
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

//Get Collection 
$collection = $objectManager->create('\Namespace\Module\Model\Module')->getCollection();

// Call function
$customCollection = $objectManager->create('Namespace\Module\Model\Module')->getCustomFunction()); 

You can also initiate objectManger with below way and use it for create model object

protected $objectManager;
public function __construct(

   \Magento\Framework\ObjectManager $objectManager

){

    $this->objectManager= $objectManager;

}
Jaimin Parikh
  • 2,392
  • 2
  • 13
  • 27
  • 4
    No no no...don't use object manager. http://magento.stackexchange.com/a/117103/146. Even if this works, it is wrong. – Marius Sep 16 '16 at 10:01