Search code examples
phpsqlcakephpassociationsmodel-associations

CakePHP Associations error? Won't save associated table


I created a user profile where users can add their information. So on URL section I want them to put their other links like Facebook, www.facebook.com/user etc.

But when I click Save nothing updates?

Here is the Model for User: Models/User.php

<?php
class User extends AppModel {

    public $name = 'User';

    public $hasMany = array (
    'UserWeb'=>array(
        'className'=>'UserWeb',
        'foreignKey'=>'user_id'
        )
    );
}
?>

The model for UserWeb: Models/UserWeb.php

<?php
class UserWeb extends AppModel {

public $name = 'UserWeb';

public $belongsTo = array('User' =>
    array('className'  => 'User',
        'conditions' => '',
    'order'      => '',
    'foreignKey' => 'user_id'
    )
    );
}
?>

The controller for user:

$this->User->id = $this->Auth->user('id');
if ($this->request->is('post')) {
    if ($this->User->save($this->request->data, array('validate' => false))) {
        $this->Session->setFlash('Profile updated succsessefully!',
                                     'default', array('class' => 'okmsg'));

    $this->redirect($this->request->here);
    } else {
      $this->Session->setFlash('Profile could not be saved. Please, try again.',
                                       'default', array('class' => 'errormsg'));
    }
}

And the View form:

<?php
    echo $this->Form->create();
echo $this->Form->input('UserWeb.title',
         array('label'=>false,'placeholder'=>'Title'));
echo $this->Form->input('UserWeb.url',
         array('label'=>false,'placeholder'=>'Enter URL'));
echo $this->Form->end('Save');
?>

Can Anyone help please? I'm trying to find solution for a lot of time. When I submit the form it won't store new informations in user_webs table.

And btw here is the table:

CREATE TABLE `user_webs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT '0',
  `title` varchar(128) DEFAULT NULL,
  `url` varchar(128) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `web` (`user_id`),
  CONSTRAINT `web` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

Solution

  • You are trying to Associated model's data. And your primary Model does not have data to save.

    You can take two approaches here,

    1. Your present way, take User's info and have them provide UserWeb information from the same form. The use will provide the information as a part of registration. For this you should refer to Saving Associated model data - saveAll()

    2. Second way, have user provide their info during registration and let them add UsersWeb later. (by later I mean user's can update it during registration as well but not in same form). For this use UserWeb. Have the same form appear from /yourapp/UsersWebs/add. You will have to provide the user_id. This will be more refined.

    P.S: I strongly recommend you to go through Saving your data again. It will hardly take you 10 mins, I am sure you have done this before but sparing another 10 mins after having encountered the issue will help you understand these things as back of your hand.