2

I have a Model for Groups and another model for Notes (Notes and Posts are same things).

NotesController:

   public function groupnotes()
   {

    if (!empty($this->data))
     {
        $data = $this->data;
       $data['Note']['user_id'] = $this->Auth->user('id');

        if ($this->Note->save($data))
           {
             PROBLEM HERE
           } 
      }


   if(empty($this->data['Note']['notes']))
      {
        PROBLEM HERE
      } 

GroupsController: (ViewCourse is used to view each group )

public function viewcourse($id=NULL)
 {
    $this->set('viewcourse', $this->Group->read(NULL,$id));
    $this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));

}

Now when i create a post in a group it redirects me to "groupnotes" action and i want it to redirect me to viewcourse/id ... I am a bit confused how can i redirect the page to viewcourse/id ...

I tried doing it by adding this to groupnotes action

 $this->redirect(array('controller'=>'groups',  'action' => 'viewcourse'));

but here i do not have the id.

What do you suggest?

Osama Yawar
  • 361
  • 2
  • 6
  • 19

1 Answers1

4

This question might help you: What is the equivalent to getLastInsertId() in Cakephp?

$this->redirect(array(
           'controller'=>'groups',  
           'action' => 'viewcourse/'.$this->Group->getLastInsertId())
            );

EDIT: I have only suggested that you go to the last inserted id of a group as a suggestion. Your question is a bit vague when you say "but here i do not have the id." 1. are you looking to go to any valid course id? 2. last entered course? 3. first entered course id?

Alternatively you could set a default course in your controller like so...

public function viewcourse($id=NULL)
 {
    if(!$id){
         $id = $this->Group->find('first');
         $id = $id['Group']['id'];
    } 
    $this->set('viewcourse', $this->Group->read(NULL,$id));
    $this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));

}

NOTE: Just a tip,

 $this->set('course', $this->Group->find('all', array('conditions'=>array('Group.id'=>$id))));

Can be substituted with

 $this->set('course', $this->Group->findById($id));

To make your code a bit leaner

Community
  • 1
  • 1
Tim Joyce
  • 4,487
  • 5
  • 34
  • 50
  • We cannot add this in NoteController as the model it takes is Group, secondly we want to redirect it to group view of etc etc id not notesview so using getLastInsertId() will take the id of last inserted Note in the table. – Osama Yawar Nov 26 '12 at 13:12
  • If you have your models setup correctly, you should be able to access the data you want with $this->Note->Group->getLastInsertID(); or vice versa... depending on your model associations. – Tim Joyce Nov 26 '12 at 13:17
  • a) find(all) should be find(first) or findById() as you stated. find(all) with one expected result doesnt make sense :) b) $this->Note->id is usually better than using getLastInsertId() – mark Nov 26 '12 at 14:09
  • @mark I only mentioned getLastInsertId() because my original thinking of the question was that he was looking to redirect to the note that is currently being saved in the controller. After re-reading the question I realized that wasn't what he was looking to do and made appropriate edits. – Tim Joyce Nov 26 '12 at 14:14
  • 1
    well, the corrections were not so much addressed to you than to the person asking the question, of course :) sry for not mentioning that. – mark Nov 26 '12 at 15:47
  • 1
    Thank you every one. It was helpful :) – Osama Yawar Nov 28 '12 at 18:08