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 = date('n-j-Y') ;
    $entry->postDate = $date;
    $entry->authorId = 1;
    $entry->enabled = true;
       'name' => $fname,
       'lname' => $lname,
       'plan' => $plan,       
       'cost' => $cost->id,

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

    if ($save){
      echo "saved" !
      echo "<pre>";
      echo "</pre>";

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

  • 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
    Commented Jun 28, 2017 at 15:24

1 Answer 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().

  • Thanks that helped me !! I have updated the question with the correct working code. Commented Jun 29, 2017 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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