Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:29:56+00:00 2026-06-13T17:29:56+00:00

How does one go about creating multiple new users, say in a loop, from

  • 0

How does one go about creating multiple new users, say in a loop, from an array in a different controller?

I have an issue where attempting to create multiple users in a form submit fails, but creating a single new user works as designed. It appears the issue may be when saving the new user and then bringing the new user_id back in the return statement. Although the new id comes back, subsequent users (2nd, 3rd, etc) all get the same id value, and it appears that the subsequent $this->save calls modify the first created user rather than create add’l ones. Any none of the new users appear in the database. (again, the problem only happens when more than one new users will be created.)

My one small clue is that if I var_dump($user) in my importPublicLDAPUser() function (user.php) just after the $user = $this->save(array('User' => array( … ))); then for the first element I see both ‘modified’ and ‘created’, whereas I see only ‘modified’ for the rest. This leads me to believe there’s a step missing, like the user needs to be saved or commit (??) before the next user can be be created.

I tried changing this to $user = $this->User->save(array('group_id' => 3, ... and adding a ‘create’ before: $this->User->create(); but these produce errors ‘Call to a member function save() on a non-object’ and ‘Call to a member function create() on a non-object’.

My application manages documents. Each document can have many authors, so it has controllers for: docs, doc-types, users, groups, and authors.

When a new document is entered, the form allows selection of multiple users to create ‘Author’ records. In addition to the local users table, it also searches our LDAP server (both via auto-sugggest) and also allows input into a text field. So, Authors are selected from

  • the existing table of users
  • via the LDAP helper
  • free text entry.

This result is two arrays: $Authors (from local users tables), and $badAuthors (LDAP and text-input) which the app then tries to add to the local users table when the form is submitted.

The form works just fine if:

  1. one or more authors are added from local users table;
  2. a single author is added from LDAP (succeeds in creating a new entry in users table), and zero or more local users
  3. a single author is added from text input (also succeeds), and zero or more local users

However if two or more non-local users are added ($badAuthors has more than one element) then the form fails. “fails” means that either the Author or User save routine failed, and so it skips the Document commit $this->Docu->commit(); and I spit out an error via ajaxResponse. Thus, the app works as designed, but only with one new User entry at a time, even though the form is designed to allow Authors/badAuthors to be >1.

What I don’t understand is why when I loop through bad authors why it doesn’t correctly add the users if $badAuthors has more than one element.

As the user enters each name (which is checked against the users table and LDAP via ajax helpers, etc) and then selected, an author_name_list array is built. And then:

foreach($this->params['form']['author_name_list'] as $author_name){
    $user_id = $this->Docu->User->field('id',array('User.name' => $author_name));
    if($user_id){
        $Authors['Author'][]=array(
            'docu_id'=>$this->Docu->id
            ,'user_id'=>$user_id
            );
    }else{
        $badAuthors[] = array('name'=>$author_name);
    }
 }

So $badAuthors is now those found in LDAP or entered manually.

So now I try to create/save all badAuthors…

docu controller (docu_controller.php):

if(count($badAuthors)){
foreach($badAuthors as $key => $author){

    $this->Memo->User->create();  // ** <-- this was missing!! **

        if($ldap_author = $this->Docu->User->importPublicLDAPUser($author['name'])){
    unset($badAuthors[$key]);
    $Authors['Author'] []= array(
        'docu_id'=>$this->Docu->id
        ,'user_id'=>$ldap_author['User']['id']
        ,'precedence' => $author['precedence']
    );
    } elseif ($new_author = $this->Docu->User->newReadonlyUser($author['name'])) {
    unset($badAuthors[$key]);
    $Authors['Author'] []= array(
        'docu_id'=>$this->Docu->id
        ,'user_id'=>$new_author['User']['id']
        ,'precedence' => $author['precedence']
    );
    }
}
}

if(!count($badAuthors)){
    $authors_saved = true;
    foreach($Authors['Author'] as $author_arr){
       $this->Docu->Author->create();
       if(!$this->Docu->Author->save(array('Author' => $author_arr))){
            $authors_saved = false;
            break;
       }
    }
}

user model (user.php)

function afterSave($created) {
    if (!$created) {
        $parent = $this->parentNode();
        $parent = $this->node($parent);
        $node = $this->node();
        $aro = $node[0];
        $aro['Aro']['parent_id'] = $parent[0]['Aro']['id'];
        $this->Aro->save($aro);
    }
}

function importPublicLDAPUser($cn){
    App::import('Vendor','adLDAP',array('file'=>'adLDAP.php'));
    $oLDAP = new adLDAP(Configure::read('LDAP_options.email'));
    $oLDAP->authenticate(NULL, NULL);

    $filter = '(&(cn='.$oLDAP->ldap_escape($cn).'))';

    $ldap_res = @$oLDAP->search($filter, array('cn', 'uid','profitcenter'),1);

    if(isset($ldap_res['count']) && ($ldap_res['count'] > 0)){//found it
    $user = $this->save(array('User' => array(
        'group_id' => 3,
        'name' => $ldap_res[0]['cn'][0],
        'username' => $ldap_res[0]['uid'][0],
        'grpnum' => pc2grpnum($ldap_res[0]['profitcenter'][0])
        )));                               

    if($user){
        $user['User']['id'] = $this->id;
    }
        return ($user ? $user : false);
    }else{
        return false;
    }
}

Any suggestions? Thanks!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:29:57+00:00Added an answer on June 13, 2026 at 5:29 pm

    It turns out that in my docu_controller.php I was missing a create() call. It seems that without a create, an object can still be saved/created when the other controller does a commit(). So before adding the create(), prior to the commit, in later loop iterations I was still modifying the original object, not any new ones. By adding a create in the controller, the save in the method function acts on the new user for each loop iteration.

    in controller:

    if(count($badAuthors)){
        foreach($badAuthors as $key => $author){
            $this->Memo->User->create();
                if($ldap_author = $this->Memo->User->importPublicLDAPUser($author['name'])){
    

    in method:

    function importPublicLDAPUser($cn){
        ....
        $user = $this->save(array('User' => array(...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one go about creating two Jars from one project source folder? Is
When creating a new xml file, how does one go about structuring the file
How does one go about catching exceptions from using controls in markup? For example,
I have two questions about creating thread safe types in python, and one related
How does one go about properly analyzing the requirements of a client in terms
I was curious as to how does one go about finding undocumented APIs in
In jQuery, how does one go about finding all the 'unchecked' checked boxes. $(':checkbox:checked');
Does any one know about opensource network monitor tool for BlackBerry ?
i am getting this error does some one knows about it??? 2009-07-08 18:42:36.778 FlashCards[1297:20b]
I have a question about creating and managing events inside an ascx custom web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.