未选中复选框

Rom*_*aza 0 symfony1 symfony-1.4

我无法获得游戏的DB值:

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 }
Run Code Online (Sandbox Code Playgroud)

我在这里添加了游戏复选框,属于Game模型,让用户将游戏添加到CampaignGames,但不幸的是他们从未检查过...而且这些值存在于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();

}
Run Code Online (Sandbox Code Playgroud)

也尝试使用

$this->widgetSchema['game_id']->setDefault(array($data));
Run Code Online (Sandbox Code Playgroud)

没运气.怎么解决?我真的很困惑.

flu*_*flu 6

有两件事引起了我的注意:

1.您没有使用Doctrine的boolean数据类型

尝试将您更改schema.yml为以下内容:

Campaign:
  [...]
  columns:
    [...]
    is_active:
      type: boolean
      notnull: true
      default: 0 # Or whichever default value you prefer
    [...]
Run Code Online (Sandbox Code Playgroud)

这样,Symfony/Doctrine将处理有关记录is_active行的任何事情Campaign.

如果您现在重建模型,您BaseCampaignForm.class.phpis_active自动定义窗口小部件,如下所示:

$this->setWidgets(array(
  [...]
  'is_active' => new sfWidgetFormInputCheckbox(),
  [...]
);

$this->setValidators(array(
  [...]
  'ist_active' => new sfValidatorBoolean(array('required' => false)),
  [...]
);
Run Code Online (Sandbox Code Playgroud)

注:required设置为false有,因为如果没有选择复选框,它没有公布任何.该sfValidatorBoolean注意到了这一问题,并通过自身的所有禁用的价值.如果您将其设置为true,则用户将无法取消选中该框并在没有验证程序异常的情况下提交表单.

2.您尝试在其窗体的窗口小部件中设置窗体对象默认值

在您使用的代码中:

$this->widgetSchema['game_id']->setDefault(array($data));
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为您正在使用附加了对象的表单(a BaseFormDoctrine).所有默认值都是从分配给该表单的对象中取出的(在您的情况下是一个Campaign对象,因为您正在扩展CampaignForm).

(主要缺陷)如果要在表单对象上设置默认值,则必须在表单对象本身上设置它们:

$this->getObject()->setGameId($id);
Run Code Online (Sandbox Code Playgroud)

无法使用对象表单的小部件设置默认对象值.这些默认值将始终被实际对象值覆盖(这是有道理的,因为表单代表对象).

很高兴,如果我能以某种方式帮助你.