【问题标题】:CakePHP Elements not updating tableCakePHP Elements 不更新表格
【发布时间】:2012-04-09 14:41:53
【问题描述】:

您好,我正在尝试在视频流媒体网站上为大学项目制作帖子部分。 我有一个帖子控制器和模型。我以为我会为帖子输入框创建一个元素,以便我可以在 localhost/evolvids/uploads/watch/ 部分中回显它,但是当我提交帖子时,我得到了一个 MISSING CONTROLLER 错误。

这是我的帖子元素代码

<div class="postsform">

<table>
<tr>
<td>
<?php echo $this->Form->create('Posts', array('action'=>'add'));
    echo $this->Form->input('comment', array('between'=>'<br/>', 'cols'=>'60'));
    echo $this->Form->input('video.id', array('id'=>'video_id','value' => $uploads['Upload']['id']));
    echo $this->Form->input('user.id', array('id'=>'userid','value' => $auth['username']));
    echo $this->Form->end(__('Submit', true));?>
</td>
</tr>
</table>


</div>

这是我的帖子控制器脚本

<?php
class PostsController extends AppController {

var $name = 'Posts';
function index(){
$this->Post->recursive = 0;
$this->set('posts', $this->paginate());
}

function add(){
if (!empty($this->data)){
$this->Post->create();
if($this->Post->save($this->data)){
$this->Session->setFlash(__('Post saved', true));
} else{
$this->Session->setFlash(__('Post could not be saved. Please try again', true));
}
}
}

function view($id = null){
}
}
?>

帖子模型

  <?php

   class Post extends AppModel{

public $name = 'Post';

 public $belongsTo = array(
    'User' => array(
    'className' => 'User',
    'foreignKey' => 'id'
    ),
    'Uploads' => array(
        'className'    => 'Uploads',
        'foreignKey'    => 'upload_id'
    )

   );  
   }
  ?>

上传控制器

<?php
class UploadsController extends AppController {


    var $name = 'Uploads';

 public $components = array(
    'Session',
    'Auth'=>array(
        'loginRedirect'=>array('controller'=>'uploads', 'action'=>'add'),
        'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'),
        'authError'=>"Members Area Only, Please Login…",
        'authorize'=>array('Controller')
       )
      );


     public function isAuthorized($user) {
     // regular user can access the file uploads area
      if (isset($user['role']) && $user['role'] === 'regular') {
      return true;
       }

  // Default deny
      return false;
    }


function browse ($id = null) {
        $this->Upload->id = $id;      

    $this->set('uploads', $this->Upload->find('all'));

    }

function watch ($id = null){

    $this->Upload->id = $id;      
  $this->set('uploads', $this->Upload->read());
   }







function add() {

    if (!empty($this->data)) {
        $this->Upload->create();
        if ($this->uploadFile() && $this->Upload->save($this->data)) {
            $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true));
            $this->redirect(array('action' => 'add'));
        } else {
            $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true));

        }
    }
        }

function uploadFile() {
    $file = $this->request->data['Upload']['file'];
    if ($file['error'] === UPLOAD_ERR_OK) {
        $this->Upload->save($this->data); // data must be saved first before renaming to ID $this->(ModelName)->id 
        if (move_uploaded_file($file['tmp_name'], APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) {
            return true;
        }
    }
    return false;
}



}

?> 上传模型

<?php

 class Upload extends AppModel {

public $name = 'Upload';

public $hasandBelongstoMany = array(
    'User' => array(
    'className' => 'User',
    'unique' => 'true'),
    'Posts' => array(
        'className'     => 'Posts',
        'foreignKey'    => 'id',
        'associationForeignKey'  => 'upload_id',
        'associationForeignKey'  => 'username',
        'unique' => 'true')

    );  

  }
 ?>

在上传视图中,我将元素调用如下

<?php echo $this->element('Posts/add'); ?>

任何想法我哪里出错了?

【问题讨论】:

    标签: php mysql cakephp-2.0 model-associations cakephp-2.1


    【解决方案1】:

    $this-&gt;Form-&gt;create() 中的模型声明不应该是复数:

    &lt;?php echo $this-&gt;Form-&gt;create('Post', array('action'=&gt;'add')); ?&gt;

    如果不能解决缺少控制器的错误,请使用错误消息的内容更新您的原始问题(例如缺少哪个控制器)。

    【讨论】:

    • 我现在收到以下错误:数据库错误错误:SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败 (evolvids2.@ 987654324@,约束posts_ibfk_1外键(upload_id)引用uploadsid)更新级联)SQL查询:插入evolvids2.postscomment
    • 查询也没有将用户名和uploadid插入到posts表中:S
    • 另外,如果我将控制器放在 app/plugins/posts addcontroller extends PostsAppController 下的插件中,我会抛出此错误致命错误:在 /Applications/XAMPP/xamppfiles/htdocs 中找不到类“PostsAppController” /evolvids/app/Plugin/Posts/Controller/AddController.php 在第 4 行
    • 我不知道evolvids2 来自哪里,也不知道posts_ibfk_1,因为我在您的代码中看不到它。视频和用户表单字段也可能不正确(video.iduser.id)。使用该符号Cake 认为它们属于videouser 模型(两者可能都不存在),您可能是指user_id 和video_id,它们都在posts 表中,对吗?我相信,关于插件的缺失控制器消息与您的原始问题无关。
    • evolvids2 是我的数据库的名称,我不确定posts_ibfk_1 来自哪里?我将 video.id 更改为 uploadid 和 userid 是相同的,我确实有一个上传模型和一个用户模型。是的 user_id 和 uploads_id 在帖子表中。关于出了什么问题的任何想法??
    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 2013-01-22
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多