2017-05-04 46 views
2

我在這裏有一個有線情況。堅持symfony插入多個記錄而不是一個。我找不到我的代碼有任何問題,我不知道如何調試這個錯誤,因爲它似乎一切正常。symfony插入多行insted的一個

應用程序後面的邏輯是用戶可以選擇兩個公交車站並創建一條路線。他還需要定義價格和組(最小和最大人數)。此外,用戶需要選擇將驅動該特定路線的公交車輛

是否有人知道問題在哪裏?

這裏是實體的,當表單提交

StationStandardPrice {#553 ▼ 
    -id: null 
    -company: Company {#549 ▶} 
    -busVehicleGroupSize: BusVehicleGroupSize {#1233 ▶} 
    -departureStation: Stations {#1247 ▶} 
    -destinationStation: Stations {#1236 ▶} 
    -currency: Currencies {#1015 ▶ …2} 
    -price: 99.0 
    -busVehicles: ArrayCollection {#554 ▼ 
    -elements: array:2 [▼ 
     0 => BusVehicle {#1023 ▶} 
     1 => BusVehicle {#1208 ▶} 
    ] 
    } 
} 

這是路由的實體(短路版本)的輸出

/** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var \AppBundle\Entity\Company 
    * 
    * @ORM\ManyToOne(targetEntity="Company") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="company_id", referencedColumnName="id") 
    * }) 
    */ 

    private $company; 

    /** 
    * @var \AppBundle\Entity\BusVehicleGroupSize 
    * 
    * @ORM\ManyToOne(targetEntity="BusVehicleGroupSize") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="bus_vehicle_group_size_id", referencedColumnName="id") 
    * }) 
    */ 
    private $busVehicleGroupSize; 

    /** 
    * @var \AppBundle\Entity\Stations 
    * 
    * @ORM\ManyToOne(targetEntity="Stations") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="departure_station_id", referencedColumnName="id") 
    * }) 
    */ 
    private $departureStation; 

    /** 
    * @var \AppBundle\Entity\Stations 
    * 
    * @ORM\ManyToOne(targetEntity="Stations") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="destination_station_id", referencedColumnName="id") 
    * }) 
    */ 
    private $destinationStation; 

    /** 
    * @var \AppBundle\Entity\Currencies 
    * 
    * @ORM\ManyToOne(targetEntity="Currencies") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="currency_id", referencedColumnName="id") 
    * }) 
    */ 

    private $currency; 

    /** 
    * @var float 
    * 
    * @ORM\Column(name="price", type="decimal", precision=10, scale=2, nullable=false) 
    */ 

    private $price; 

    /** 
    * Many groups can have many bus vehicles 
    * 
    * @ORM\ManyToMany(targetEntity="BusVehicle", inversedBy="busVehicleGroup") 
    * @ORM\JoinTable(name="standard_station_price_bus_groups", 
    *  joinColumns={@ORM\JoinColumn(name="station_standard_price_id", referencedColumnName="id")}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="bus_vehicle_id", referencedColumnName="id")} 
    *) 
    **/ 

    private $busVehicles; 

    /** 
* Add bus vehicle 
* @param BusVehicle $busVehicles 
*/ 
public function addBusVehicles(BusVehicle $busVehicles) 
{ 
    if ($this->busVehicles->contains($busVehicles)) { 
     return; 
    } 
    $this->busVehicles->add($busVehicles); 
    $busVehicles->addBusVehicleGroup($this); 
} 



    /** 
    * Remove bus vehicle 
    * @param BusVehicle $busVehicles 
    */ 
public function removeBusVehicles(BusVehicle $busVehicles) 
{ 
    if (!$this->busVehicles->contains($busVehicles)) { 
     return; 
    } 
    $this->busVehicles->remove($busVehicles); 
    $busVehicles->removeBusVehicleGroup($this); 
} 

控制器:

/** 
    * Creates a new stationStandardPrice entity. 
    * @Template 
    */ 
    public function newAction(Request $request) 
    { 
     $stationStandardPrice = new Stationstandardprice(); 
     $form = $this->createForm(StationStandardPriceType::class, $stationStandardPrice, array(
       'user' => $this->getUser() 
      ) 
     ); 
     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 
      dump($stationStandardPrice); 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($stationStandardPrice); 
      $em->flush(); 

      $this->addFlash('success', 'admin.stationstandardprice.created'); 
      //return $this->redirectToRoute('stationstandardprice_show', array('id' => $stationStandardPrice->getId())); 
     } 

     return [ 
      'stationStandardPrice' => $stationStandardPrice, 
      'form' => $form->createView(), 
     ]; 
    } 

數據庫輸出:

After persisting values

多對多表狀態:

Many to many table output

編輯1:增加枝杈

{% extends 'AdminBundle::layout.html.twig' %} 
{% block stylesheets %} 
    {{ parent() }} 

    <link href="{{ asset('resources/public/css/datatables.min.css', 'busrent_admin') }}" rel="stylesheet"/> 
    <link href="{{ asset('resources/public/css/smart_wizard.min.css', 'busrent_admin') }}" rel="stylesheet"/> 
    <link href="{{ asset('resources/public/css/smart_wizard_theme_dots.min.css', 'busrent_admin') }}" rel="stylesheet"/> 
{% endblock %} 
{% block title %} {{ 'admin.stationstandardprice.new.title'|trans }} {% endblock %} 

{% block breadcrumb %} 
    <div class="col-lg-10"> 
     <h2>{{ 'admin.stationstandardprice.new.title'|trans }}</h2> 
     <ol class="breadcrumb"> 
      <li> 
       <a href="{{ path('dashboard') }}">{{ 'admin.dashboard.index.title'|trans }} </a> 
      </li> 
      <li> 
       <a href="{{ path('stationstandardprice_index') }}">{{ 'admin.stationstandardprice.index.title'|trans }} </a> 
      </li> 
      <li class="active"> 
       <strong>{{ 'admin.stationstandardprice.new.title'|trans }}</strong> 
      </li> 
     </ol> 
    </div> 
    <div class="col-lg-2"> 

    </div> 
{% endblock %} 

{% block content %} 

    <div class="wrapper wrapper-content animated fadeInRight"> 

     <div class="row"> 
      <div class="col-lg-12"> 
       <div class="ibox float-e-margins"> 
        <div class="ibox-title"> 
         <h5>{{ 'admin.stationstandardprice.new.title'|trans }}</h5> 
         <div class="ibox-tools"> 
          <a class="collapse-link"> 
           <i class="fa fa-chevron-up"></i> 
          </a> 

         </div> 

        </div> 
        <div class="ibox-content"> 
         <div class="row"> 
          <div class="col-lg-12"> 

           {{ form_start(form) }} 

           <div id="smartwizard"> 
            <ul> 
             <li><a href="#step-1">Step Title<br/> 
               <small>Step description</small> 
              </a> 
             </li> 
             <li><a href="#step-2">Step Title<br/> 
               <small>Step description</small> 
              </a> 
             </li> 
             <li><a href="#step-3">Step Title<br/> 
               <small>Step description</small> 
              </a> 
             </li> 
            </ul> 

            <div> 
             <div id="step-1" class=""> 
              {{ form_row(form.departureStation) }} 
              {{ form_row(form.destinationStation) }} 
             </div> 
             <div id="step-2" class=""> 
              {{ form_row(form.busVehicleGroupSize) }} 
              {{ form_row(form.price) }} 
              {{ form_row(form.currency) }} 
             </div> 
             <div id="step-3" class=""> 
              {% if app.user.company is not null %} 
               <div class="ibox-content"> 

                <table id="busVehicleTable" class="table table-striped"> 
                 <thead> 
                 <tr> 
                  <th>{{ 'admin.busvehicle.form.licencePlate'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.chassisNumber'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.passengerSeatsNumber'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.busType'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.emissionClass'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.fullDayPrice'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.halfDayPrice'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.pricePerKm'|trans }}</th> 
                  <th>{{ 'admin.busvehicle.form.amenities'|trans }}</th> 
                  <th>{{ 'actions'|trans }}</th> 
                 </tr> 
                 </thead> 
                 <tbody> 
                 {% set inc = 0 %} 
                 {% for busVehicle in app.user.company.busVehicle %} 

                  <tr> 
                   <td>{{ busVehicle.licencePlate }}</td> 
                   <td>{{ busVehicle.chassisNumber }}</td> 
                   <td>{{ busVehicle.passengerSeatsNumber }}</td> 
                   <td>{{ busVehicle.busType.type }}</td> 
                   <td>{{ busVehicle.emissionClass.name }}</td> 

                   <td>{{ busVehicle.fullDayPrice }}</td> 
                   <td>{{ busVehicle.halfDayPrice }}</td> 
                   <td>{{ busVehicle.pricePerKm }}</td> 

                   <td> 
                    <i style="cursor: pointer;" class="fa fa-search" 
                     aria-hidden="true" 
                     data-toggle="collapse" 
                     data-target="#amenities{{ inc }}"></i> 
                   </td> 
                   <td> 
                    <div id="addBusVehicleDiv{{ busVehicle.id }}"> 
                     <button data-id="{{ busVehicle.id }}" href="#" 
                       class="addBusVehicle btn btn-primary btn-sm"> 
                      <i class="fa fa-plus"></i> 
                      <span class="bold"> Add bus vehicle</span> 
                     </button> 

                    </div> 

                    <div id="removeBusVehicleDiv{{ busVehicle.id }}" 
                     style="display: none;"> 
                     <div id="removeBusVehicleDiv{{ busVehicle.id }}"> 
                      <button data-id="{{ busVehicle.id }}" 
                        href="#" 
                        class="removeBusVehicle btn btn-danger btn-sm"> 
                       <i class="fa fa-times"></i> 
                       <span class="bold"> Remove bus vehicle</span> 
                      </button> 

                     </div> 
                    </div> 
                   </td> 

                  </tr> 
                  <tr id="amenities{{ inc }}" class="collapse"> 
                   <td> 
                    >{{ 'admin.busvehicle.form.amenities'|trans }}: 
                   </td> 
                   <td> 
                    <div> 
                     <p> 
                      {% for busAmenity in busVehicle.busVehiclesAmenities %} 
                       {% if loop.last %} 
                        {{ busAmenity.name }} 
                       {% else %} 
                        {{ busAmenity.name }}, 
                       {% endif %} 

                      {% endfor %} 
                     </p> 
                    </div> 
                   </td> 
                  </tr> 

                  {% set inc = inc + 1 %} 
                 {% endfor %} 
                 </tbody> 
                </table> 

               </div> 
              {% endif %} 
             </div> 
            </div> 
           </div> 

           {{ form_end(form) }} 


          </div> 


         </div> 
        </div> 

       </div> 
      </div> 
     </div> 
    </div> 

{% endblock %} 

{% block javascripts %} 
    <script src="{{ asset('resources/public/js/jquery.smartWizard.min.js', 'busrent_admin') }}"></script> 
    <script src="{{ asset('resources/public/js/busVehicleGroup.js', 'busrent_admin') }}"></script> 



{% endblock %} 

JS:

// Smart Wizard 
    $('#smartwizard').smartWizard({ 
     selected: 0, // Initial selected step, 0 = first step 
     keyNavigation:true, // Enable/Disable keyboard navigation(left and right keys are used if enabled) 
     autoAdjustHeight:true, // Automatically adjust content height 
     cycleSteps: false, // Allows to cycle the navigation of steps 
     backButtonSupport: true, // Enable the back button support 
     showStepURLhash: true, 
     useURLhash: true, // Enable selection of the step based on url hash 
     lang: { // Language variables 
      next: 'Next', 
      previous: 'Previous' 
     }, 
     toolbarSettings: { 
      toolbarPosition: 'bottom', // none, top, bottom, both 
      toolbarButtonPosition: 'right', // left, right 
      showNextButton: true, // show/hide a Next button 
      showPreviousButton: true, // show/hide a Previous button 
      toolbarExtraButtons: [ 
       $('<button disabled id="finishForm" type="submit"></button>').text('Finish') 
        .addClass('btn btn-info') 

      ] 
     }, 
     anchorSettings: { 
      anchorClickable: false, // Enable/Disable anchor navigation 
      enableAllAnchors: false, // Activates all anchors clickable all times 
      markDoneStep: true, // add done css 
      enableAnchorOnDoneStep: true // Enable/Disable the done steps navigation 
     }, 
     contentURL: null, // content url, Enables Ajax content loading. can set as data data-content-url on anchor 
     disabledSteps: [], // Array Steps disabled 
     errorSteps: [], // Highlight step with errors 
     theme: 'dots', 
     transitionEffect: 'fade', // Effect on navigation, none/slide/fade 
     transitionSpeed: '400' 
    }); 

    $("#smartwizard").on("showStep", function(e, anchorObject, stepNumber, stepDirection) { 
     if (stepNumber == 2){ 
      $('#finishForm').attr("disabled", false); 
     } 
     else{ 
      $('#finishForm').attr("disabled", true); 
     } 

    }); 
+0

客戶如何看待這個? – Webbanditten

+0

@Webbanditten你是指客戶端窗體的樣子? – user2496520

+0

是的,我做了什麼你的HTML/Javascript/CSS看起來像 – Webbanditten

回答

1

的問題似乎是我N:

public function addBusVehicles(BusVehicle $busVehicles) 
{ 
    if ($this->busVehicles->contains($busVehicles)) { 
     return; 
    } 
    $this->busVehicles->add($busVehicles); 
    $busVehicles->addBusVehicleGroup($this); 
} 

當呼叫

$busVehicles->addBusVehicleGroup($this); 

您添加了一個新的路線的關係,儘量不調用此方法。