为什么我的申请中的论坛无效?

时间:2014-09-25 15:10:50

标签: cakephp

我尝试开发一个论坛,但我对函数索引的键有问题,我测试它是否存在,并且在Cakephp发现该论坛不存在但在我的数据库中我有论坛存在。每次当我点击论坛链接时我都会收到“无效论坛”。我想知道原因,我认为模型之间的联系是正确的。有些不对劲,但我不知道在哪里。谢谢

TopicsController:

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('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' => ''
        )
    );

}

模型主题:

<?php
App::uses('AppModel', 'Model');

class Topic 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
            ),
        ),
        'content' => 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
            ),
        ),
        'forum_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'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

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'Forum' => array(
            'className' => 'Forum',
            'foreignKey' => 'forum_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Message' => array(
            'className' => 'Message',
            'foreignKey' => 'topic_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

编辑:

如果我做pr($ this-&gt; Topic-&gt; Forum-&gt; findById($ forumId)),

我明白了:

Array
(
    [Forum] => Array
        (
            [id] => 1
            [name] => General Discussion
            [created] => 2013-12-06
            [modified] => 2013-12-06
        )

    [Message] => Array
        (
            [0] => Array
                (
                    [id] => 13
                    [topic_id] => 5
                    [forum_id] => 1
                    [created] => 2014-09-25
                    [modified] => 2014-09-25
                    [content] => Je répond ^^
                    [user_id] => 98
                )

        )

    [Topic] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [name] => mon topic
                    [content] => c mon topic ^^
                    [created] => 2014-09-25
                    [modified] => 2014-09-25
                    [forum_id] => 1
                    [user_id] => 98
                )

            [1] => Array
                (
                    [id] => 6
                    [name] => a voir
                    [content] => super
                    [created] => 2014-09-10
                    [modified] => 2014-09-19
                    [forum_id] => 1
                    [user_id] => 98
                )

        )

)

查询:

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:

<?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 '&nbsp;';
                                echo $this->Time->timeAgoInWords($message['created']);
                                echo '&nbsp;<small>par</small>&nbsp;';
                                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>

0 个答案:

没有答案