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

How to show many-to-many relationship as checkboxes? Symfony

Symfony 1.4 using Propel.

If I look in BasesfGuardPermissionForm.class.php I see this:

abstract class BasesfGuardPermissionForm extends BaseFormPropel
{
public function setup()
{
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'name' => new sfWidgetFormInputText(),
'description' => new sfWidgetFormTextarea(),
'sf_guard_group_permission_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardGroup')),
'sf_guard_user_permission_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
));


For sf_guard_user_permission_list, this seems to manage a many-to-many relationship and show it as multiple choice. I'd like to do something similar, but render the choices as checkboxes.

I have a deal model that looks like this:


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 }



There is also a database table called 'weekday' and this table has just 7 rows in it, with the 7 days of the week. In between those 2 models is a "deal_weekday" model:

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:


When I look at the form for the Deals, I need to also have the 7 weekdays appear as checkboxes. How do I do this?

Answers (2)

2012-11-14

mullerivan answers:

In the form try this




$this->setWidget('WIDGET_NAME_RELATION' ,new sfWidgetFormPropelChoice(array('model' => 'WeekDay')),

$this->getWidget('WIDGET_NAME_RELATION')->setOption('expanded',true);
$this->getWidget('WIDGET_NAME_RELATION')->setOption('multiple',true);

2012-11-14

Milena Dimitrova answers:

You can add this to the configure() method in your DealForm class:

$weekdays = WeekdayPeer::doSelect(new Criteria());
$weekdayChoices = array();
foreach ($weekdays as $weekday)
{
$weekdayChoices[$weekday->getId()] = $weekday->getName();
}
$this->setWidget('deal_weekdays', new sfWidgetFormSelectCheckbox(array('choices' =>$weekdayChoices )));
$this->setValidator('deal_weekdays', new sfValidatorPass());



Once added, do not forget to also add the new widget 'deal_weekdays' to appear by adding it in generator.yml for actions 'edit' and 'new'.

Note: This will make the checkboxes appear but you will have to save them automatically.