We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    EDITED - ok actually will teach me to look at everything before posting but it is only the username that is not set on the child.

    OK here is the situtation I have a form which accepts a Parent who will become a modx user it also accepts multiple children who will also become modx users.

    here is the code I’m using to create these users
    <?php
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    $modx->addPackage('HCSpr', '/opt/lampp/htdocs/HCSpr/core/components/HCSpr/model/');
    
    if (!isset($_POST['submit'])) {
        return null;
    } else {
        /* Process Parent */
        $parentRes = $modx->runProcessor('security/user/create', array(
                    'username' => $_POST['firstname'] . $_POST['surname'],
                    'fullname' => $_POST['firstname'] . ' ' . $_POST['surname'],
                    'address' => $_POST['address1'],
                    'city' => $_POST['address2'],
                    'state' => $_POST['address3'],
                    'zip' => $_POST['postcode'],
                    'phone' => $_POST['phonenum'],
                    'mobilephone' => $_POST['mobilenum'],
                    'email' => $_POST['email'],
                    'extended' => array(
                        'title' => $_POST['title'],),
                    'newpassword' => TRUE,
                    'passwordnotifymethod' => 'e',
                    'passwordgenmethod' => 'g',
                    'class_key' => 'ParentExtUser',
                ));
        if ($parentRes->isError()) {
            if ($parentRes->hasFieldErrors()) {
                $fieldErrors = $parentRes->getAllErrors();
                $errorMessage = implode("\n", $fieldErrors);
            } else {
                $errorMessage = 'An error occurred: ' . $parentRes->getMessage();
            }
            $hook->addError('error_message', $errorMessage);
            return false;
        } 
        /* Parent Processed and return including id in parentRes */
    
        /* Process Each Child */
        $i = 0;
        $val = (int) $child = $_POST['numberChildren'];
      /*  $object = $parentRes->getObject();
        $parentid = $object['id']; */
        $rolenum = $_POST['child-rolenum'];
        $firstname = $_POST['child-firstname'];
        $surname = $_POST['child-surname'];
        $yearGroup = $_POST['child-YG'];
        $group = $_POST['child-class'];
        $dob = $_POST['child-dob'];
        $UPN = $_POST['child-UPN'];
        $house = $_POST['child-House'];
        $religion = $_POST['child-religion'];
        $language = $_POST['child-homelang'];
        $status = $_POST['child-status'];
    
    
        while ($i <= $val) {
            $username = $firstname[$i] . $surname[$i];
    
            $childRes[$i] = $modx->runProcessor('security/user/create', array(
                        'username' => $username,
                        'fullname' => $firstname[$i]. ' ' .$surname[$i],
                        'dob' => $dob[$i],
                        'extended' => array(
                            'rolenum' => $rolenum[$i],
                            'yearGroup' => $yearGroup[$i],
                            'group' => $group[$i],
                            'UPN' => $UPN[$i],
                            'house' => $house[$i],
                            'religion' => $religion[$i],
                            'language' => $language[$i],
                            'status' => $status[$i],
                            ),
                        'email' => '[email protected]',
                        'newpassword' => TRUE,
                        'passwordnotifymethod' => 'e',
                        'passwordgenmethod' => 'g',
                        'class_key' => 'PupilExtUser',
                    ));
    
            if ($childRes[$i]->isError()) {
                if ($childRes[$i]->hasFieldErrors()) {
                    $fieldErrors = $childRes[$i]->getAllErrors();
                    $errorMessage = implode("\n", $fieldErrors);
                } else {
                    $errorMessage = 'An error occurred: ' . $childRes[$i]->getMessage();
                }
                return ($errorMessage);
            }
            $i++;
        }
    
        return true;
    }
    ?>
    


    Now if I run this as is the parent is created but when the child is created all the fields are set to blank, but if I comment out the parent creation then the child is created as expected. So the code for creating the child is obviously correct. When I run this in debug the $scriptproperties in create contain all the information but they are not being saved to the DB.

    Now I could create a seperate form for parents and children but from an end user point of view I feel it is easier to have them on the same form.

    Enviroment
    mySQL 5.1.41
    PHP 5.3.1
    MODX MODx Revolution 2.1.0-dev (git)