Pimcore:按多种类型检索单个对象列表

时间:2016-05-04 19:55:26

标签: custom-lists pimcore

我目前有一个控制器,它可以从Pimcore对象中提取样本数据的样本。

我们需要通过这个版本来实现的目标是允许一个"精选"要分配给任何NewsArticle对象或EventsArticle对象的类别。我们需要从同时分配了NewsArticle类别的EventsArticleFeatured对象中提取合并列表。我们需要跟踪此列表中返回的所有ID,并将它们从单个轨道列表中排除,以便它们不会在同一页面上显示两次。

这是我们的两个单曲目列表,它们按预期工作,受到文档上的自定义属性的限制。

要求:

  • featured类别过滤。
  • 防止任何帖子被列出两次。
  • 能够按日期排序ascdesc
// TODO: List Featured News and Events Objects...
// $this->view->featured = $featuredList->getObjects();

// List News Objects...
$newsList = new Object\NewsArticle\Listing();
$newsList->setOrderKey( "date" );
$newsList->setOrder( "DESC" );
$newsList->setLimit( $this->document->getProperty( 'newsLimit' ) );
// TODO: Exclude any IDs in $this->view->featured
$this->view->news = $newsList->getObjects();

// List Events Objects...
$eventsList = new Object\EventsArticle\Listing();
$eventsList->setOrderKey( "date" );
$eventsList->setOrder( "DESC" );
$eventsList->setLimit( $this->document->getProperty( 'eventsLimit' ) );
// TODO: Exclude any IDs in $this->view->featured
$this->view->events = $eventsList->getObjects();

2 个答案:

答案 0 :(得分:1)

经过一番努力,我找到了办法。它可能不是最佳的,但它可以工作并完成工作。

我之所以发现这种方式最有效,是因为setCondition还会检查非目标类中可能没有的列,导致列表失败。

#
# Hybridized Featured Articles List
#

# Get News and Events Objects...
$hybridList = new Object\Listing();
$hybridList->setCondition( "o_className IN ( 'newsArticle', 'eventsArticle' )" );

# Get a list of IDs for News and Events that have the "featured" category...
$featuredList = array();
foreach( $hybridList->getObjects() as $obj ) {
    foreach( $obj->categories as $obj_cat ) {
        if( $obj_cat->o_key == 'featured' ) {
            $key = strtotime( $obj->date->date );
            $key .= str_pad( $obj->o_id, 8, "0", STR_PAD_LEFT );
            $featuredList[ $key ] = $obj;
            break;
        }
    }
}

# Sort and Slice the list...
if( $this->document->getProperty( 'featuredSort' ) == 'asc' ) {
    ksort( $featuredList ); // Oldest First
} else {
    krsort( $featuredList ); // Newest First
}
$this->view->featured = array_slice( $featuredList, 0, $this->document->getProperty( 'featuredLimit' ) );

#
# Audit the Hybridized Featured Articles List for IDs
#

$block_ids = array();
foreach( $this->view->featured as $featured ) {
    $block_ids[] = (int)$featured->o_id;
}

#
# News Articles List...
#

$newsList = new Object\NewsArticle\Listing();
$newsList->setOrderKey( "date" );
$newsList->setOrder( $this->document->getProperty( 'newsSort' ) == 'asc' ? 'asc' : 'desc' );
$newsList->setCondition( 'o_id NOT IN ('.implode( ',', $block_ids ).')' );
$newsList->setLimit( $this->document->getProperty( 'newsLimit' ) );
$this->view->news = $newsList->getObjects();

#
# Events Articles List...
#

$eventsList = new Object\EventsArticle\Listing();
$eventsList->setOrderKey( "date" );
$eventsList->setOrder( $this->document->getProperty( 'eventsSort' ) == 'asc' ? 'asc' : 'desc' );
$eventsList->setCondition( 'o_id NOT IN ('.implode( ',', $block_ids ).')' );
$eventsList->setLimit( $this->document->getProperty( 'eventsLimit' ) );
$this->view->events = $eventsList->getObjects();

答案 1 :(得分:1)

这种方法会为您提供精选列表:

$featuredListObj = \Pimcore\Model\Object::getByPath("featured-list");
$featuredListObjId = $featuredListObj->getId();

$eventClassId = \Pimcore\Model\Object\ClassDefinition::getByName("news")->getId();
$newsClassId = \Pimcore\Model\Object\ClassDefinition::getByName("event")->getId();

$combinedListing = new Pimcore\Model\Object\Listing();
$combinedListing->setCondition("o_className IN ('event','news') AND o_id IN (
    SELECT o_id FROM object_$eventClassId WHERE categories LIKE '%,$featuredListObjId,%'
    UNION SELECT o_id FROM object_$newsClassId WHERE categories LIKE '%,$featuredListObjId,%'
)");

foreach ($combinedListing as $item) {
    echo get_class($item) . "<br>";
}