简体   繁体   English

在Symfony2中嵌入表格并保存到数据库

[英]Embedded form in Symfony2 and saving to database

In my Symfony2 application, there are two entities: Address and Town. 在我的Symfony2应用程序中,有两个实体:地址和镇。 The Address entity has a 4-digit postal code property ("pcNum"), which refers to the id of a Town entity. 地址实体具有一个4位数的邮政编码属性(“ pcNum”),它引用镇实体的ID。 An address can only have one postal code and hence refer to one town only (however the reverse is possible: a town could have more postal codes). 一个地址只能有一个邮政编码,因此只能指一个城镇(但是可能相反:一个城镇可以有更多邮政编码)。

For both entities I have created a form called TownType and AddressType. 对于这两个实体,我都创建了一个名为TownType和AddressType的表单。 Users can enter and save a Town (this works fine). 用户可以输入并保存城镇(可以正常运行)。 The form for the Address entity allows users to fill in an address, including a postal code. 地址实体的表单允许用户填写地址,包括邮政编码。 The postal code is linked to the id of a Town entity. 邮政编码链接到Town实体的ID。

However, when I try to persist a new Address entity retrieved from the AddressType form, I receive the MySql error that the pc_num field (pcNum in the entity) is empty: 但是,当我尝试保留从AddressType表单中检索到的新Address实体时,收到MySql错误,其中pc_num字段(实体中的pcNum)为空:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'pc_num' cannot be null SQLSTATE [23000]:违反完整性约束:1048列“ pc_num”不能为空

(This is the field of the Address entity that should have contained a ref key to a Town.) Somehow its value is lost before persisting the Address entity. (这是Address实体的字段,应该包含Town的ref键。)在持久保存Address实体之前,其值会丢失。 If I fill the field manually in my controller, regardless of the user input in the form, I can persist the new Address without the error. 如果我在控制器中手动填写该字段,无论用户在表格中输入什么,我都可以保留新地址而不会出现错误。 If I drop the link to the Town entity and use an unaware form field, I can also safe without error as long as the postal code happens to exist. 如果我删除了到Town实体的链接并使用了一个不知道的表单字段,那么只要邮政编码存在,我也可以确保没有错误。 But in the way it should be, using two forms and entity-association, I cannot get it to work. 但是按照应有的方式,使用两种形式和实体关联,我无法使其正常工作。 I have tried many different things over the past weeks and I am out of ideas. 在过去的几周里,我尝试了许多不同的事情,但我没有主意。 Below is my code. 下面是我的代码。 Feel free to comment on anything, maybe my whole approach is wrong. 随便评论任何事情,也许我的整体方法是错误的。

This is my Town entity: 这是我镇的实体:

namespace Xx\xBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Check;

 /**
 *
 * Xx\xBundle\Entity\Town
 *
 * @ORM\Table(name="lib_towns")
 * @ORM\Entity
 */

class Town
{
    #id
    /**
     * @ORM\Id
     * @ORM\Column(type="integer", length="4", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Check\NotBlank()
     * @Check\MaxLength(4)
     */
    protected $id;

    #name
    /**
     * @ORM\Column(name="name", type="string", length="100", nullable=true)
 * @Check\NotBlank()
     * @Check\MaxLength(100)
     */
    protected $name;

    //some more properties

    #getters and setters

    /**
     * Set id
     *
     * @param integer $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

  //some more getters ans setters

}

This is my Address entity: 这是我的地址实体:

namespace Xx\xBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Check;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Xx\xBundle\Entity\Town;

 /**
 *
 * Xx\xBundle\Entity\Address
 *
 * @ORM\Entity
 * @ORM\Table(name="addresses")
 */

class Address
{
    #id
    /**
    * @ORM\Id
     * @ORM\Column(type="integer", length="6", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
 * @Check\NotBlank()
 * @Check\MaxLength(6)
     */
    protected $id;

    #town
    /**
    * @orm\OneToOne(targetEntity="town")
 * @ORM\JoinColumn(name="pc_num", referencedColumnName="id", nullable=false)
    */
    protected $town;

    //some other properties...

    #getters and setters

