0

I'm trying code

<?php $models = $this->getModel();  ?> ("getModel() is model slideshowdata")
        <?php if($models -> count() > 0 ): ?>
            <?php foreach($models as $model): ?>
                <?php   $objSlideshow = $this->_objectManager->get("Xanka\Slider\Model\Slideshow")->load($model->getId());?>
...

but not true. Any suggestion ? I wana getModel slideshow with condition slideshow_id(slidehoswdata) aqua slidesshow_id(slideshow). so how make it ? I use

<?php   $objSlideshow = $this->_objectManager->get("Xanka\Slider\Model\Slideshow")->load($model->getId());?>

but wrong. thanks

2 Answers 2

3

First add method getCollection() to Block Class

<?php
namespace Xanka\Slider\Block\Adminhtml\SlideshowData;

use \Magento\Backend\Block\Widget\Container;
class Index extends \Magento\Backend\Block\Widget\Container
{   

    protected $_slideshowDataFactory;
     protected $_coreRegistry = null;

     public function __construct(
         \Magento\Backend\Block\Widget\Context $context, 
          \Magento\Framework\Registry $registry,
        \Xanka\Slider\Model\Resource\SlideshowData\CollectionFactory $slideshowDataFactory,     
        array $data =[]     
    )
    {
        $this->_slideshowDataFactory = $slideshowDataFactory;
         $this->_coreRegistry = $registry;
      parent::__construct($context, $data);

    }

     public function getModel(){
         $models = $this->_coreRegistry->registry('slideshowdata_index');
         return $models;
     }
   public function getCollection()
   {
      return $this->_slideshowDataFactory->create();
   }
}

second, use it in template

<?php $models = $block->getCollection(); ?>

<?php if($models->count() > 0 ): ?>
    <?php foreach($models as $model): ?>
      // implement your logic here
    <?php endforeach;?>
<?php endif;?>
6
  • why do you need the _prepareLayout and getModel methods? The rest looks awesome :).
    – Marius
    Commented Sep 29, 2015 at 5:26
  • It's copypasta from previous @xanka question. I just add getCollection method.
    – KAndy
    Commented Sep 29, 2015 at 5:59
  • @xanka. you don't need my help. KAndy already answered your question. it should work.
    – Marius
    Commented Sep 29, 2015 at 6:47
  • Thanks @KAndy but that's not what I want.
    – xanka
    Commented Sep 29, 2015 at 6:51
  • i want get collection B And get collection A by Id of B
    – xanka
    Commented Sep 29, 2015 at 7:13
0

In Block it will be

<?php
namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $_coreRegistry = null;
    protected $_testmodel;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Sugarcode\Test\Model\Test $testmodel,
        array $data = []
    ) {
        $this->_testmodel = $testmodel;
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getAllData()
    {

        //$data = $this->_testmodel->load(1);
        $datas = $this->_testmodel->getCollection();
        return $datas;
    }


}

and in phtml i called

<?php
echo '<pre>';
$datas = $this->getAllData();
foreach($datas as $data){
    print_r($data->getData());
    echo '***************************************************************</br>';
}
?>

its working !!!

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.