Symfony 2-具有一个表单的多个实体



我有以下具有关系的表:

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

UserAddress有两个外键:

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

UserAddressFields有一个外键:

  • address_id->useraddress.id

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

用户地址.php

<?php
namespace SCWDesignsBundleEntity;
use DoctrineORMMapping as ORM;
use SCWDesignsBundleEntityUserStates;
use UsersBundleEntityUser;
/**
* @ORMEntity(repositoryClass="SCWDesignsBundleEntityRepository
UserAddressesRepository")
* @ORMTable(name="user_addresses")
*/
class UserAddresses {
/**
* @ORMID
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="UsersBundleEntityUser")
* @ORMJoinColumn(name="profile_id", referencedColumnName="id")
*/
protected $profile_id;
/**
* @ORMManyToOne(targetEntity="UserStates")
* @ORMJoinColumn(name="state_id", referencedColumnName="id")
*/
protected $state_id;
/**
* @ORMColumn(type="boolean")
*/
protected $isBilling;
/**
* @ORMColumn(type="boolean")
*/
protected $isShipping;
/**
* @ORMColumn(type="string", columnDefinition="CHAR(1)")
*/
protected $addressType;

UserAddressFieldsTranslation.php

<?php
namespace SCWDesignsBundleEntity;
use DoctrineORMMapping as ORM;
use SCWDesignsBundleEntityUserAddressFields;
use UsersBundleEntityUser;
/**
* @ORMEntity(repositoryClass="SCWDesignsBundleEntityRepositoryUserAddressFieldsTranslationRepository")
* @ORMTable(name="user_address_fields_translation")
*/
class UserAddressFieldsTranslation {
/**
* @ORMID
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", columnDefinition="VARCHAR(255) NOT NULL")
*/
protected $name;

UserStates.php

<?php
namespace SCWDesignsBundleEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="SCWDesignsBundleEntityRepositoryStatesRepository")
* @ORMTable(name="user_states")
*/
class UserStates {
/**
* @ORMID
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="string", columnDefinition="CHAR(2)")
*/
protected $country_code;
/**
* @ORMColumn(type="string", columnDefinition="VARCHAR(64)")
*/
protected $state;

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

用户地址窗体类型

<?php
namespace SCWDesignsBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'SCWDesignsBundleEntityUserAddresses'
));
}
public function getName() {
return 'UserAddressFormType';
}
}

用户地址字段FormType.php

<?php
namespace SCWDesignsBundleFormType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
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' => 'SCWDesignsBundleEntityUserAddressFields'
));
}
public function getName() {
return 'UserAddressFieldsFormType';
}
}

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

<?php
namespace SCWDesignsBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
use SCWDesignsBundleEntityUserAddresses;
use SCWDesignsBundleFormTypeUserAddressFormType;
use SCWDesignsBundleFormTypeUserAddressFieldsFormType;
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 "SCWDesignsBundleEntityUserAddresses".

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

$resolver->setDefaults(array(
'data_class' => 'SCWDesignsBundleEntityUserAddresses'
));

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

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

它应该起作用!

您需要仔细阅读有关如何做到这一点的文档:

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

你的代码没有意义:

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

这并不能让它安静下来。

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

相关内容

  • 没有找到相关文章

最新更新