CakePHP - 如何将$变量传递给控制器​​中的find函数

时间:2011-05-16 13:05:14

标签: cakephp

所有

我想知道如何从视图传递$变量,例如,当前文章ID($ article ['Article'] ['id'])到函数中的find('all')调用articles_controller.php。

function getRelated($id = null){
$relatedarticles = $this->Article->find(
                    'all',
                    array(
                         'fields' => array(
                        'Article.title'
                        ),
                         'limit' => 4,
                         'order' => 'Article.id ASC',
                         'recursive' => 1,
                         'conditions' => array(
                        'Article.category_id =' => $id
                         )
                         )
                    );

if (!empty($this->params['requested'])) {
    return $relatedarticles;
} else {
    $this->set(compact('relatedarticles'));
}
}

然后从视角来看,我会打电话给这样的事情。我似乎无法让这个工作......最好的办法是什么?

<div id="articles_view_related">
Related News
<?php 
    $currentid = $articles['Article']['id'];
    $relatedarticles = $this->requestAction('/articles/getRelated/id:$currentid');
    //debug($relatedarticles);
?>
<ul>
    <?php
        foreach($relatedarticles as $relatedarticle){
    ?>
    <li>
        <?php
            echo $relatedarticle['Article']['title'];
        ?>
    </li>
    <?php
        }
    ?>
</ul>

</div>

谢谢你的帮助

1 个答案:

答案 0 :(得分:2)