Ask your Symfony questions! Pay money and get answers fast! (more info)

How do I save an array in a form that points to another model? Symfony

  • SOLVED

Symfony 1.4 using Propel 1.6.

I have a deals table. The deals might only occur on certain days of the week. So we also have a weekdays table, which only has 7 rows, for the 7 days of the week, and then we have a deal_weekday table, which maps the many-to-many relationship. This is the 3 tables in the schema.yml:

deal:
id:
app_id: { type: integer, required: true, foreignTable: app, foreignReference: id, onDelete: cascade }
title: varchar(255)
deal: varchar(255)
original_price: integer
discounted_price: integer
starts_at: timestamp
ends_at: timestamp
place_id: { type: integer, required: false, foreignTable: place, foreignReference: id, onDelete: cascade }
limited_to: integer
is_recurring: boolean
push_before_sec: integer
push_text: longvarchar
image: varchar(255)
description: longvarchar
notification_id: { type: integer, required: false, foreignTable: notification, foreignReference: id, onDelete: cascade }



weekday:
id:
name: varchar(255)



deal_weekday:
weekday_id: { type: integer, primaryKey: true, required: true, foreignTable: weekday, foreignReference: id, onDelete: cascade }
deal_id: { type: integer, primaryKey: true, required: true, foreignTable: deal, foreignReference: id, onDelete: cascade }
created_at:




We wanted the 7 weekdays to appear as 7 checkboxes in the deals form. I got this working by doing:

$this->setWidget('weekday_list', new sfWidgetFormPropelChoice(array('multiple' => true, 'expanded' => true, 'model' => 'Weekday')));


So far, so good. But when I save this form, nothing gets saved.

The easiest way for me to do this would be to go into a for() loop and loop over the 7 items and save each one (assuming someone created a deal that is available 6 days a week, that should become 6 separate rows in deal_weekday). But I am wondering if there is a function that will do all this for me in a single line?

Right now the HTML in the form looks like this:

<div class="content-input"><ul class="checkbox_list"><li><input name="deal[weekday_list][]" type="checkbox" value="1" id="deal_weekday_list_1" /> <label for="deal_weekday_list_1">Mon</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="2" id="deal_weekday_list_2" /> <label for="deal_weekday_list_2">Tue</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="3" id="deal_weekday_list_3" /> <label for="deal_weekday_list_3">Wed</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="4" id="deal_weekday_list_4" /> <label for="deal_weekday_list_4">Thu</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="5" id="deal_weekday_list_5" /> <label for="deal_weekday_list_5">Fri</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="6" id="deal_weekday_list_6" /> <label for="deal_weekday_list_6">Sat</label></li>
<li><input name="deal[weekday_list][]" type="checkbox" value="7" id="deal_weekday_list_7" /> <label for="deal_weekday_list_7">Sun</label></li></ul></div>


I can loop over this in my form code and save this, but I'm wondering if there is a method that will do this for me?

Answers (1)

2012-11-15

mullerivan answers:

yo can use the dosave in the form\


public function doSave($con = null){
parent::doSave($con);
foreach (this->getValue('weekday_list') as $days)
{
$deal_weekday = new DealWeekday();
$deal_weekday->setDayId($day);
#$this->getObject() is a deal object
$deal_weekday->setDeal($this->getObject());
$deal_weekday->save($con)
}

}




Whith embebed forms the framework make this for you ;)

Show the BasesfGuardUserForm
Aqui! [[LINK href="http://pastebin.com/CryuRR6g"]][[/LINK]]


Lawrence Krubner comments:

Yes, I know. I was wondering if there was a method that does this for me? I guess maybe this is the best option? It seems a little clunky, especially given all the form functionality in Symfony 1.4.


Lawrence Krubner comments:

Well, this worked, so I'll stick with this.