    /**
     * Set id
     *
     * @param integer $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set town entity linked to this address
     *
     */
    public function setTown(town $town = null)
    {
        $this->town = $town;
    }

    /**
     * Get town entity linked to this address
     *
     */
    public function getTown()
    {
        return $this->town;
    }

    //some other functions...

}

Next, this is the form I've created for the Address: 接下来,这是我为地址创建的表单:

namespace Xx\xBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;

class AddressType extends AbstractType
{
    public function buildForm(Formbuilder $builder, array $options)
    {
        $builder->add('id', 'hidden'); //necessary for updates
        $builder->add('town', 'entity', array
            (
                'label' => 'Postal code (4 digits):*',
                'class' => 'XxxBundle:Town',
                'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('t')
            ->orderBy('t.id', 'ASC'); },            
                //'empty_value' => '----',
                'property'=> 'Id',
                'property_path'=> false,
                'expanded' => false,
                'multiple' => false,
                'required'  => true
            ));
        $builder->add('street', 'text', array
            (
                'label' => 'Street:*',
                'required'  => true
            ));
        //some more fields...
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Xx\xBundle\Entity\Address'
        );
    }

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

This is the relevant action function in my Address controller: 这是我的地址控制器中的相关动作函数:

public function editAddressAction($id = null)
{
$em = $this->getDoctrine()->getEntityManager();
$address = new Address();
$isNew = is_null($id);
     //this also tests positve after a form has been sent
    if($isNew)
    {
           #fill address object with some defaults
            $address->setCreated(new \DateTime("now"));
         } else
         {
            #fill address object with existing db data
            $address = $em->getRepository('XxxBundle:Address')->find($id);
         }
         #create form and fill it with address object data
         $form = $this->createForm(new AddressType(), $address);
            #if form sent: check input
            $request = $this->get('request');
            if ($request->getMethod() == 'POST')
            {
               $form->bindRequest($request); //calls setters
               if ($form->isValid())
               {
                  //if I leave the following lines in: no error (but also no sense)
                  //because the pcNum should be retrieved from the form
                  $pcNum = 2222;
                  $town = $em->getRepository('XxxBundle:Town')->find($pcNum);
                  $address->setTown($town);
                  //end

                  #persist and flush

                     $id_unknown = is_null($address->getId());
                     if($id_unknown)
                     {
                        #insert address
                        $em->persist($address);
                     }   else
                     {
                        #update address
                        $address->setModified(new \DateTime("now"));
                        $em->merge($address);
                     }
                     #commit
                     $em->flush();
                  #get id of update or insert and redirect user
                     $address_id = $address->getId();
                     return $this->redirect($this->generateUrl('displayAddress', array('id'=>$address_id)));
               }
            }
         return $this->render('XxxBundle:Forms:address.html.twig', array('form'=>$form->createView(), 'new' => $isNew, 'id' => $id));
   }

To conclude, this is the relevant Twig snippet: 总结一下,这是相关的Twig代码段:

   <form action="{{ path('addAddress') }}" method="post" {{ form_enctype(form) }}>
      {{ form_errors(form) }}
      <div class="form_row">
         {{ form_errors(form.town) }}
         {{ form_label(form.town, 'Postal code:*') }} 
         {{ form_widget(form.town, {'attr': { 'class': 'form_pcNum', 'maxlength': '4', 'size': '4' } } ) }} 
      </div>
      <div class="form_row">
        <!-- some more fields here -->
      </div>
      <div class="form_row">
         <button name="btn_add" id="do_add" type="submit" class="" value="btn_add" title="Ok!">Ok</button>
      </div>
      {{ form_rest(form) }}
   </form>

Any help is appreciated... If you know of a relevant (working) example, that would also be great. 感谢您提供任何帮助...如果您知道一个相关的(有效的)示例,那也很好。 Cheers! 干杯!

I had similar problem with foreign key violation and I solved. 我在违反外键方面遇到了类似的问题,并且解决了。 Please read Symfony2 form and Doctrine2 - update foreign key in assigned entities fails [solved] 请阅读Symfony2表格和Doctrine2-在分配的实体中更新外键失败[解决]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM