Symfony 2 - 具有一种形式的多个实体

时间:2015-04-09 19:31:26

标签: php mysql forms symfony doctrine-orm

我有以下关系表:

  • UserAddress table(保存地址的基本信息)
  • UserAddressFields table(保存与地址关联的每个字段的值)
  • UserAddressFieldsTranslation table(持有字段ID)
  • UserStates table(保存美国所有州的值和缩写)

UserAddress有2个外键:

  • Profile_id - > useraccounts.id
  • state_id - > userstates.id

UserAddressFields有一个外键:

  • address_id - > useraddress.id

我为所有这4个表创建了实体(Getters和Setter已被删除以节省空间,通过控制台生成):

UserAddress.php

<?php
namespace SCWDesignsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SCWDesignsBundle\Entity\UserStates;
use UsersBundle\Entity\User;

/**
 * @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\
UserAddressesRepository")
 * @ORM\Table(name="user_addresses")
 */
class UserAddresses {
    /**
     * @ORM\ID
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="UsersBundle\Entity\User")
     * @ORM\JoinColumn(name="profile_id", referencedColumnName="id")
     */
    protected $profile_id;

    /**
     * @ORM\ManyToOne(targetEntity="UserStates")
     * @ORM\JoinColumn(name="state_id", referencedColumnName="id")
     */
    protected $state_id;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $isBilling;

    /**
     * @ORM\Column(type="boolean")
     */
    protected $isShipping;

    /**
     * @ORM\Column(type="string", columnDefinition="CHAR(1)")
     */
    protected $addressType;

UserAddressFieldsTranslation.php

<?php
namespace SCWDesignsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SCWDesignsBundle\Entity\UserAddressFields;
use UsersBundle\Entity\User;

/**
 * @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\UserAddressFieldsTranslationRepository")
 * @ORM\Table(name="user_address_fields_translation")
 */
class UserAddressFieldsTranslation {
    /**
     * @ORM\ID
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", columnDefinition="VARCHAR(255) NOT NULL")
     */
    protected $name;

UserStates.php

<?php
namespace SCWDesignsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="SCWDesignsBundle\Entity\Repository\StatesRepository")
 * @ORM\Table(name="user_states")
 */
class UserStates {
    /**
     * @ORM\ID
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", columnDefinition="CHAR(2)")
     */
    protected $country_code;

    /**
     * @ORM\Column(type="string", columnDefinition="VARCHAR(64)")
     */
    protected $state;

我有2种表单类型,UserAddressFormType和UserAddressFieldsFormType。

UserAddressFormType

<?php
namespace SCWDesignsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserAddressFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('profile_id', null, array('label' => 'Profile ID'))
            ->add('state_id', null, array('label' => 'State ID'))
            ->add('isBilling', null, array('label' => 'Billing Address'))
            ->add('isShipping', null, array('label' => 'Shipping Address'))
            ->add('addressType', null, array('label' => 'Address Type'))
            ->add('addressFields', new UserAddressFieldsFormType());
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'SCWDesignsBundle\Entity\UserAddresses'
        ));
    }

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

UserAddressFieldsFormType.php

<?php
namespace SCWDesignsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserAddressFieldsFormType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('address_id', null, array('label' => 'Address Field'))
            ->add('address_field_id', null, array('label' => 'Address Field ID'))
            ->add('value', null, array('label' => 'Value'));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'SCWDesignsBundle\Entity\UserAddressFields'
        ));
    }

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

在控制器中是您的基本表单调用。 ProfileController.php

<?php
namespace SCWDesignsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use SCWDesignsBundle\Entity\UserAddresses;
use SCWDesignsBundle\Form\Type\UserAddressFormType;
use SCWDesignsBundle\Form\Type\UserAddressFieldsFormType;

class ProfileController extends Controller {
    public function editAddressAction() {
        $address = new UserAddresses();
        $form_address = $this->createForm(new UserAddressFormType(), $address);


        $request = $this->getRequest();
        if ($request->getMethod() == 'POST') {
            // Perform some action.

            return $this->redirect($this->generateUrl('fos_user_profile_show'));
        }



        return $this->render('SCWDesignsBundle:Profile:edit.html.twig', array(
            'active_page' => 'profile',
            'form_address' => $form_address->createView(),
        ));
    }
}

到处都读到这是向表单构建器添加其他实体的正确方法,但每次我这样做,或者我遇到的任何其他变体都会出现以下错误

Neither the property "addressFields" nor one of the methods
"getAddressFields()", "isAddressFields()", "hasAddressFields()", "__get()" exist
and have public access in class "SCWDesignsBundle\Entity\UserAddresses".

2 个答案:

答案 0 :(得分:2)

您需要仔细阅读有关如何执行此操作的文档:

http://symfony.com/doc/current/cookbook/form/form_collections.html

你的代码没有理智:

->add('addressFields', new UserAddressFieldsFormType());

这不安静吧。

$builder->add('addressFields', 'collection', array('type' => new UserAddressFieldsFormType())); 

答案 1 :(得分:1)

因此,在第一种形式中,您将默认设置为:

$resolver->setDefaults(array(
   'data_class' => 'SCWDesignsBundle\Entity\UserAddresses'
));

因此,您添加的任何字段都应与实体中的字段对应。所以我认为geoB是对的。您需要添加所需的映射,如果您希望它在类上或使表单独立于实体(因此从默认值中删除&#39; data_class&#39;选项)。 要添加映射,请考虑OneToMany关系的以下代码:

 /**
 * @ORM\OneToMany(targetEntity="UserAddressFields", mappedBy="user_address", cascade={"persist", "remove"})
 */
protected $addressFields;

public function addAddressField($addressField) {
    $this->addressFields[] = $addressField;
}

public function removeAddressField() {...}

public function getAddressFields() {...}

public function __construct()
{
    $this->addressFields = new ArrayCollection();
}

如果你把它与Nawfal的回答结合起来:

$builder->add('addressFields', 'collection', array('type' => new UserAddressFieldsFormType()));

它应该有用!