1

I'm using CakeDC Users plugin and I want to save a "Docente" after saving a "User". "User" has a one to one relationship with "Docente".

I decided to get the id of the last "User" inserted and then saving "Docente". It worked, but I'm worried that when there are multiple users being inserted at the same time then there is going be problems at saving the proper 'user_id' for the "Docente" table.

So I was asking if there is a method to overcome problems in the "Docente" table regarding the user_id foreign key.

I don't want to mess with the plugin funcionality too much since there are some parts I can hardly understand.

Thanks in advance.

Vanessa
  • 51
  • 7

1 Answers1

1

Like explained in the readme.md, just extend the user model and overload the register() method.

public function register($postData = array(), $options = array()) {
    if (parent::register($postData, $options)) {
        // Do your additional saves here
        debug($this->data); // See the output
        return true;
    }
    return false;
}

Or implement a method in your Docente model and attach it to the Users.Model.User.afterRegister event. I don't think the plugin code is hard to understand.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • 1
    I followed your first suggestion and overloaded the method in the Docente model. Now in DocenteController, in the 'add' method I put this line: $doc = $this->Docente->register($this->request->data); But I got this 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 'register' at line 1" So what is it am doing wrong? – Vanessa Jun 02 '14 at 15:30
  • Well, I checked Docente model and there was an error at extending the user model, this is how it is right now "App::uses('User', 'Users.Model'); class Docente extends User{public $useTable = 'users';....}" but now I get this error at 'docentes/add' "Undefined index: password [APP\Plugin\Users\Model\User.php, line 158]" – Vanessa Jun 02 '14 at 16:52
  • Well, then debug the data? It's not present for a reason. Can't tell you anything without the code. http://blog.teamtreehouse.com/how-to-debug-in-php – floriank Jun 02 '14 at 18:52