Hello there.
I followed Bob's Guide for creating a custom table and I am using it to store data generate with a FormIt form. That works perfectly. However, I have two problems and spent the last two hours searching for possible solution and couldn't find any. I know it is probably just a small issue and I would highly appreciate if anyone could help me out:
1. I'm trying to list all of the rows from the table:
$contacts = $modx->getCollection('Contacts', $c, false);
[...]
foreach($contacts as $contact)...
[...]
$contact->get('name');
That works perfectly. However, I have no idea how I can get the ID of the row. I tried:
$contact->get('id');
$contact->get('pk');
$contact->get('primary_key');
Any idea?
2. Even though I can insert new rows without a problem, I can not update them. In the error.log I found the following error:
No primary key value found for pk definition:
This is how I update:
$contact = $modx->getObject('Contacts', array('id' => $_GET['contact_id']), false);
$contact->set('name', $_POST['name']);
$contact->save();
$contact->save() returns true but the changes are not stored in the database. It thus seems that error 1 and 2 are somehow connected. My table schema does not represent any primary key but I read that xPDOSimpleObject automatically selects the id field for that?
<?xml version="1.0" encoding="UTF-8"?>
<model package="contact_form" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Contacts" table="contacts" extends="xPDOSimpleObject">
<field key="name" dbtype="varchar" precision="255" phptype="string" null="true" />
[...]
</object>
</model>
It thus seems to be an issue related to the primary key.
Thanks in advance for your help.