I cannot get DB values of games to be selected:
Game:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
game_name: { type: string(100), notnull: true }
logo: { type: string(100), notnull: true, comment: "Game Logo" }
indexes:
it:
fields: game_name
type: unique
Campaign:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
name: { type: string(100), notnull: true }
percentage: { type: integer(4), notnull: true, unsigned: true }
is_active: { type: integer(1), notnull: true, unsigned: true }
start: { type: datetime, notnull: true }
end: { type: datetime, notnull: true }
CampaignGames:
actAs:
Timestampable: ~
columns:
id: { type: integer(4), primary: true, autoincrement: true, unsigned: true }
campaign_id: { type: integer(4), notnull: true, unsigned: true }
game_id: { type: integer(4), notnull: true, unsigned: true }
indexes:
tc:
fields: [campaign_id, game_id]
type: unique
relations:
Campaign: { onDelete: CASCADE, local: campaign_id, foreign: id, foreignAlias: CampaignCampaignGames }
Game: { onDelete: CASCADE, local: game_id, foreign: id, foreignAlias: GameCampaignGames }
I have added games checkbox here which belongs to Game model to let the user add games to CampaignGames, but unfortunately they never checked… And these values are present in DB.
class AdminconsoleCampaignForm extends CampaignForm
{
public function configure()
{
parent::configure();
$this->widgetSchema['is_active'] = new sfWidgetFormSelectRadio(array(
'choices' => array(1 => 'On', 0 => 'Off'),
));
$games = Doctrine_Core::getTable('Game')->getGames();
$this->widgetSchema['game_id'] = new sfWidgetFormSelectCheckbox(array(
'choices' => $games
));
$this->validatorSchema['game_id'] = new sfValidatorChoice(array(
'choices' => array_keys($games)
, 'multiple' => true
, 'required' => false
));
$this->removeFields();
}
Also tried to use
$this->widgetSchema['game_id']->setDefault(array($data));
No luck. How to resolve it? I’m really stuck on that.
There are two things that caught my attention:
1. You’re not using Doctrine’s
booleandata typeTry changing your
schema.ymlto the following:This way Symfony/Doctrine will take care of anything regarding the
is_activerow of yourCampaignrecord.If you now rebuild your model your
BaseCampaignForm.class.phpwill define theis_activewidget automatically like this:Note: That
requiredis set tofalseis there because if a checkbox isn’t selected it’s not posted either. ThesfValidatorBooleantakes care of this and disables the value all by itself. If you would set it to true than the user wouldn’t be able to uncheck the box and submit the form without a validator exception.2. You tried to set form object defaults in its form’s widget
In your code you used:
This won’t work because you’re using a Form with an object attached to it (a
BaseFormDoctrine). All the default values are taken right out of the object assigned to that form (in your case aCampaignobject because you’re extendingCampaignForm).(Major pitfall) If you want to set default values on a form object you have to set them on the form object itself :
Default object values can’t be set using the object form’s widgets. These default values will always be overwritten by the actual object values (which makes sense because the form represents the object).
Glad if I was able to help you in some way.