4

编辑- 重新定义问题并包含其他特定代码

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

1)创建一个新配置-> 呈现一个空白表单,其中包含 Configurations 中的值字段和 ConfigurationsFeatures 中潜在值的复选框集合;潜在价值的来源实际上是第三个实体,CoreCodes。验证后,表单的配置值将保存到配置实体,选定的复选框将保存到配置功能。

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

我已经为场景 #1 构建了表单类型和控制器操作,对其进行了测试,一切正常。但是,我似乎无法解决表单复选框部分中的“已选择现有值”。我意识到,当我目前设置了我的控制器时,我基本上是在比较苹果和橘子,因为 $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',));
    }
}

控制器\配置控制器::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;
        }
    }

控制器\配置控制器::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();
        }
    }
4

1 回答 1

4

啊,耐心和坚持……我突然想到,在持久化到数据库时,我正在将 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);
}
于 2012-10-22T21:13:31.687 回答