tags:

views:

454

answers:

1

Hi,

I Want to separate the database functionality and logic part by having the database operations in the model and the logic part in the controller. Earlier I had all the code in the action part of the controller itself. I have tried something, but it doesn't work. Some one guide me.

This is what I had earlier.
/* Controller */

function insertFormName(){    
    $formname=$_POST['formname'];
 $ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
 $newid=$ret[0]['forms']['id'];
 $this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid)); 
    }

And now I changed it a bit,which doesn't work.

/* Controller */

function insertformname()
    {    
       $this->data['Form']['formname']=$this->params['form']['formname'];
       $this->Form->save($this->data);  
    $this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid)); 
    }

/* Model */

function save($data)
    {    

 $ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
 $newid=$ret[0]['forms']['id'];
 $this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
 return $newid;

    }

EDIT:

I have tried it another way.. Have the entire functionality in the model and just call that function from the controller.Is this method correct?

/* Model */

 function saveFormName($data)
  {    
    $this->formname=$data[$this->name]['formname'];
 $ret = $this->Form->query("Select id from forms order by id DESC LIMIT 1");
 $newid=$ret[0]['forms']['id'];
 $this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));
  }

/* controller */

 function insertformname()
    {    
 $this->data['Form']['formname']=$this->params['form']['formname'];
 $this->Form->saveFormName($this->data);  
    }
+2  A: 

It looks like you should probably revisit the Cake book (book.cakephp.org) and redo the lessons. If you setup your form correctly, you shouldn't have to manually assign $_POST['formname'] to $this->data. Try setting the field names in your form (in the HTML) to data[ModelName][FieldName].

Next:

$this->Form->updateAll(array('Form.name' => "'$formname'"),array('Form.id' => $newid));

Why are you updating data right after you saved it? Where do the $newid and $formname variables come from? You have them defined in the Model::save, but not in the controller.

This seems like you're trying to fight with the Cake automagic stuff too much. Perhaps you should repost your question, but please spell out your high level description rather than just a "why doesn't this work?" It looks to me like this could be simplified a ton, but, again, I'm not quite sure what your objectives are.

Respectfully, Travis

Travis Leleu