ManyToOne和OneToMany与Symfony2

时间:2015-01-18 22:45:11

标签: forms symfony entity one-to-many many-to-one

我有一家餐馆。 该餐厅可以在七天内举办多场开幕式。

这是我的RESTAURANT_HOURS表:

    | Field           | Type        | Null | Key | Default | Extra          |
+-----------------+-------------+------+-----+---------+----------------+
| ID              | int(11)     | NO   | PRI | NULL    | auto_increment |
| RESTAURANT_ID   | int(11)     | NO   | MUL | NULL    |                |
| DAY_OF_WEEK_ID  | smallint(6) | YES  | MUL | NULL    |                |
| START_TIME      | time        | YES  |     | NULL    |                |
| END_TIME        | time        | YES  |     | NULL    |                |
| CREATED_DATE_TS | bigint(14)  | YES  |     | NULL    |                |
| UPDATED_DATE_TS | bigint(14)  | YES  |     | NULL    |                |
+-----------------+-------------+------+-----+---------+----------------+

这是我的RestaurantEntity(与小时相关)

     /**   
   * @ORM\OneToMany(targetEntity="Hour", mappedBy="restaurant", cascade={"persist","remove"}, fetch="EAGER")   
   * @JMS\Expose
   * @JMS\Groups({"restaurant_full"}) 
   */
  private $hours;

这是我的HourEntity

/**
   * @ORM\ManyToOne(targetEntity="Restaurant", inversedBy="id", cascade={"persist"})
   * @ORM\JoinColumn(name="RESTAURANT_ID", referencedColumnName="ID")
   * @JMS\Expose
   * @JMS\Groups({"restaurant_full"}) 
   */  
  private $restaurant;

这是我的RestaurantForm(与小时相关)

 $builder->add('hours', 'collection', array(
            'type'         => new HourType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype'    => true
        )
    );

这是我的HourForm

    class HourType extends AbstractType {

  private $create;
  private $restRepo;

  public function __construct($create = true) {
    $this->create = $create;
    //$this->restRepo = $restRepo;
  }

  public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->add('startTime', 'text');
    $builder->add('endTime', 'text');
    $builder->add('dayOfWeekId', 'integer');

  }  

  /**
   * (non-PHPdoc)
   * @see \Symfony\Component\Form\AbstractType::setDefaultOptions()
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $defaultOptions = array(
      'data_class'      => 'Leapset\Bundle\APIBundle\Entity\Restaurant\Hour',
      'csrf_protection' => false,
      'method'          => $this->create?'POST':'PUT',
      'extra_fields_message' => 'Opening Hours data should not contain extra fields "{{ extra_fields }}".'
    );

    $resolver->setDefaults($defaultOptions);    
  }

  public function getName() {
    return 'hours';
  }
}

以下是我发送到API服务器的示例json正文

{
"restaurant": {
    "isValid": true,
    "id": 759,
    "salesforceId": "Unassigned",
    "name": "The r Cafe",
    "description": "we are open 24 hours 365dqy",
    "phone": "(xxx) xxx-xxx",
    "address1": "91 Street",
    "city": "San Carlos",
    "state": "CA",
    "zipCode": "94070",
    "timeZone": "America/Los_Angeles",
    "hasMenu": true,
    "isPublished": false,
    "email": "xyz@hh.net",
    "lat": 47.5017808,
    "lon": -1522.2552368,        
    "status": 1,        

    "hours": [
      {
            "dayOfWeekId": 0,
            "startTime": "01:00:00",
            "endTime": "12:00:00"
        },
        {
            "dayOfWeekId": 0,
            "startTime": "08:00:00",
            "endTime": "10:00:00"
        },
        {
            "dayOfWeekId": 1,
            "startTime": "01:00:00",
            "endTime": "12:00:00"
        },
        {
            "dayOfWeekId": 2,
            "startTime": "01:00:00",
            "endTime": "12:00:00"
        }
    ]
}

}

但我一直都会收到此错误。

"An exception occurred while executing 'INSERT INTO RESTAURANT_HOURS (DAY_OF_WEEK_ID, START_TIME, END_TIME, RESTAURANT_ID) VALUES (?, ?, ?, ?)' with params [0, "01:00:00", "12:00:00", null]:\ \ SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'RESTAURANT_ID' cannot be null"

有人可以帮我这个。为什么它不绑定restaurant_id?

0 个答案:

没有答案