2012-04-09 73 views
0

嗨我試圖做一個大學項目的視頻流網站的職位部分。 我有一個帖子控制器和模型。我認爲我會爲posts輸入框創建一個元素,這樣我就可以在localhost/evolvids/uploads/watch/section中回顯這一點,但是當我提交一篇文章時,我會引發一個MISSING CONTROLLER錯誤。CakePHP元素不更新表

這是我的帖子元

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

任何想法,我要去哪裏錯了嗎?

回答

1

$this->Form->create()模型聲明不應該是複數:

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

它不解決失蹤控制器錯誤,請更新您與錯誤消息的內容原來的問題(該控制器缺失,例如)。

+0

我現在得到此以下錯誤: 數據庫錯誤 錯誤:SQLSTATE [23000]:完整性約束違規:1452不能添加或更新子行,外鍵約束失敗('evolvids2'.'posts', CONSTRAINT'posts_ibfk_1' FOREIGN KEY('upload_id')REFERENCES'uploads'('id')ON UPDATE CASCADE) SQL查詢:INSERT INTO'evolvids2'.'posts'('comment')VALUES('dsgsdg') – 001221 2012-04-10 14:28:59

+0

該查詢還沒有插入用戶名和uploadid到帖子表中:S – 001221 2012-04-10 14:33:18

+0

也如果我把控制器放在app/plugins/posts下的插件中addcontroller擴展PostsAppController我得到這個錯誤 致命錯誤:類'PostsAppController'不是在/ Applications/XAMPP/xamppfiles/htdocs/evolvids/app/P中找到lugin/Posts/Controller/AddController.php on line 4 – 001221 2012-04-10 14:51:25