We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42128
    • 30 Posts
    Dharmesh Mandaliya Reply #1, 9 years, 4 months ago
    Hello Everyone,

    I have created new custom table in database like Jobs. After i have created Form for insert job for particular user using FormIT component. It's work fine for me. But now how can i update job for particular user using FORMIT?

    Here are the steps for insert Form

    Chunk : postJobChunk

    [[!FormIt?
    &hooks=`email,postJobSnippet`
    &emailTpl=`postJobEmailChunk`
    &emailTo=`[email protected]`
    &successMessage=`Thanks for Posting Job. Your Job is awaiting moderation.`
    ]]
    [[!+fi.successMessage]]

    <form method="post" action="[[~[[*id]]]]" name="JobForm">
    <legend>Company profile</legend>
    <div class="row">
    <div class="col-xs-12 col-md-6">
    <div class="form-group">
    <label for="company_name">Company name</label>
    <input type="hidden" name="user_id" value="[[+modx.user.id]]">
    <input type="text" name="company_name" value="[[!+fi.company_name]]" class="form-control input-lg">
    <small class="help-block">Example block-level help text here.</small>
    </div>
    </div>
    <div class="col-xs-12 col-md-6">
    <div class="form-group">
    <label for="company_website">Website</label>
    <input type="url" name="company_website" value="[[!+fi.company_website]]" class="form-control input-lg">
    <small class="help-block">Example block-level help text here.</small>
    </div>
    </div>
    </div>
    <div class="row">
    <div class="col-xs-12 col-md-6">
    <div class="form-group">
    <label>Description</label>
    <textarea rows="5" name="job_description" value="[[!+fi.job_description]]" class="form-control"></textarea>
    <small class="help-block">Example block-level help text here.</small>
    </div>
    </div>
    <div class="col-xs-12 col-md-6">
    <div class="form-group">
    <label>Additional details</label> <span><small>e.g. requirements</small></span>
    <textarea rows="5" name="job_detail" value="[[!+fi.job_detail]]" class="form-control"></textarea>
    <small class="help-block">Example block-level help text here.</small>
    </div>
    </div>
    </div>


    <legend>Additional information for applicant</legend>
    <div class="form-group">
    <label>Message</label>
    <textarea rows="3" name="job_message" value="[[!+fi.job_message]]" class="form-control"></textarea>
    <small class="help-block">Example block-level help text here.</small>
    </div>


    <button type="submit" class="btn btn-primary input-lg">Submit post</button>
    </form>

    Snippet: postJobSnippet

    <?php
    /**
    * mytest table
    */
    $output = '';// this is what the snippet will return

    // add package so xpdo can be used:
    $package_path = $modx->getOption('core_path').'components/modxJobs/model/';
    // see the scheme file and the xml model element and you will see the attribute package and that must match here
    $modx->addPackage('modxJobs', $package_path);

    // lets add some data!
    // see the scheme file and the xml object element and you will see the attribute class and that must match here
    // the class name is taken from table names without the prefixed, and is capitalized.
    $myRow = $modx->newObject('Jobs');

    $user_id = $hook->getValue('user_id');
    $company_name = $hook->getValue('company_name');
    $company_website = $hook->getValue('company_website');
    $company_phone = $hook->getValue('company_phone');
    $company_email = $hook->getValue('company_email');
    $company_address = $hook->getValue('company_address');
    $country = $hook->getValue('country');
    $company_detail = $hook->getValue('company_detail');
    $job_title = $hook->getValue('job_title');
    $job_category = $hook->getValue('job_category');
    $job_type = $hook->getValue('job_type');
    $job_overview = $hook->getValue('job_overview');
    $job_description = $hook->getValue('job_description');
    $job_detail = $hook->getValue('job_detail');
    $job_message = $hook->getValue('job_message');
    $now = new DateTime();

    //$job_types = implode('||' , $job_type);

    $createdon = $now->format('Y-m-d');
    $data = array(
    'user_id' => $user_id,
    'company_name' => $company_name,
    'company_website' => $company_website,
    'company_phone' => $company_phone,
    'company_email' => $company_email,
    'company_address' => $company_address,
    'country' => $country,
    'company_detail' => $company_detail,
    'job_title' => $job_title,
    'job_category' => $job_category,
    'job_type' => $job_type,
    'job_overview' => $job_overview,
    'job_description' => $job_description,
    'job_detail' => $job_detail,
    'job_message' => $job_message,
    'createdon' => $createdon
    );
    $myRow->fromArray($data);

    if ( !$myRow->save() ) {
    $output .= '<p>Could not create row</p>';
    } else {
    $output .= '<p>Created row successfully</p>';
    }

    return $output;

    Please give me answer my above question.
      Thanks & Regards,
      Dharmesh Mandaliya
      Modx Expert
      Mail : [email protected]
      Skype : dharmesh.square1
      Linkedin : http://in.linkedin.com/in/dharmeshmandaliya
      Website : http://www.squareonewebsolutions.com/modx-development.php
      • 49529
      • 196 Posts
      Dharmesh Mandaliya,

      You can try the following code in your snippet to fetch and modify already existing row for particular user:

      $myRow = $modx->getObject('Jobs', array( // searching for row for particular user
          'user_id' => $user_id
      ));
      
      if (! is_object($myRow)) { // nothing found - creating new one
        $myRow = $modx->newObject('Jobs');
      }
      
      // manipulations with $myRow
      

      BTW, you can use more convenient method getValues instead of getting every single value:
      $fields = $hook->getValues();
      $now = new DateTime();
      $fields['createdon'] = $now->format('Y-m-d');
      $myRow->fromArray($fields);
      
      

        • 42128
        • 30 Posts
        Dharmesh Mandaliya Reply #3, 9 years, 3 months ago
        @whitebyte : Yeah I have solved my problem using your code. Thanks a lot.
          Thanks & Regards,
          Dharmesh Mandaliya
          Modx Expert
          Mail : [email protected]
          Skype : dharmesh.square1
          Linkedin : http://in.linkedin.com/in/dharmeshmandaliya
          Website : http://www.squareonewebsolutions.com/modx-development.php