How to get CMS block ids between dates?
-
31-12-2020 - |
Pregunta
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
Solución
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.