MODX3: REST API Problem PUT id=0

Dear Community,

at the moment I learn to use MODX3 Rest functionality.

Now I crashed with the following problem:

When I send POST request, the data is added in database, but in die returning object the id is always 0 insteed of the real id of new entry.

What do I have made wrong?

Thank you in advance.

bye
Chris

Do you have an autoincrement column called id in your database table?


In general, you can override the post function (or maybe the afterPost function) in your REST controller, if you need to change the default behaviour.

public function post()
{
	...
}

thank you for your answer.

the custom database schema


CREATE TABLE `modx_todos` (
	`id` INT(10) NOT NULL AUTO_INCREMENT,
	`title` VARCHAR(250) NULL DEFAULT '' COLLATE 'utf8_general_ci',
	`done` TINYINT(1) NULL DEFAULT NULL,
	PRIMARY KEY (`id`) USING BTREE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=17

The insert is correct done, but I dont get the object with the real insertion id value back.

Any idea?

No! I can’t reproduce this behaviour.

you got back the object with id not 0?

Yes.

If you look at the source code

this is basically like a snippet with this code:

<?php
use todo\Model\Todos;

$item = $modx->newObject(Todos::class);
if ($item->save()) {
    return json_encode($item->toArray());
}

If you execute a snippet with this content, do you get the ID in the result?

I tested it, but without success.
I got always:

{"id":0,"title":"","done":false}

:man_shrugging:

That’s weird.

Can you post your schema?

sure:


<?xml version="1.0" encoding="UTF-8"?>
<model package="todo\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="3.0">
  <object class="Todos" table="todos" extends="xPDO\Om\xPDOSimpleObject">
    <field key="id" dbtype="int" precision="10" phptype="integer" null="false" default="AUTO_INCREMENT"/>
    <field key="title" dbtype="varchar" precision="250" phptype="string" null="true" default=""/>
    <field key="done" dbtype="int" precision="1" phptype="boolean" null="false" default="0"/>
    <index alias="id" name="id" primary="true" unique="true" type="BTREE">
      <column key="id" length="" collation="A" null="false"/>
    </index>
    <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE">
      <column key="id" length="" collation="A" null="false"/>
    </index>
  </object>
</model>

I imagine, that I have had problems with AUTO_INCREMENT in schema. I modified it in database directly.

If your class extends xPDO\Om\xPDOSimpleObject you don’t have to define an id column. xPDO\Om\xPDOSimpleObject already has an id field.

<?xml version="1.0" encoding="UTF-8"?>
<model package="todo\Model" baseClass="xPDO\Om\xPDOObject" platform="mysql" defaultEngine="InnoDB" phpdoc-package="" version="3.0">
  <object class="Todos" table="todos" extends="xPDO\Om\xPDOSimpleObject">
    <field key="title" dbtype="varchar" precision="250" phptype="string" null="true" default=""/>
    <field key="done" dbtype="int" precision="1" phptype="boolean" null="false" default="0"/>
  </object>
</model>

MODX also creates a corresponding PHP class. If you change the database you also have to change the mapping.

Recreate the class files and database table with the schema above and it should work correctly.

wow, thats it.

Thank you so much. :pray: