0

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.

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>

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){
}
}
?>

Posts Model

  <?php

   class Post extends AppModel{

public $name = 'Post';

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

   );  
   }
  ?>

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



}

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

user0129e021939232
  • 6,205
  • 24
  • 87
  • 140

1 Answers1

1

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

mensch
  • 4,411
  • 3
  • 28
  • 49
  • i've now got this following error: Database Error Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`evolvids2`.`posts`, CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`upload_id`) REFERENCES `uploads` (`id`) ON UPDATE CASCADE) SQL Query: INSERT INTO `evolvids2`.`posts` (`comment`) VALUES ('dsgsdg') – user0129e021939232 Apr 10 '12 at 14:28
  • the query also is not INSERTING the username and uploadid into the posts table :S – user0129e021939232 Apr 10 '12 at 14:33
  • also if i put the controller in the plugin under the app/plugins/posts addcontroller extends PostsAppController i get thrown this error Fatal error: Class 'PostsAppController' not found in /Applications/XAMPP/xamppfiles/htdocs/evolvids/app/Plugin/Posts/Controller/AddController.php on line 4 – user0129e021939232 Apr 10 '12 at 14:51
  • I don't know where `evolvids2` is coming from, nor `posts_ibfk_1`, as I see it nowhere in your code. Both the video and user form fields are likely incorrect as well (`video.id`, `user.id`). Using that notation Cake thinks they belong to a `video` and `user` model (both likely don't exist), you probably mean user_id and video_id, they're both in the posts table, right? The missing controller message regarding plugins is unrelated to your original question, I believe. – mensch Apr 10 '12 at 14:55
  • evolvids2 is the name of my database, I'm not sure where posts_ibfk_1 is coming from? ive changed the video.id to uploadid and userid is the same, I do have a uploads model and a user model.and yes the user_id and the uploads_id are in the posts table. any ideas as to what is going wrong?? – user0129e021939232 Apr 11 '12 at 13:24
  • does it make any difference that I've set up foreign keys in my database? as well as defining them in my models? – user0129e021939232 Apr 11 '12 at 13:32
  • It should be `upload_id` and `user_id` if those columns have those names in the respective tables. Are you using MySQL or PostgreSQL, because the database error doesn't look like the standard errors MySQL would throw. – mensch Apr 11 '12 at 13:47
  • im using MySQL, i've removed the foreign keys from the database and the post has been updated, however it is not updating the upload_id field or the user_id field with the save function. also I get this missing view error, Missing View Error: The view for PostsController::add() was not found. Error: Confirm you have created the file: /Applications/XAMPP/xamppfiles/htdocs/evolvids/app/View/Posts/add.ctp Notice: If you want to customize this error message, create app/View/Errors/missing_view.ctp – user0129e021939232 Apr 11 '12 at 13:52
  • If the fields (`upload_id` and `user_id`) exist in your posts table and are present in your form they should be updated. You're receiving the error message because the form action is set to go to `/posts/add` after submitting. Your question about related comments might merit another separate question. – mensch Apr 11 '12 at 14:12
  • yeah i got it working, the `upload_id` and `user_id` were not consistant in form and mysql table. its now working, was wondering how i would query the database in the uploads controller to search the comments table to display the comments related to the upload that is currently been viewed? – user0129e021939232 Apr 11 '12 at 14:30