symfony-更新实体和更改的字段

时间:2019-02-21 10:09:45

标签: php symfony doctrine-orm doctrine

我尝试在symfony中使用editAction,但是如果不更改输入文件,则会出错,它将数据库中的字段文件更新为null ...如果在更新操作中未更改值,如何不更新字段文件

代码操作:

          /**
       * @Route("/babysitter/update/{id}", name="update_babysitter_by_admin")
       *
       * @param Request $request
       * @param BabySitter $babySitter
       * @ParamConverter("id", options={"id": "id"})
       *
       * @return mixed
       */
        public function updateBabySitterAction(BabySitter $babySitter, Request $request){
          $em= $this->getDoctrine()->getManager();
          $form= $this->createForm(BabySitterType::class, $babySitter,['requiredFile'=> false]);

          $form->handleRequest($request);
          if($form->isSubmitted() && $form->isValid()){
            if($babySitter->getPicture()->getDocument()){
              $this->uploadDocument->upload($babySitter->getPicture(), $this->getParameter('pictures_directory'));
            }

            if($babySitter->getCriminalRecord()->getDocument()){
              $this->uploadDocument->upload($babySitter->getCriminalRecord(), $this->getParameter('criminalRecord_director_babySitter'));
            }

            if($babySitter->getIdCards()){
              $this->uploadDocument->uploadIdCard($babySitter->getIdCards(), $babySitter,$this->getParameter('idCard_directory'));
            }
            $em->persist($babySitter);
            $em->flush();

            $url = $this->generateUrl('info_babySitter',['id'=> $babySitter->getId()]);
            $response = new RedirectResponse($url);
            return $response;
          }

          return $this->render('admin/registerBabySitter.html.twig',[
            'form'=> $form->createView()
          ]);
        }

细枝:

{% trans_default_domain 'FOSUserBundle' %}

    <div class="register-box"  style="width:460px">
      <div class="register-box-body">
        <p class="login-box-msg">Register a new BabySitter</p>

        {{ form_start(form, {'method':'post', 'attr': {'class': 'fos_user_registration_register', 'novalidate': 'novalidate'}}) }}

        <div class="form-group has-feedback">
          {{ form_widget(form.email,{'attr': {'class': 'form-control', 'placeholder': 'Email'}}) }}
          {{ form_errors(form.email) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.firstName,{'attr': {'class': 'form-control', 'placeholder': 'FirstName'}}) }}
          {{ form_errors(form.firstName) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.lastName,{'attr': {'class': 'form-control', 'placeholder': 'LastName'}}) }}
          {{ form_errors(form.lastName) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.plainPassword.first,{'attr': {'class': 'form-control', 'placeholder': 'Password'}}) }}
          {{ form_errors(form.plainPassword.first) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.plainPassword.second,{'attr': {'class': 'form-control', 'placeholder': 'Repeat Password'}}) }}
          {{ form_errors(form.plainPassword.second) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.genre,{'attr': {'class': 'form-control', 'placeholder': 'Genre'}}) }}
          {{ form_errors(form.genre) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.dateBirth,{'attr': {'class': 'form-control', 'placeholder': 'date Birthday'}}) }}
          {{ form_errors(form.dateBirth) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.linkVideo,{'attr': {'class': 'form-control', 'placeholder': 'link Video'}}) }}
          {{ form_errors(form.linkVideo) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.criminalRecord) }}
          {{ form_errors(form.criminalRecord) }}
        </div>

        <div class="form-group has-feedback">

          <ul id="idCard-fields-list"
              data-prototype="{{ form_widget(form.idCards.vars.prototype)|e }}"
              data-widget-tags="{{ '<li></li>'|e }}">
            {{ form_row(form.idCards) }}
            {% for idCardField in form.idCards %}
              <li>
                {{ form_errors(idCardField) }}
                {{ form_widget(idCardField) }}
              </li>
            {% endfor %}
          </ul>
          <div class="row">
            <button type="button"
                    class="add-another-collection-widget-idCard btn btn-primary btn-flat"
                    data-list="#idCard-fields-list">Add another idCard</button>
          </div>
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.nbrYears,{'attr': {'class': 'form-control', 'placeholder': 'Number Years'}}) }}
          {{ form_errors(form.nbrYears) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.rib,{'attr': {'class': 'form-control', 'placeholder': ' rib'}}) }}
          {{ form_errors(form.rib) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_widget(form.presentation,{'attr': {'class': 'form-control', 'placeholder': 'presentation'}}) }}
          {{ form_errors(form.presentation) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.adress) }}
          {{ form_errors(form.adress) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.availability) }}
          {{ form_errors(form.availability) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.assignement) }}
          {{ form_errors(form.assignement) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.qualification) }}
          {{ form_errors(form.qualification) }}
        </div>

        <div class="form-group has-feedback">
          {{ form_row(form.picture) }}
          {{ form_errors(form.picture) }}
        </div>

        <div class="form-group has-feedback">

          <ul id="language-fields-list"
              data-prototype="{{ form_widget(form.languages.vars.prototype)|e }}"
              data-widget-tags="{{ '<li></li>'|e }}">
            {{ form_widget(form.languages) }}
            {% for languageField in form.languages %}
              <li>
                {{ form_errors(languageField) }}
                {{ form_widget(languageField) }}
              </li>
            {% endfor %}
          </ul>
          <div class="row">
            <button type="button"
                    class="add-another-collection-widget btn btn-primary btn-flat"
                    data-list="#language-fields-list">Add another language</button>
          </div>
        </div>

        <div class="row">
          <div class="col-xs-12">
            <input type="submit" class="btn btn-primary btn-block btn-flat" value="{{ 'registration.submit'|trans }}">
          </div>
          <!-- /.col -->
        </div>

        {{ form_rest(form) }}
        {{ form_end(form) }}

      </div>
      <!-- /.form-box -->
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script>

      jQuery(document).ready(function () {
        jQuery('.add-another-collection-widget').click(function (e) {
          var list = jQuery(jQuery(this).attr('data-list'));
          // Try to find the counter of the list or use the length of the list
          var counter = list.data('widget-counter') | list.children().length;

          // grab the prototype template
          var newWidget = list.attr('data-prototype');
          // replace the "__name__" used in the id and name of the prototype
          // with a number that's unique to your emails
          // end name attribute looks like name="contact[emails][2]"
          newWidget = newWidget.replace(/__name__/g, counter);
          // Increase the counter
          counter++;
          // And store it, the length cannot be used if deleting widgets is allowed
          list.data('widget-counter', counter);

          // create a new list element and add it to the list
          var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
          newElem.appendTo(list);
          addTagFormDeleteLink(newElem);
        });

        function addTagFormDeleteLink($tagFormLi) {
          var $removeFormButton = $('<button class="btn btn-danger btn-flat"  style="margin-top:2%;margin-left:50%" type="button">Delete this Language</button>');
          $tagFormLi.append($removeFormButton);

          $removeFormButton.on('click', function(e) {
            // remove the li for the tag form
            $tagFormLi.remove();
          });
        }


        jQuery('.add-another-collection-widget-idCard').click(function (e) {
          var list = jQuery(jQuery(this).attr('data-list'));
          // Try to find the counter of the list or use the length of the list
          var counter = list.data('widget-counter') | list.children().length;

          // grab the prototype template
          var newWidget = list.attr('data-prototype');
          // replace the "__name__" used in the id and name of the prototype
          // with a number that's unique to your emails
          // end name attribute looks like name="contact[emails][2]"
          newWidget = newWidget.replace(/__name__/g, counter);
          // Increase the counter
          counter++;
          // And store it, the length cannot be used if deleting widgets is allowed
          list.data('widget-counter', counter);

          // create a new list element and add it to the list
          var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
          if(counter <= 3){ newElem.appendTo(list); }


        });

        function addIdCardFormDeleteLink($tagFormLi) {

          var $removeFormButton = $('<button class="btn btn-danger btn-flat" style="margin-top:2%;margin-left:50%" type="button">Delete this idCard</button>');
          $tagFormLi.append($removeFormButton);

          $removeFormButton.on('click', function(e) {
            // remove the li for the tag form
            $tagFormLi.remove();
          });
        }

      });


    </script>

当我更新实体而不更新字段文件时,我的字段文件在数据库中为空...如何在不更改输入文件的情况下更新实体

0 个答案:

没有答案