symfony插入一行

时间:2017-05-04 11:57:30

标签: php mysql forms symfony doctrine-orm

我有一个有线的情况。在持久化symfony上插入多个记录而不是一个。我发现我的代码没有任何问题,我不知道如何调试此错误,因为它看起来很好。

应用程序背后的逻辑是用户可以选择两个公交车站并创建路线。他还需要定义价格和组(最小和最大人数)。此外,用户需要选择将驾驶这条特定路线的公交车辆

有人知道问题出在哪里吗?

以下是提交表单时实体的输出

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

    });

1 个答案:

答案 0 :(得分:1)

问题似乎在:

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

致电时

$busVehicles->addBusVehicleGroup($this);

您正在为关系添加新路由,请尝试不调用此方法。