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?