title: CakePHP does not save to the database
body: <p>So I have three tables, the users, groups and users_groups which is a join table.</p>
  
  <pre><code>--
  -- Table structure for table `groups`
  --
  
  CREATE TABLE `groups` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(50) NOT NULL,
    `description` text NOT NULL,
    `all_versions_available` tinyint(1) unsigned NOT NULL DEFAULT '1',
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    PRIMARY KEY (`id`),
    KEY `name` (`name`,`created`,`modified`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `users`
  --
  
  CREATE TABLE `users` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `username` varchar(40) NOT NULL,
    `email` varchar(80) NOT NULL,
    `password` varchar(50) NOT NULL,
    `role` varchar(20) NOT NULL,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    `fullname` varchar(80) NOT NULL,
    `password_token` varchar(40) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `nickname` (`username`,`email`,`password`),
    KEY `role` (`role`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `users_groups`
  --
  
  CREATE TABLE `users_groups` (
    `user_id` int(11) unsigned NOT NULL,
    `group_id` int(11) unsigned NOT NULL,
    KEY `user_id` (`user_id`,`group_id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>Before I have implemented the HABTM in my Group and User models, the code I have below worked fine, now, I am getting all the data I need but I am unable to save.</p>
  
  <p>So, my Group Model looks like this:</p>
  
  <pre><code>&lt;?php
  
  class Group extends AppModel {
  
      public $hasAndBelongsToMany = array(
          'Application' =&gt; array(
              'className' =&gt; 'Application',
              'joinTable' =&gt; 'applications_groups',
              'foreignKey' =&gt; 'group_id',
              'associationForeignKey' =&gt; 'application_id',
              'unique' =&gt; 'keepExisting',
          ),
          'User' =&gt; array(
              'className' =&gt; 'User',
              'joinTable' =&gt; 'users_groups',
              'foreignKey' =&gt; 'group_id',
              'associationForeignKey' =&gt; 'user_id',
              'unique' =&gt; 'keepExisting',
          )
      );
  
      public $validate = array(
          'name' =&gt; array(
              'required' =&gt; array(
                  'rule' =&gt; array('notEmpty'),
                  'message' =&gt; 'Group name is required'
              )
          )
      );
  
      public function saveGroup($id, $name, $description) {
          $id = (int)$id;
          if ($id) {
              $this-&gt;id = $id;
          }
          else {
              $this-&gt;create();
          }
          $this-&gt;set('name', $name);
          $this-&gt;set('description', $description);
          $this-&gt;save();
          return $this;
      }
  
      public function getAll() {
          $options = array('order' =&gt; array('Group.name' =&gt; 'ASC'));
          $data = $this-&gt;find('all', $options);
          return $data;
      }
  
      public function getOne($id) {
          $id = (int)$id;
          return $this-&gt;find('first', array('conditions' =&gt; array('Group.id' =&gt; $id)));
      }
  
  }
  </code></pre>
  
  <p>My User model looks like this:</p>
  
  <pre><code>&lt;?php
  
  class User extends AppModel {
  
      public $hasAndBelongsToMany = array(
          'Group' =&gt; array(
              'className' =&gt; 'Group',
              'joinTable' =&gt; 'users_groups',
              'foreignKey' =&gt; 'group_id',
              'associationForeignKey' =&gt; 'user_id',
              'unique' =&gt; 'keepExisting',
          )
      );
  
      public function getOne($id) {
          $this-&gt;id = $id;
          $data = $this-&gt;read(null, $id);
          unset($data['User']['password']);   
          unset($data['User']['password_token']);
          if (isset($data['User'])) $data['User']['gravatar_url'] = 'http://www.gravatar.com/avatar/'.md5($data['User']['email']).'.jpg';
          return $data;
      }
  
      private function addGravatars($data) {
          foreach ($data as $key=&gt;$user) {
              $data[$key]['User']['gravatar_url'] = 'http://www.gravatar.com/avatar/'.md5($user['User']['email']).'.jpg';
          }
          return $data;
      }
  
      public function getAll() {
          $data =  $this-&gt;find('all', array('order' =&gt; array('User.fullname' =&gt; 'ASC')));
          $data = $this-&gt;addGravatars($data);
          return $data;
      }
  
      public function countAll() {
          return $this-&gt;find('count');
      }
  
  }
  </code></pre>
  
  <p>I have been using model for the join table:</p>
  
  <pre><code>&lt;?php
  
  class UsersGroup extends AppModel {
  
      public function deleteAllWithGroup($groupId) {
  
          $id = (int)$groupId;
          return $this-&gt;deleteAll(array('UsersGroup.group_id' =&gt; $id), false);
      }
  
      public function saveUsersForGroup($users, $groupId=0) {
          $this-&gt;deleteAllWithGroup($groupId);
          $data = array();
          foreach ($users as $id=&gt;$user) {
              $data[] = array('user_id'=&gt;(int)$id, 'group_id'=&gt;$groupId);
          }
          $this-&gt;saveMany($data);
      }
  
  }
  </code></pre>
  
  <p>And this is my Groups controller:</p>
  
  <pre><code>&lt;?php
  
  class GroupsController extends AppController {
  
      var $uses = array('Group', 'User', 'UsersGroup');
  
      public function index() {
          $this-&gt;set('groups', $this-&gt;Group-&gt;getAllWithInfo());
      }
  
      public function edit($id=0) {
          $this-&gt;set('group', $this-&gt;Group-&gt;getOne($id));
  
          $this-&gt;set('usersList', $this-&gt;User-&gt;getAllWithGroupInfo($id));
  
          if ($this-&gt;request-&gt;is('post')) {
              $group = $this-&gt;Group-&gt;saveGroup($this-&gt;request-&gt;data['id'], $this-&gt;request-&gt;data['name'], $this-&gt;request-&gt;data['description']);
  
              // Saving users
              if (!isset($this-&gt;request-&gt;data['user']) || empty($this-&gt;request-&gt;data['user'])) {
                  $this-&gt;UsersGroup-&gt;deleteAllWithGroup($group-&gt;id);
              }
              else $this-&gt;UsersGroup-&gt;saveUsersForGroup($this-&gt;request-&gt;data['user'], $group-&gt;id);
  
          }
      }
  
      public function view($id) {
          App::uses('Platforms', 'Lib/Platform');
          $this-&gt;setPageIcon('group');
          $this-&gt;set('group', $this-&gt;Group-&gt;getOne($id));
      }
  
      public function delete($id) {
          $this-&gt;Group-&gt;delete((int)$id);
          return $this-&gt;redirect(array('action' =&gt; 'index'));
      }
  
  }
  </code></pre>
  
  <p>There is a couple of issues, the system above works if I remove the HABTM configs, second, I don't, for some very specific reasons not using the forms helper to generate the form and unfortunately for the complexity of the code (this is just a little bit) I can't so I have to name everything manually myself (that's where I see the biggest potential for failure) and lastly when I fire this code now I get:</p>
  
  <pre><code>Database Error
  
  Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'deleteAllWithGroup' at line 1
  
  SQL Query: deleteAllWithGroup
  
  Notice: If you want to customize this error message, create app/View/Errors/pdo_error.ctp
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ELia2.jpg" alt="SQL error message"></p>
  
  <p>So the UsersGroup model is not being registered, nothing changes when I even delete the file, it is trying to use the name of the method I have previously used to delete the old join data as an SQL command. I have tried all the possible suggestions for naming and structure of the data I have found on Stack but failed, furthest I got was when I got only one of the join items to save, always the last one in the array ...</p>
  
  <p>Anyone can help with this one?</p>
  
  <p>Cheers,</p>
  
  <p>O.</p>