0

我有这个 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 

我已经在这个模式启动 build --forms 上创建了我的表单,但现在我想将 ProductForm 与 MakeproductForm 合并,因为我希望当我在 ProductForm 中时我也可以输入成分,将它们连接到当前产品.

我尝试按照symfony 网站上的这个示例执行此操作,并且在我的代码中进行了此更改:

MakeproductForm :

class MakeproductForm extends BaseMakeproductForm
{
  public function configure()
  {

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

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

  }
}

产品形式:

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 无法添加或更新子行:外键约束失败(dolcisymfony. makeproduct, CONSTRAINT makeproduct_product_id_product_idFOREIGN KEY ( product_id) REFERENCES product( id))

我确定我遗漏了一些东西,但是按照那个例子我无法解决问题,并且mrge这两种形式:你能帮我,给我一些提示吗?

4

1 回答 1

1

我认为你的问题是关于这条线:

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

我想应该是这样的:

$makeproduct->Product = $this->getObject();
于 2012-06-22T16:47:30.757 回答