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?

2 Answers 2

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;

}
1
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).

Your Answer

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

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