gpt4 book ai didi

symfony - 基于 ArrayCollection() 填充 Symfony2 中的复选框

转载 作者:行者123 更新时间:2023-12-02 13:28:07 25 4
gpt4 key购买 nike

编辑 - 重新定义问题并包含额外的特定代码

在我的 Symfony2 项目中,我有一个实体 (Configurations),其中包含与另一个实体 (ConfigurationsFeatures) 的 OneToMany 关系。我正在尝试构建一个表单,允许用户更改 Configurations 实体中的值,并选择要保留到 ConfigurationsFeatures 实体的值。该表单有两种使用场景:

1) 创建一个新配置 -> 渲染一个空白表单,其中包含 Configurations 中值的字段以及 ConfigurationsFeatures 中潜在值的复选框集合;潜在值的来源实际上是第三个实体,CoreCodes。验证后,表单的配置值将保留到配置实体,选定的复选框将保留到配置功能。<​​/p>

2) 更新现有配置 -> 使用字段中显示的现有配置值呈现表单,并呈现所有可能的 ConfigurationsFeatures 值的复选框集合,但已选择现有值。

我已经为场景 #1 构建了表单类型和 Controller 操作,并对其进行了测试,一切正常。但是,我似乎无法解决表单复选框部分中的“已选择现有值”的问题。我意识到,当我当前设置 Controller 时,我基本上是在比较苹果和橙子,因为 $entity->getFeatures() 返回 ConfigurationsFeatures 实体的 ArrayCollection,并且表单中没有任何内容可以映射,因为表单使用CoreCodes 实体的集合。

那么我该如何弥补这个差距呢?

<小时/>

Entity\Configurations(存储单独的配置)

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Lynux\AssetBundle\Entity\Core\Configurations
* ... ORM declarations ...
*/
class Configurations
{
// ... plain old properties, nothing fancy here...

/**
* @ORM\OneToMany(targetEntity="Lynux\AssetBundle\Entity\Core\ConfigurationsFeatures", mappedBy="configurationFk")
*/
private $features;

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

// getters and setters

/**
* Set features
*/
public function setFeatures(ArrayCollection $features)
{
$this->features = $features;
}

/**
* Get features
*/
public function getFeatures()
{
return $this->features;
}
}
<小时/>

Entity\ConfigurationsFeatures(存储每个配置的选定功能)

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* Lynux\AssetBundle\Entity\Core\ConfigurationsFeatures
* ... ORM declarations ...
*/
class ConfigurationsFeatures
{
/**
* @var Codes
*
* @ORM\ManyToOne(targetEntity="Lynux\AssetBundle\Entity\Core\Codes")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="feature_fk", referencedColumnName="id")
* })
*/
private $featureFk;

/**
* @var Configurations
*
* @ORM\ManyToOne(targetEntity="Lynux\AssetBundle\Entity\Core\Configurations", inversedBy="features")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="configuration_fk", referencedColumnName="id")
* })
*/
private $configurationFk;

/**
* Set featureFk
*
* @param Lynux\AssetBundle\Entity\Core\Codes $featureFk
* @return ConfigurationsFeatures
*/
public function setFeatureFk(\Lynux\AssetBundle\Entity\Core\Codes $featureFk = null)
{
$this->featureFk = $featureFk;

return $this;
}

/**
* Get featureFk
*
* @return Lynux\AssetBundle\Entity\Core\Codes
*/
public function getFeatureFk()
{
return $this->featureFk;
}

/**
* Set configurationFk
*
* @param Lynux\AssetBundle\Entity\Core\Configurations $configurationFk
* @return ConfigurationsFeatures
*/
public function setConfigurationFk(\Lynux\AssetBundle\Entity\Core\Configurations $configurationFk = null)
{
$this->configurationFk = $configurationFk;

return $this;
}

/**
* Get configurationFk
*
* @return Lynux\AssetBundle\Entity\Core\Configurations
*/
public function getConfigurationFk()
{
return $this->configurationFk;
}
}
<小时/>

