2

I am finding the way to call my custom extension's model inside my phtml file. and i am using the following code to get the model inside my phtml file.

<?php $price_rules=$objectManager->getModel('Startupready_Myrules/Quantitybreaks'); $price_rules->load($priceRuleList); ?>

But after calling like this. I found that no css of js are applying in the front-end. Is there any way to call my custom created extension's model? Thanks in advance.

4
  • Do the process in block related to your phtml, then return the result and use in the template. Not recommended to use object manager. Is there any reason for processing the data in phtml ? Commented Sep 17, 2016 at 7:18
  • can you please be more elaborate your answer...I am not getting you..also i would be more helpful if you can use the code to tell me what is the mean...thanks for your reply..
    – Yudi
    Commented Sep 17, 2016 at 7:22
  • and the reason to process the data is that i have to show some data in list page which is related to products.. So i created my custom extension by the help of that i can show the required data. but the problem is that i am not understanding the phenomena of how do i call that extension in my list page..?
    – Yudi
    Commented Sep 17, 2016 at 7:31
  • "I found that no css of js are applying in the front-end" it's because you've got a PHP error. Check your logs. Commented Oct 21, 2016 at 8:56

2 Answers 2

1

Assume you have a custom block (Startupready/Myrules/Block) for your template. Inside the block call the model and render the data like getPosts() in the below example, in your case $price_rules->load($priceRuleList); and return the result.

In the templates call $result = $block->load();

Then process the data.

I have placed some sample example code how to call, by using this approach you can process your code.

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Ashsmith\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->_postCollectionFactory = $postCollectionFactory;
}

/**
 * @return \Ashsmith\Blog\Model\ResourceModel\Post\Collection
 */
public function getPosts()
{
    // Check if posts has already been defined
    // makes our block nice and re-usable! We could
    // pass the 'posts' data to this block, with a collection
    // that has been filtered differently!
    if (!$this->hasData('posts')) {
        $posts = $this->_postCollectionFactory
            ->create()
            ->addFilter('is_active', 1)
            ->addOrder(
                PostInterface::CREATION_TIME,
                PostCollection::SORT_ORDER_DESC
            );
        $this->setData('posts', $posts);
    }
    return $this->getData('posts');
}
8
  • Thanks for your reply. As you told me that with the help of <?php $result = $block->load(); ?> , i can render the data. But my dear friend how do i give the path of my extension before <?php ...->load(); ?>. Because if i put like as you told me. then it wouldn't get the path of my extension. my extension is in app/code/Startupready folder.
    – Yudi
    Commented Sep 17, 2016 at 7:53
  • I totally confused, please provide the clear code with your intention in the question ..........:) Commented Sep 17, 2016 at 8:00
  • public function load($categories = false) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $rules = $objectManager->create('Magento\Salesrule\Rule\Collection')->load(); foreach($rules as $rule){ ................ my custom code which returns the data................ } } this is a file inside my custom extension form where i am getting my data which i want to show. and as you know that after install the extension its location is inside app/code/myextension. now i am want to ask you that how can i render this data and show inside the list.phtml file.
    – Yudi
    Commented Sep 17, 2016 at 8:03
  • I mean what you are trying to do? Commented Sep 17, 2016 at 8:05
  • 1
    as like magento 1 i was using this $price_rules = Mage::getModel('startupready_Myrules/quantitybreaks'); $price_rules->load($priceRuleList); to render the data form extension. now is there any way to get data like this in magento 2?
    – Yudi
    Commented Sep 17, 2016 at 8:08
1

You can use ObjectManager to call method of any model. just for an example-

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $price_rules = $objectManager->get('CustomModel\namespace\Model\Quantitybreaks'); $price_rules->load($price_rule_category); ?>

But try to use dependency injection instead of this way, However this is also a quick way to do it.

0

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.