Cakephp 为什么我的应用程序中会出现无效论坛?
我试图开发一个论坛,但我对函数索引的键有问题,我测试它是否存在,Cakephp发现这个论坛不存在,但在我的数据库中我有一个存在的论坛。每次我点击有问题的论坛链接时,我都会得到无效的论坛。我想知道为什么,我认为模型之间的连接是正确的。有点不对劲,但我不知道在哪里。多谢各位 主题控制器:Cakephp 为什么我的应用程序中会出现无效论坛?,cakephp,Cakephp,我试图开发一个论坛,但我对函数索引的键有问题,我测试它是否存在,Cakephp发现这个论坛不存在,但在我的数据库中我有一个存在的论坛。每次我点击有问题的论坛链接时,我都会得到无效的论坛。我想知道为什么,我认为模型之间的连接是正确的。有点不对劲,但我不知道在哪里。多谢各位 主题控制器: public function index($forumId=null) { //debug($forumId); if (!$this->Topic->Forum-&g
public function index($forumId=null) {
//debug($forumId);
if (!$this->Topic->Forum->exists($forumId)) {
throw new NotFoundException(__('Invalid forum'));
}
$forum = $this->Topic->Forum->read(null,$forumId);
$this->set('forum',$forum);
$this->Paginator->settings['contain'] = array('User','Message'=>array('User'));
$this->set('topics', $this->Paginator->paginate());
}
<?php
App::uses('AppController', 'Controller');
class ForumsController extends AppController {
public $components = array('Paginator');
public function beforeFilter() {
$this->Auth->allow();
}
public function index() {
$this->Paginator->settings['contain'] = array('Topic', 'Message'=>array('User','Topic'));
$this->set('forums', $this->Paginator->paginate());
}
}
示范论坛:
<?php
App::uses('AppModel', 'Model');
class Forum extends AppModel {
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
public $hasMany = array(
'Message' => array(
'className' => 'Message',
'foreignKey' => 'forum_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'forum_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
以及查询:
Page Introuvable Invalid forum
La page que vous avez essayez d'atteindre n'existe pas
(default) 4 queries took 0 ms Nr Query Error Affected Num. rows Took (ms)
1 SELECT `Forum`.`id`, `Forum`.`name`, `Forum`.`created`, `Forum`.`modified` FROM `forums` AS `Forum` WHERE `Forum`.`id` = 1 LIMIT 1 1 1 0
2 SELECT `Message`.`id`, `Message`.`topic_id`, `Message`.`forum_id`, `Message`.`created`, `Message`.`modified`, `Message`.`content`, `Message`.`user_id` FROM `messages` AS `Message` WHERE `Message`.`forum_id` = (1) 1 1 0
3 SELECT `Topic`.`id`, `Topic`.`name`, `Topic`.`content`, `Topic`.`created`, `Topic`.`modified`, `Topic`.`forum_id`, `Topic`.`user_id` FROM `topics` AS `Topic` WHERE `Topic`.`forum_id` = (1) 2 2 0
4 SELECT `Post`.`id`, `Post`.`slug`, `Post`.`name`, `Post`.`type` FROM `posts` AS `Post` WHERE `type` = 'page' AND `online` = 1 ORDER BY `Post`.`created` DESC 1 1 0
ForumsController:
public function index($forumId=null) {
//debug($forumId);
if (!$this->Topic->Forum->exists($forumId)) {
throw new NotFoundException(__('Invalid forum'));
}
$forum = $this->Topic->Forum->read(null,$forumId);
$this->set('forum',$forum);
$this->Paginator->settings['contain'] = array('User','Message'=>array('User'));
$this->set('topics', $this->Paginator->paginate());
}
<?php
App::uses('AppController', 'Controller');
class ForumsController extends AppController {
public $components = array('Paginator');
public function beforeFilter() {
$this->Auth->allow();
}
public function index() {
$this->Paginator->settings['contain'] = array('Topic', 'Message'=>array('User','Topic'));
$this->set('forums', $this->Paginator->paginate());
}
}
ForumsController的功能索引,我可以在其中看到我创建的论坛,并可以单击链接:
<div class="row">
<div class="col-lg-12 ">
<table class="table table-bordered">
<thead>
<tr>
<th colspan=2>Forum</th>
<th>Topics</th>
<th>Messages</th>
<th>Activity</th>
</tr>
</thead>
<tbody>
<?php foreach ($forums as $forum): ?>
<tr>
<td><?php echo $forum['Forum']['id'];?></td>
<td>
<?php
echo $this->Html->link('<h4>'.$forum['Forum']['name'].'</h4>',
array('controller'=>'topics','action'=>'index',$forum['Forum']['id']),
array('escape'=>false));
?>
</td>
<td><?php echo count($forum['Topic']);?></td>
<td><?php echo count($forum['Message']);?></td>
<td>
<?php
if(count($forum['Message'])>0) {
$message = $forum['Message'][0];
echo $this->Html->link($message['Topic']['name'],array('controller'=>'topics',
'action'=>'view',
$message['Topic']['id']));
echo ' ';
echo $this->Time->timeAgoInWords($message['created']);
echo ' <small>par</small> ';
echo $this->Html->link($message['User']['username'],array('controller'=>'users',
'action'=>'profile',
$message['User']['id']));
}
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="pull-right">
<?php
echo $this->element('paginator');
?>
</div>
</div>
</div>
在操作的第一行执行pr$this->Topic->Forum->findById$forumId,而不是$debug$forumId,并向我们展示它的输出。同时给出一个例子,说明什么是$forumId。你能把你的问题格式化一点,更好的标点和语法吗?对不起,我来自法国:/i添加了pr$this->Topic->Forum->findById$forumId,正如你所看到的,我应该得到两个论坛id为1的主题。但我得到这个消息无效论坛!谢谢你的帮助!我编辑了我的帖子