Cakephp请求操作给了我一个永无止境的请求循环


Cakephp request action gives me a never ending request loop

我有一个带有requestAction的元素,但它给了我一个永无止境的请求循环。

例如:

这是我的元素

<?php $plans = $this->requestAction('profile/Plans'); ?>
<?php foreach($plans as $plan): ?>
    <div class="notificationsmall <?php echo $plan['sections']['lable']; ?>">
        <p><b><?php echo $plan['plans']['plan_title']; ?></b></p>
        <p><?php echo $plan['plans']['plan_description']; ?></p>
    </div>  
<?php endforeach;?>

这就是函数。它在"配置文件"控制器中

function Plans($id = Null){ 
    $this->set('plans', $this->Plan->Query('SELECT * FROM plans, sections WHERE plans.user_id='.$id.' AND sections.id=plans.section_id'));
}

我不知道怎么了。

第一段代码是概要文件/计划视图吗?如果是这样,你会得到一个无限循环,因为视图再次调用概要文件/计划操作,回到视图中的同一点,以此类推

根据代码判断,我认为您对元素的工作方式有一些误解。当您需要插入元素时,应该使用requestAction,而不是在元素本身内部。

(我同意Dunhamzzz的观点,即应该避免requestAction,但有时这是不可能的。您应该考虑在这种情况下使用实际元素是否有效。)

问题是您使用的是RequestAction!不惜一切代价避免它。您应该在Plans模型中创建一个方法,该方法基本上返回您正在进行的查询的内容,例如

function getByUserId($userId) {
    return $this->find('all', array('conditions' => array('Plan.user_id' => $userId)));
}

然后在您的操作中,只需执行$this->set('plans', $this->Plan->getByUserId($userId));