We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7858
    • 97 Posts
    Hello, it's not completly MODx related question, but someone here can definetly help me.

    I'm writing an Angularjs app, that uses MODx data.

    First I create new user. I need to separate first name and last name, but I don't wat to create user extended settings for this, so I do:

    $userName = $array = [
        "first_name" => $formData->name,
        "last_name" => $formData->lastName
    ];
    $user = $modx->newObject('modUser', array ('username'=>$formData->email));
    $user->set('password', $formData->password ? $formData->password : generatePassword());
    $userProfile = $modx->newObject('modUserProfile');
    $userProfile->set('fullname', json_encode($userName));
    $success = $user->addOne($userProfile);
    if ($success) {
        $user->save();
        $newUserId = $user->get('id');
    } else {
        $error = 1;
        $errortext = 'User not created';
    }


    It saves something like {"first_name":"Alex","last_name":"Hoffman"} in fullname cell.

    Than if I load all users as object:

    $c = $modx->newQuery('modUser');
    $c->innerJoin ('modUserProfile','Profile');
    $c->where(array(
    	'active' => true,
    	'primary_group:!=' => 2,
    ));
    $c->select(array(
        'modUser.id',
        'Profile.fullname',
        'Profile.email',
    	'Profile.photo',
    ));
    $c->sortby('Profile.fullname','ASC');
    $c->prepare();
    $c->stmt->execute();
    $users = $c->stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(array("customers" => $users, "success" => "", "error" => ""));


    But it handles fullname as a string. So I do have to iterate through the whole users object inside angularjs to convert fullname fields to objects.
    Is there a way to load this fields as objects direct from MODx?

    Thx

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      • 4172
      • 5,888 Posts
      no, you will need to loop either clientside or serverside thrue the collection.
      Or store firstname,lastname into seperate fields.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 7858
        • 97 Posts
        Thx, Bruno, wanted to make it as easy, as I could, but have to stich with "manual" conversion. Will make it clien-side, i think.
          • 47401
          • 295 Posts
          server side would be more secure smiley
            • 4172
            • 5,888 Posts
            would do it server side, too.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 7858
              • 97 Posts
              ok will do it sever side. Thx!