Hi all,
I’m currently developing an API to manage users (manager users), documents, documents groups and permissions.
The API is based on xPdo that I’ve found very convenient to use so far.
One problem I’m having is to check if an operation has finished successfully.
Simple example:
I’m creating a user and assigning him some attributes:
<?php
global $modx;
include_once ( './manager/xpdo/xpdo.class.php');
$xpdo= new xPDO('mysql:host=xxx;dbname=xxx', "xxx", "xxx");
$xpdo->setPackage('usermanagement');
$xpdo->setDebug(true);
$manager= $xpdo->getManager();
$generator= $manager->getGenerator();
$generator->parseSchema(XPDO_CORE_PATH . 'om/usermanagement/mysql/modx095.mysql.schema.xml');
/* Create user*/
$user= $xpdo->newObject('User');
$user->set('username', 'roki13');
$user->set('password', md5('password'));
/* Create attributes */
$userAttributes= $xpdo->newObject('UserAttributes');
$userAttributes->set('fullname', 'roki13');
$userAttributes->set('email', '[email protected]');
$userAttributes->set('role', 1);
$userAttributes->set('internalKey', $user->getPrimaryKey());
/* Save */
$user->addMany($userAttributes);
$user->save();
$userAttributes->save();
?>
This is ok the first time it is executed, because user ’roki13’ doesn’t exists.
If I run this a 2nd time, it will not work as ’roki13’ exists and the username field is unique.
I could start by testing if ’roki13’ exists and if so throwing an error at this time. But I was wondering if there was a more generic way to handle SQL errors like this one. that would also avoid to run another SQL statement that I think is useless.
Thanks for the great work !
Seb