如何将相关对象的数据提供给我的视图? CakePHP的

时间:2017-03-28 13:18:25

标签: cakephp

我试图将文字添加到我的视图中,但这似乎没有成功。

在我的WepagesController.php中,我有一个关于aboutus.ctp的函数:

        public function aboutus() {

        // load the website base-layout
        $this -> layout = 'website';

        // load relevant models
        $this -> loadModel('Contentitem');


    //query for 2 contentitems
    $aboutusinfos = $this->Contentitem->find('first', array('conditions' => array('contentitem_slug' => 'lorum')));
    debug($aboutusinfos);
    $aboutuspersons = $this->Contentitem->find('first', array('conditions' => array('contentitem_slug' => 'Robotics')));
    debug($aboutuspersons);
   //set the 2 contentitems     
    $this->set('aboutusinfos', $this->Contentitem->find('first', array('conditions' => array('contentitem_slug' => 'lorum'))));
    $this->set('aboutuspersons', $this->Contentitem->find('first', array('conditions' => array('contentitem_slug' => 'Robotics'))));


    }

这为我提供了正确的contentitems输出:

array(
    'Contentitem' => array(
        'contentitem_id' => '4',
        'contentitem_slug' => 'lorum',
        'contentitem_note' => ''
    ),
    'Contenttext' => array(
        (int) 0 => array(
            'contenttext_id' => '4',
            'contenttext_text' => '<p><span style="color: #555555; font-family: Verdana, Arial, sans-serif; font-size: 12.8px; text-align: justify;">A butt load of text</span></p>',
            'contenttext_rel_contentitem' => '4',
            'contenttext_rel_language' => '2'
        )
    )
)

array(
    'Contentitem' => array(
        'contentitem_id' => '3',
        'contentitem_slug' => 'Robotics',
        'contentitem_note' => ''
    ),
    'Contenttext' => array(
        (int) 0 => array(
            'contenttext_id' => '3',
            'contenttext_text' => '<p><span style="color: #555555; font-family: Verdana, Arial, sans-serif; font-size: 12.8px; text-align: justify;">A butt load of text</span></p>',
            'contenttext_rel_contentitem' => '3',
            'contenttext_rel_language' => '2'
        )
    )
)

我现在的问题是我无法在about.ctp中请求contenttext_text,如下所示:

<div class="column_2">
<?php
echo $aboutusinfos['Contenttext']['contenttext_text'];

?>
</div>

只要我想从$aboutusinfos['Contentitem']获取任何内容,而不是从相关表中获取任何内容,它就能正常工作。 有人有想法吗?

1 个答案:

答案 0 :(得分:0)

echo $aboutusinfos['Contenttext'][0]['contenttext_text'];

应该做的伎俩