Entity\CoreCodes(ConfigurationsFeatures->featureFk 的潜在值来源)

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Lynux\AssetBundle\Entity\Core\Codes
*
* @ORM\Table(name="core_codes")
*/
class Codes
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer", nullable=false, unique=true)
* @ORM\Id
* @Assert\NotBlank(message="ID cannot be blank!")
* @Assert\Regex(pattern="/^\d+$/", match=true, message="ID must be an integer!")
* @Assert\MinLength(limit=8, message="ID must be 8 digits in length!")
* @Assert\MaxLength(limit=8, message="ID must be 8 digits in length!")
*/
private $id;

// ... various properties, not applicable to question ...

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

return $this;
}

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

// ... getters and setters for other properties ...
}
<小时/>

类型\表单配置

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class FormConfigurations extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$features = $this->features;

// ... builders for other properties ...

$builder->add(
'features',
'entity',
array(
'class' => 'LynuxAssetBundle:Core\Codes',
'property_path' => false,
'query_builder' => function(EntityRepository $er) use ($features)
{
return $er->createQueryBuilder('u')
->where('u.submodule = :submodule')
->setParameter('submodule', 'feature');
},
'expanded' => true,
'multiple' => true,
'property' => 'title',));
}

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

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array(
'data_class' => 'Lynux\AssetBundle\Entity\Core\Configurations',));
}
}
<小时/>

Controller\ConfigurationsController::createAction()

public function createAction(Request $request)
{
$config = $request->getSession()->get('configuration');
$manage = $this->getDoctrine()->getEntityManager();

$entity = new Configurations();
$form = $this->createForm(new FormConfigurations($features), $entity);
$failed = null;

$features = $manage
->getRepository('LynuxAssetBundle:Core\Codes')
->findBySubmodule('feature');

if ('POST' == $request->getMethod()) {
$form->bind($request);

if ($form->isValid()) {
$status = $manage
->getRepository('LynuxAssetBundle:Core\Codes')
->findOneById(10103022);

$entity->setToken($entity->getName());
$entity->setStatusFk($status);
$entity->setCreateAccountFk($this->getUser());

$features = $form->get('features')->getData();
foreach($features as $feature) {
$addFeature = new ConfigurationsFeatures();
$addFeature->setConfigurationFk($entity);
$addFeature->setFeatureFk($feature);
$manage->persist($addFeature);
}
$manage->persist($entity);
$manage->flush();

return $this->redirect($this->generateUrl(
'CoreConfigurationsConfirm',
array(
'code' => 'create',
'token' => $entity->getToken(),)));
} else {
$failed = true;
}
}
<小时/>

Controller\ConfigurationsController::updateAction()

public function updateAction(Request $request, $token)
{
$manage = $this->getDoctrine()->getEntityManager();
$entity = $manage
->getRepository('LynuxAssetBundle:Core\Configurations')
->findOneByToken($token);

$form = $this->createForm(new FormConfigurations($features), $entity);

if ('POST' == $request->getMethod()) {
$form->bind($request);

if ($form->isValid()) {
$currentFeatures = $manage
->getRepository('LynuxAssetBundle:Core\ConfigurationsFeatures')
->findByConfigurationFk($entity);
foreach($currentFeatures as $currentFeature) {
$manage->remove($currentFeature);
}

$features = $form->get('features')->getData();
foreach($features as $feature) {
$addFeature = new ConfigurationsFeatures();
$addFeature->setConfigurationFk($entity);
$addFeature->setFeatureFk($feature);
$manage->persist($addFeature);
}

$manage->flush();
}
}

最佳答案

啊,耐心和坚持...我突然想到,在持久化到数据库时,我正在将 CoreCodes 集合转换为 ConfigurationsFeatures 集合。同样,在显示表单时从 ConfigurationsFeatures 转换为 CoreCodes 也是有意义的。

// ConfigurationsController::updateAction()

$formFeatures = new ArrayCollection();
$currentFeatures = $entity->getFeatures();
foreach($currentFeatures as $feature) {
$formFeatures->add($feature->getFeatureFk());
}

if ('POST' != $request->getMethod()) { // first time the form is loaded
$form->get('features')->setData($formFeatures);
}

关于symfony - 基于 ArrayCollection() 填充 Symfony2 中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12923142/

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