symfony - 试图合并两个表单,但我得到一个约束错误

时间:2012-06-12 13:46:35

标签: php symfony1 symfony-1.4 symfony-forms

我认为这是schema.yml

Makeproduct:  
  actAs: 
    Timestampable: ~ 
  columns:  
    product_id:   { type: integer(8), notnull: true }  
    ingredient_id: { type: integer(8), notnull: true }  
    ingredient_quantity: { type: integer(8), notnull: true } 
    measure_id: { type: integer(8), notnull: false } 
   relations:  
    Product:
      local: product_id
      foreign: id
      foreignAlias: ProductIngredients 
    Ingredient:
      local: ingredient_id
      foreign: id
      foreignAlias: ProductIngredients 
    Measure:  
      local:         measure_id  
      foreign:       id 

Ingredient:
  actAs:
    I18n:
      fields: [name]
  columns:
    name: { type: string(50), notnull:true}
    price: { type: decimal, notnull:true}

Category:
  actAs:
    I18n:
      fields: [name]
  columns:
    name: { type: string(50), notnull:true}
  relations: 
    ProductsCategory:  
      class:        Product
      local:        id
      foreign:     category_id 
      type:         many 

Measure:
  columns:
    name: { type: string(2), notnull:true}
  relations: 
    ProductsCategory:  
      class:        Makeproduct
      local:        id
      foreign:     measure_id 
      type:         many 

Product:
  actAs:
    I18n:
      fields: [name, description]
      actAs:
        Sluggable: { fields: [name], uniqueBy: [lang, name] } 
  columns:
    category_id:   { type: integer(8), notnull: true }  
    name: { type: string(50), notnull:true}
    pic: { type: string(150)}
    description: { type: clob}
    price: { type: decimal, notnull:true}
  relations: 
    Ingredients:
      class: Ingredient 
      foreignAlias: Products 
      refClass: Makeproduct 
      local: product_id
      foreign: ingredient_id
    Category:  
      local:         category_id  
      foreign:       id 

我已经在这个模式启动构建中创建了表单 - 但现在我想将ProductForm与MakeproductForm合并,因为当我在ProductForm中时我会喜欢这样,我也可以输入成分,连接它们到目前的产品。

我已经尝试在symfony网站上的this example之后执行此操作,并且我在代码中进行了此更改:

MakeproductForm:

class MakeproductForm extends BaseMakeproductForm
{
  public function configure()
  {

  unset($this['created_at'], $this['updated_at']);

  $this->useFields(array('ingredient_id', 'ingredient_quantity', 'measure_id'));

  }
}

ProductForm:

class ProductForm extends BaseProductForm
    {
      public function configure()
      {

          $this->setWidgets(array(
          'id'               => new sfWidgetFormInputHidden(),
          'category_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Category'), 'add_empty' => false)),
          'pic'    => new sfWidgetFormInputFile(),
          'price'            => new sfWidgetFormInputText(),
          //'ingredients_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Ingredient')),      
        ));  

        $this->setValidators(array(
          'id'               => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
          'category_id'      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Category'))),
          'pic'    => new sfValidatorFile((array('mime_types' => 'web_images','path' => sfConfig::get('sf_web_dir').'/images/', 'required' => false))),
          'price'            => new sfValidatorNumber(),
          //'ingredients_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Ingredient', 'required' => false)),    
        ));

        $this->widgetSchema->setNameFormat('product[%s]');

        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);    

        $this->embedI18n(array('en', 'it'));
        $this->widgetSchema->setLabel('en', 'English');
        $this->widgetSchema->setLabel('it', 'Italian');

        //$this->mergeForm(new MakeproductForm(Doctrine::getTable('Makeproduct')->find($this->getObject()->getId())));


      $subForm = new sfForm();
      for ($i = 0; $i < 2; $i++)
      {
        $makeproduct = new Makeproduct();
        $makeproduct->Ingredient = $this->getObject();

        $form = new MakeproductForm($makeproduct);

        $subForm->embedForm($i, $form);
      }
      $this->embedForm('newIngredients', $subForm);


      }

现在的问题是它无法识别产品的ID,并且给了我一个costraint错误:

  

SQLSTATE [23000]:完整性约束违规:1452无法添加或   更新子行:外键约束失败   (dolcisymfonymakeproduct,CONSTRAINT   makeproduct_product_id_product_id外键(product_id)   参考productid))

我确信我错过了一些东西,但是按照那个例子,我无法解决问题,并且提出了这两种形式:请你帮助我,给我一些提示?

1 个答案:

答案 0 :(得分:1)

我认为你的问题在于这一行:

$makeproduct->Ingredient = $this->getObject();

我想应该是这样的:

$makeproduct->Product = $this->getObject();