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

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

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?

This question has been answered.

Lawrence Krubner | 11/15/12 at 4:08pm Edit


(3) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    11/15/12
    4:47pm
    mullerivan says:

    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!

    Previous versions of this answer: 11/15/12 at 4:20pm | 11/15/12 at 4:42pm | 11/15/12 at 4:42pm | 11/15/12 at 4:45pm | 11/15/12 at 4:47pm

  • avatar
    Last edited:
    11/15/12
    4:28pm
    Lawrence Krubner says:

    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.

  • avatar
    Last edited:
    11/15/12
    4:34pm
    Lawrence Krubner says:

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

This question has expired.



Lawrence Krubner had additional discourse to offer.

Lawrence Krubner voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.