Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Model Save/Create not getting passed primary key

Ok today's issue.. The model does not seem to recognize the primary key, column named "id"

  $robot  = new robot();
  $robot->code = $unique_code;
  $robot->user_id = 0;
  $robot->agent_name = "name here";
  $robot->date_created = date("Y-m-d H:i:s");
  $robot->date_submitted = NULL;
  $robot->create() ;  (also tried save())

error being returned We can't store robots right now: id is required

If I add in $robot->id = 1, it saved the first record

Then if I run with $robot->id = 1 again I get "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'"

Confused at this point, my model is there "empty" in the class, just there for the reference

Table and model class were not names with "S" on the end robot vs robots

Please, provide a model and table structure

Total guess, but it sounds like you forgot to set your id column as an Auto Increment field.



20.3k

Adding to Pavel's answer:

if (!$robot->create())
{
    echo '<pre>';
    foreach($robot->getMessages() as $message)
    {
        echo $message;
    {
    die();
}

And yeah, of course you get duplicated, because it already exists in the DB; a PK NEVER can be repeated in a table. So, when you are going to save your model, and the DB sees that the ID already exists, throws an exception (just like if you were adding it manually in the DB).



9.3k
Accepted
answer
edited Jan '15

It was because Model class and Table were not ending with an "s" robot vs robots

btw, I don't see that specifically stated in the model documentation even tho all examples are plural. I know other ORMs require items as plural table names and the auto id to actually be "id" (ie tables.id)

Is this correct to assume it needs plural table names? Also is there a way to not force that?



20.3k
edited Jan '15

You can name it as you want:robot => Robot, my_robots => MyRobots.

Also you can do this http://docs.phalconphp.com/es/latest/reference/models.html#pointing-to-a-different-schema

edited Jan '15

You can name it as you want:robot => Robot, my_robots => MyRobots.

Also you can do this http://docs.phalconphp.com/es/latest/reference/models.html#pointing-to-a-different-schema

Actually the getSource command works to make the non plural table and plural model (but not with a singular class name and singular table for me unless the class name does not match the table name.)

model robots getSource(robot) = works

model robot getSource(robot) = not working

model robotcreator getSource(robot) = working

 public function getSource()
    {
        return "robot";
    }


20.3k

Your second choice works for me. Maybe will be because of your DMBS. But I cannot go far away on this problem 'cause of lacky knowledge.

edited Jan '15

Your second choice works for me. Maybe will be because of your DMBS. But I cannot go far away on this problem 'cause of lacky knowledge.

don't worry about it now, have it working now Problem is the issue was created using Vokuro as foundation and the naming was not obviouly "robot" it was "HealthFormFull" which class was empty on model. the table was named "health form full" (with underscores) using mysql and yes primary key etc was all created properly. In the controller I used " use Crm\Models\HealthFormFull" and simply did a new/create as stated with the ORM. It did not work without plural.