We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25420
    • 74 Posts
    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
    • You can see if the save() is successful (returns true on success, false on failure), you can set PDO’s error-related attributes appropriately, and you can use PDO’s error-related functions. See more information under Errors and Error Handling at http://php.net/pdo.
        • 25420
        • 74 Posts
        ok thanks, just testing the save() return will do.

        I have now completed the description of classes/maps of the following tables:

        site_content
        document_groups
        documentgroup_names
        member_groups
        membergroup_names
        membergroup_access
        manager_user
        user_attributes
        user_settings

        I’m starting to write some generics functions that I will use with this tables, like duplicating site_content , assign an user to a group, a document to a group, etc...This is extending the core Modx API to users/groups/documents and thanks to xPdo, so quick to build. I will share it if anybody find it of any interest and if somebody tells me what’s the best way to do it.

        Out of topic, you probably know it already, but the naming convention of tables/fields is not very good (table names are not consistents, foreign key fields are not consistents from one table to another, etc). I guess that’s because Modx has followed different paths to come to what it is now; probably it would worth to spend some time one day to improve that. Modx is "only" amazing, it needs to be perfect wink

        Cheers
        seb
        • Roki, what you are doing has already been done. It’s called MODx 0.9.7...
            • 25420
            • 74 Posts
            oh dear...Well it was needed then grin

            I needed it now though, so that’s ok. And it has been a good pratice for me.