php - CakePHP Elements 不更新表

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

您好,我正在尝试在视频流网站上为大学项目制作帖子部分。 我有一个帖子 Controller 和模型。我以为我会为帖子输入框创建一个元素,以便我可以在 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>

这是我的帖子 Controller 脚本

<?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'
    )

   );  
   }
  ?>

上传 Controller

<?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')

    );  

  }
 ?>

在上传 View 上,我按如下方式调用该元素

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

有什么想法我哪里出错了吗?

最佳答案

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

<?php echo $this->Form->create('Post', array('action'=>'add')); ?>

如果这无法解决缺少 Controller 的错误,请使用错误消息的内容(例如缺少哪个 Controller )更新您的原始问题。

关于php - CakePHP Elements 不更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10074897/

相关文章:

php - 根据条件从数组中删除值

php - 连接五个表时如何避免重复

php - 不使用 PDO 创建表

mysql - 为什么这个异或操作在mysql中结果为零?

mysql - CakePHP表单提交 "array"

php - 为什么 "104"排在 "0000105"之前?

php - 如何获取mysql中数据的字数并以条形图显示

php - while 循环的替代方法

cakephp - CakeEmail调试

php - CakePHP 2.0 - virtualFields 不用于带选项的查找操作