dongyi2006 2015-04-09 19:31
浏览 55
已采纳

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

I've got the following tables with relationships:

  • UserAddress table (Holds basic information for the address)
  • UserAddressFields table (Holds the value for each field associated to an address)
  • UserAddressFieldsTranslation table (Holds the field ids)
  • UserStates table (Holds the values of all the states in the US and the abbreviations)

UserAddress has 2 foreign keys:

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

UserAddressFields has one foreign key:

  • address_id -> useraddress.id

I've created entities for all 4 of these tables (Getters and Setters have been removed to save space, generated via console):

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;

I have 2 form types, UserAddressFormType and 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';
    }
}

In the controller is your basic form call. 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(),
        ));
    }
}

Everywhere I've read this is the correct means of adding another entity to your form builder but everytime I do this, or any other variation I've ran across I get the following error

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

2条回答 默认 最新

  • douchengfei3985 2015-04-10 20:12
    关注

    So in the first form you set defaults to this:

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

    So any field that you add is expected to correspond to a field in the entity. So I think geoB was right. You need to add the mapping that you want, if you want it on the class or make the form independent of the entity (so remove the 'data_class' option from the defaults). To add the mapping consider the following code for a OneToMany relation:

     /**
     * @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();
    }
    

    And if you combine this with Nawfal's answer:

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

    It should work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题