CakePHP元素未更新表 [英] CakePHP Elements not updating table

查看:97
本文介绍了CakePHP元素未更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试在视频流媒体网站上为大学项目张贴文章部分。
我有一个职位控制器和模型。我以为可以为posts输入框创建一个元素,以便可以在localhost / evolvids / uploads / watch /部分中对此进行回显,但是当我提交帖子时,我抛出了MISSING CONTROLLER错误。

Hi Im trying to make a posts sections for a university project on video streaming website. I have a posts controller and model. I thought that I would create an element for the posts input box so that I could echo this out in the localhost/evolvids/uploads/watch/ section, however when I submit a post I get thrown a MISSING CONTROLLER error.

这是我对Posts元素的代码

This is my code for the Posts Element

<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>

这是我的Posts Controller脚本

This is my Posts Controller script

<?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;
}



}

?>
上传模型

?> Uploads Model

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

    );  

  }
 ?>

在上传视图中,我将元素称为如下

On the uploads view I am calling the element as follows

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

有什么想法我要去哪里吗?

Any ideas where I'm going wrong?

推荐答案

$ this-> Form-> create()中的模型声明不应为复数形式:

The model declaration in $this->Form->create() should not be plural:

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

这并不能解决丢失的控制器错误,请使用错误消息的内容更新您的原始问题(其中例如缺少控制器)。

It that doesn't solve the missing controller error, please update your original question with the contents of the error message (which controller is missing, for example).

这篇关于CakePHP元素未更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