1

Going forward with my customization I would like to get CMS blocks using a filter:

My code to get the collection:

    $today = Mage::getModel( 'core/date' )->date( "Y-m-d" );
    $collection = Mage::getModel( 'cms/block' )->getCollection()
      ->addFieldToFilter( 'is_slider', true )
      ->addFieldToFilter( 'start_time', array( 'lteq' => $today ))
      ->addFieldToFilter( 'end_time', array( 'gteq' => $today ))
      ->getAllIds()
      ;

is it possible to do it better ?

in further steps I'm looping trough to get blocks identifiers and put into array then return array:

    foreach($collection as $id){
        $slides[] = Mage::getModel('cms/block')->load($id)->getData('identifier');  
    }
    $this->_slides = $slides;
    return $this->_slides;

I would like to add also a sort by "order_slide" so I can decide which slide is first and which one will be last.


Edit:

Final version of the function:

public function getSlideIds()
{
    $today = Mage::getModel( 'core/date' )->date( "Y-m-d" );
    $collection = Mage::getModel( 'cms/block' )->getCollection()
      ->addFieldToSelect('identifier')
      ->addFieldToFilter( 'is_slider', true )
      ->addFieldToFilter( 'start_time', array( 'lteq' => $today ))
      ->addFieldToFilter( 'end_time', array( 'gteq' => $today ))
      ;
    foreach($collection as $id){
        $slides[] = $id->getData('identifier'); 
    }
    $this->_slides = $slides;
    return $this->_slides;
}

works properly. Thank you Douglas. Actually I should change now function name to getSlideIdentifiers() :P

5
  • I assume that is_slider, start_time and end_time are all custom attributes you've added to cms blocks? I'm not sure what you're trying to do with the code? Commented Feb 22, 2018 at 16:45
  • Douglas@ as the theme does not have this functionality and banners/sliders can not be controlled by date, I modified code, added custom attributes to cms_block table and now I can decide which one and for how long should be displayed. So this code actually select only those which fulfill criteria, then banners/slider will be displayed instead of a manually created list of cms_block_ids. My question was "how can I do it better/shorter"?
    – Rob D. A.
    Commented Feb 22, 2018 at 16:51
  • 1
    I don't see anything wrong with your current implementation. What does your code look like after this? Do you then loop through all IDs and load CMS blocks again? (which could be improved) Commented Feb 22, 2018 at 16:52
  • Updated my code, take a look please :P
    – Rob D. A.
    Commented Feb 22, 2018 at 16:56
  • You have accepted Dougla anwser ... so there is no need to add it to your question. Could be misleading to others :)
    – sv3n
    Commented Feb 22, 2018 at 22:12

1 Answer 1

1

With your updated code snippet, you could shorten this to:

$today = Mage::getModel( 'core/date' )->date( "Y-m-d" );
$collection = Mage::getModel( 'cms/block' )->getCollection()
  ->addFieldToSelect('identifier')
  ->addFieldToFilter('is_slider', array('eq' => '1'))
  ->addFieldToFilter('start_time', array( 'lteq' => $today))
  ->addFieldToFilter('end_time', array( 'gteq' => $today));

foreach($collection as $cmsBlock){
    $slides[] = $cmsBlock->getData('identifier');
}

$this->_slides = $slides;
return $this->_slides;

This would reduce the overhead of making many DB calls loading the CMS_BLOCK model repeatedly.

If you adapted the function that receives this output, you could probably return $collection instead.

8
  • Unfortunately .... Fatal error: Call to a member function getData() on array in .../Block/Slideshow.php on line 76 ... oops i think I found why :P ..... nope, it does not work this way ;(
    – Rob D. A.
    Commented Feb 22, 2018 at 17:10
  • Sorry, had a typo, have updated my example Commented Feb 22, 2018 at 21:11
  • Hi Douglas, a typo ? where was this typo ? I haven't seen it.
    – Rob D. A.
    Commented Feb 22, 2018 at 21:14
  • I called $collection->getData() instead of $cmsBlock->getData() Commented Feb 22, 2018 at 21:33
  • I'm sorry Douglass, tried this way, getting error so it seems something is wrong. Fatal error: Call to a member function getData() on string in .../Slideshow.php on line 78 same as before
    – Rob D. A.
    Commented Feb 22, 2018 at 21:40

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.