satboy78
satboy78

Reputation: 317

symfony - trying to merge two forms but I get a constraint error

I've goth this 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 

I've created my forms on this schema launching build --forms but now I'd like to merge ProductForm with MakeproductForm, because I'd like that when I'm in ProductForm I could enter ingredients as well, connecting them to current product.

I've tried to do this following this example present on symfony website, and I've made this change in my code:

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);


      }

The problem now is that it doesn't recognize the id of the product, and gives me a costraint error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (dolcisymfony.makeproduct, CONSTRAINT makeproduct_product_id_product_id FOREIGN KEY (product_id) REFERENCES product (id))

I'm sure that I'm missing something, but following that example I'm unable to solve the problem, and mrge these two forms: could you please help me, giving me some hints?

Upvotes: 0

Views: 253

Answers (1)

glerendegui
glerendegui

Reputation: 1507

I think your problem it's about this line:

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

I guess should looks like:

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

Upvotes: 1

Related Questions