2

Let's say I have a section 'foo', with following fields and slug:

First Name (name)

Last Name (lname)

Plan (plan)

Cost (cost)

The field cost is dynamically created by a function already present in controller. I tried the following:

public function actionGetCost()
{
    $fname = craft()->request->getParam('name');
    $lname = craft()->request->getParam('lname');
    $plan = craft()->request->getParam('plan');

    ...

    $entry = new EntryModel();
    $entry->sectionId = 16;
    date_default_timezone_set('America/New_York');
    $date = date('n-j-Y') ;
    $entry->postDate = $date;
    $entry->authorId = 1;
    $entry->enabled = true;
    $entry->setContentFromPost([
       'name' => $fname,
       'lname' => $lname,
       'plan' => $plan,       
       'cost' => $cost->id,
    ]);

    $save = craft()->entries->saveEntry($entry);

    if ($save){
      echo "saved" !
    }else{
      echo "<pre>";
      print_r($entry()->getAllErrors());
      echo "</pre>";
    }
}

What's wrong in the code? How can I do it?

1
  • 1
    Have you checked out the Guest Entries plugin? If you go into the controllers and service files that could give you some insight.
    – Damon
    Jun 28 '17 at 15:24
1

As Damon mentions, I'd highly recommend checking out the Guest Entries plugin.

Specifically the private _populateEntryModel() method which creates the EntryModel class and gives a good example of the properties that are necessary to set to before calling EntriesService->saveEntry().

1
  • Thanks that helped me !! I have updated the question with the correct working code. Jun 29 '17 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.