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 977853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:58:10+00:00 2026-05-16T03:58:10+00:00

I am new to cakephp and have been working through the Apress book Beginning

  • 0

I am new to cakephp and have been working through the Apress book “Beginning CakePHP from Novice to Professional” but have ran into a problem after the baking chapter.

(If you’ve seen my other recent question, you’ll know that I had to skip that chapter because I cant get the bake console working on my win7 machine).

The problem is that I don’t think the table associations are working properly anymore, even though they used to when I started the blog application example at the start of the book.

The blog example has the following tables:

Users
Posts

The User model: $hasMany = array(‘Post’);
The Post model: $belongsTo = array(‘User’);

I am currently using the scaffold just to test out everything but have a few custom views and a custom add() action for the posts_controller.

The problem is that when I use the add() action for the posts_controller, the id field for the user (from the Users table) isn’t put into the user_id foreign key field of the Posts table. Therefore, when I display the post, the “author” part of the view is blank because the ID cannot be found. If I use debug($posts) then the array returned doesn’t have any information for user_id of each post and therefore no information in the ‘User’ array.

I thought user_id was the conventional way of adding an association between tables for cakePHP but it doesn’t seem to be working.

Any ideas what I need to do?

Thanks so much in advance,

Infiniti Fizz

P.S. Sorry everyone if I’m overwhelming you with cakephp problems.

P.P.S

Almost forgot, my add.ctp view for posts looks as follows:

<div class="posts form">
 <?=$form->create('Post');?>
 <fieldset>
 <legend>Add Post</legend>
 <?
  e($form->input('name'));
  e($form->input('date'));
  e($form->input('content'));
  e($form->input('User'));
 ?>
 </fieldset>
 <?=$form->end('Submit');?>
</div>
<div class="actions">
 <ul>
  <li><?=$html->link(__('List Posts', true),array('action'=>'index'));?></li>
  <li><?=$html->link(__('List Users', true),array('controller','users', 'action'=>'index'));?></li>
  <li><?=$html->link(__('New User', true),array('controller'=>'users', 'action'=>'add'));?></li>
 </ul>
</div>

And the add() action in posts_controller.php:

function add()
{
   if(!empty($this->data))
   {
    $this->Post->create();
    if($this->Post->save($this->data))
    {
     $this->Session->setFlash('The Post has been saved', true);
     $this->redirect(array('action' => 'index'));
    }
    else
    {
     $this->Session->setFlash('The Post could not be saved. Please try again.', true);
    }
   }
   $users = $this->Post->User->find('list');
   $this->set(compact('users'));
}
  • 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-16T03:58:11+00:00Added an answer on May 16, 2026 at 3:58 am

    I am also new to cake but as far as I know I think you should change this in add.ctp:

    $form->input('User')
    

    to this

    $form->input('user_id')
    

    The formhelper takes care to display the input as a dropdown (based on the hasMany-belongsTo references).

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

Sidebar

Related Questions

I'm new to cakephp so have just been working through the cookbook and am
I'm new to MVC3 coming from CakePHP, and have been quite impressed. But I've
I have a CakePHP website that's been working great. I just developed a new
I'm new to cakephp and following this tutorial. http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html I have created the blog
I'm new to CakePHP and am just running through the configuration process, but am
I'm new to cakephp, but from what I understand all of the database interaction
I'm new to the MVC pattern but have been trying to grasp it, for
I'm new to cakephp so go easy on me! I have a table called
I'm new to CakePHP and am wondering how to protect my forms from Cross
I'm extremely new to CakePHP. From what I've gathered, it seems like I can

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.