user1104854
user1104854

Reputation: 2167

cakephp - modifying and saving data

I've been trying to learn cakephp recently but I'm struggling to find any tutorials that deal with storing data into a table after it's been modified. I'm used having complete control where everything goes in PHP, so it's been a struggle adjusting to the automated processe of MVC.

I thought a good first experiment would be to take an input and concatenate a letter to it(let's just say "m"). Then, store both the original value and the concatenated value in a table with fields "orignal" and "concatenated". So, if I typed "hello", the value in the original field would be "hello" and the concatenated field would be "hellom".

My question is would the model be responsible for concatenating the original value? Would it also do the saving or is that the controllers responsibility?

Here is my code: I'm getting the following error. Fatal error: Call to a member function save() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/cake/app/Model/Concatenate.php on line 6

View/Concatenates/add.php
<h1>Add Something</h1>
<?php

   echo $this->Form->create('Concatenate');
   echo $this->Form->input('original');
   echo $this->Form->end('Add Numbers');

?>

Now for the model

class Concatenate extends AppModel {

function saveConcat($original,$concatenated) {
        $this->set(array(
        'original' => $original,
        'concatenated' => $concatenated));
        $this->save();
        }

}

?>

Now for the controller

<?php
class ConcatenatesController extends AppController {

public  $helpers = array('Html', 'Form');
public $components = array('Session');

public function index() {
     $this ->set('concatenates', $this->Concatenate->find('all'));  
} 

 public function add() {
    if ($this->request->is('post')) {
        $original = $this->request->data['Concatenate']['original'];
        $concatenated =  $original."m" ;
        $this->Concatenate->saveConcat($original,$concatenated);
    }
}

function isempty(){ //used to check if there is data in the table. If there isn't any, "no data" will be displayed
       $mysorts = $this->Concatenate->find('all');
       $this->set('concatenates', $mysorts);
 }
}
?>

Upvotes: 2

Views: 2746

Answers (1)

Jeremy Harris
Jeremy Harris

Reputation: 24549

This is the never ending debate (or preference) about fat model/skinny controller and vice versa.

As far as saving goes, the model should definitely handle the logic for that. Although, you would most likely call it from the controller like $myModel->save($data);

In concatenating values, I would personally handle that in the controller because it is business logic that isn't directly related to the model. For example, you may wish to concatenate a string and send it to the view instead.

[EDIT]

Disclaimer: I have almost zero experience with CakePHP but the fundamentals are the same.

You mentioned you can't get it to work, so one thing I am noticing is you have a function called Concatenate() in your model. This is the PHP4 style of constructors and is no longer "best practice" (unless of course you are running PHP4 but why on earth would you be doing that). In fact, it is likely to be deprecated entirely in the near future. The PHP5 way of doing constructors is with the __construct() function. If you do decide to use a constructor, I'd make sure to call parent::__construct(); in it to ensure the parent AppController class loads correctly.

In looking at the Concatenate() method's functionality, I doubt you intend to have that as your constructor anyway. Rename that function to something clear like saveConcat(). Also, I'm not sure I would be using $this->request->data as your source in case you want to be able to reuse this function and call it with any value. In that case, I'd add a parameter to the function

class Concatenate extends AppModel {

    function saveConcat($data) {

       if ($this->Concatenate->save($data)) { 

          $this->Session->setFlash('Your post has been saved.');
          $this->redirect(array('action' => 'index'));

       } else {

          $this->Session->setFlash('Unable to add your post.');

       }

    }

}

Then somewhere in your controller, you will have to actually call this function. Modify your add() function from your controller to be something like this:

public function add() { 

   if ($this->request->is('post')) {

      // Put data into array for saving
      $data[] = array( 'original' => $this->request->data );
      $data[] = array( 'concatenated' => $original."m" );

      // Call model function to save it
      $this->Concatenate->saveConcat($data);

   }

}

[EDIT 2]

I just can't figure out why I'm getting the error: Call to a member function save() on a non-object.

When you call $this->Concatenate->save from inside the Concatenate class, that means you are trying to access a variable inside the class called Concatenate and execute a function. Neither of which exist of course. The reason is you need to call the object itself as such:

$this->save("blah blah");

That method (I'm assuming is a parent method from the AppModel class) will be called referencing the current instance of the Concatenate object.

Upvotes: 1

Related Questions