gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:55:51 28 4
gpt4 key购买 nike

我有以下关系表:

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

UserAddress 有 2 个外键:

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

UserAddressFields 有一个外键:

  • address_id -> useraddress.id

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

用户地址.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;

用户状态.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。

用户地址表单类型

<?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';
}
}

在 Controller 中是您的基本表单调用。配置文件 Controller .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".

最佳答案

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

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

因此,您添加的任何字段都应该对应于实体中的一个字段。所以我认为 geoB 是对的。你需要添加你想要的映射,如果你想要它在类上或者使表单独立于实体(所以从默认值中删除'data_class'选项)。要添加映射,请考虑以下用于 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()));

它应该有效!

关于php - Symfony 2 - 具有一种形式的多个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547321/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com