0

抱歉,如果这个问题在某个地方得到了回答,我试图找到它但无济于事

Cakephp 通常是我的朋友,但它现在让我发疯。我正在尝试创建选项,以便在存在许多其他条目的同一页面上对条目进行投票。我已经在 Cake 1.3 中做了一百万次

问题: 如果我在一个页面上有 10 个条目(包装在唯一的 div 中),我只能“评分”第一个条目,即使控制器 (FireBug) 中正确接收了 entry_id 并且调试显示 $this->request- >数据是正确的。尽管如此,Cake 只保存页面上第一个条目的“分数”,并且在单击链接后不会显示其他条目的分数的任何错误消息。

UpdateALL 操作对所有条目都正确,只有 SAVE 操作失败。

问题:如果我单击 + 链接,为什么 Cake 不保存页面上所有条目的分数。同样,不会抛出任何错误消息。

控制器:

public function score($id = null)
{
    if ($this->Auth->user('id')) {
        if ($this->Entry->updateAll(array('Entry.score' => 'Entry.score+1'), array('Entry.id' => $id))) {
            $this->Entry->Score->create();
            $this->request->data['Score']['entry_id'] = $id;
            $this->request->data['Score']['user_id'] = $this->Auth->user('id');
            if ($this->Entry->Score->save($this->request->data)) {
                $total_scored = $this->Entry->find('first', array('conditions' => array('Entry.id' => $id)));
                $this->Entry->contain();
                $this->set('total_scored', $total_scored);
                if ($this->request->is('ajax')) {
                    $this->render('_scored', 'ajax');
                } else {
                    $this->redirect(array('action' => 'index'));

                }
            }
        }
    }
}

看法:

 <div class="scoreButton-<?php echo $entry['Entry']['id'];?>">
 <?php if (((AuthComponent::user('id')) && ($entry['Entry']['user_id'] !== AuthComponent::user('id')))): ?>
            <p class="monster l20 t25 hover"><?php echo $this->Js->link('+','/entries/score/' . $entry['Entry']['id'] . '', array('update' => '.scored-' . $entry['Entry']['id'] . '', 'complete' => $this->Js->get('.scoreButton-' . $entry['Entry']['id'] . '')->effect('hide'))); ?>
            </p>
        </div>
        <div class="scored-<?php echo $entry['Entry']['id'];?>"> </div>

        <?php endif;?>
        <?php if (!AuthComponent::user('id')): ?>
        <p class="monster grey l20 t25">+</p>
        <?php endif;?>
4

1 回答 1

0

我暂时找到了解决方法。它看起来像一个蛋糕错误,我会报告它。解决方法是使用虚拟 entry_id(用户不可见)保存分数,并在保存后立即将其更新为正确的 entry_id。

    public function score($id = null)
{
if ($this->Auth->user('id')) {
    if ($this->Entry->updateAll(array('Entry.score' => 'Entry.score+1'), array('Entry.id' => $id))) {
        //$this->Entry->Score->create();
        $this->request->data['Score']['entry_id'] = '50458841-0000-0000-0000-1166a76fed09';
        $this->request->data['Score']['user_id'] = $this->Auth->user('id');
        if ($this->Entry->Score->save($this->request->data)) {
            $this->Entry->Score->updateAll(
                array('Score.Entry_id' => "'".$id."'"),
                array('Score.id' => $this->Entry->Score->id)
            );
            $total_scored = $this->Entry->find('first', array('conditions' => array('Entry.id' => $id)));
            $this->Entry->contain();
            $this->set('total_scored', $total_scored);
            if ($this->request->is('ajax')) {
                $this->render('_scored', 'ajax');
            } else {
                $this->redirect(array('action' => 'index'));

            }
        }
    }
}

}

于 2012-09-23T23:22:53.477 回答