首先,谢谢你的期待。我在学习CakePHP并设置应用程序。我遇到了一个我似乎不明白如何解决的问题。
我有一个带有以下功能的文章控制器:
function getRelated(){
$relatedarticles = $this->Article->find(
'all',
array(
'fields' => array(
'Article.title'
),
'limit' => 4,
'order' => 'Article.id ASC',
'recursive' => 1,
'conditions' => array(
'Article.category_id =' => '1'
)
)
);
if (!empty($this->params['requested'])) {
return $relatedarticles;
} else {
$this->set(compact('relatedarticles'));
}
}
我还设置了一个包含以下代码的元素,我可以在整个应用程序中使用该代码:
<div id="articles_view_related">
Related News
<?php
$relatedarticles = $this->requestAction('/articles/getRelated');
//debug($relatedarticles);
?>
<ul>
<?php
foreach($relatedarticles as $relatedarticle){
?>
<li>
<?php
echo $relatedarticle['Article']['title'];
?>
</li>
<?php
}
?>
</ul>
</div>
从代码中可以看出,我将'limit'设置为4,'category_id'设置为1.基本上,我要做的就是让CakePHP返回4个文章标题,其category_id为1。
我的问题:它只返回数据库中的第一条记录,其中包含请求的category_id但是4次。
基本上:
它返回:
当我需要它返回这样的东西时:
拜托,如果有人能对此有所了解,我当然会感激。我花了好几个小时试图解决这个问题。
非常感谢,
Andre S。
更新CAKEPHP调试输出:
Array
(
[0] => Array
(
[Article] => Array
(
[title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
[id] => 1
)
[Attachment] => Array
(
)
[Comment] => Array
(
)
)
[1] => Array
(
[Article] => Array
(
[title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
[id] => 1
)
[Attachment] => Array
(
)
[Comment] => Array
(
)
)
[2] => Array
(
[Article] => Array
(
[title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
[id] => 1
)
[Attachment] => Array
(
)
[Comment] => Array
(
)
)
[3] => Array
(
[Article] => Array
(
[title] => Djabraba recognized as the most underdeveloped island in the world here with us djabraba is here
[id] => 1
)
[Attachment] => Array
(
)
[Comment] => Array
(
)
)
)
答案 0 :(得分:0)
问题是我试图理解的一些不良关系:
我在articles.php模型文件中有以下关系:
var $belongsTo = array(
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
我删除了用户关系,现在它对此有用,但会导致需要用户关系的问题..有谁知道为什么?
<已解决] [已解决] - 见下面的评论......