Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:39:47+00:00 2026-05-31T19:39:47+00:00

I’ve been trying to learn cakephp recently but I’m struggling to find any tutorials

  • 0

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);
 }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:39:48+00:00Added an answer on May 31, 2026 at 7:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.