Symfony表单集合 - 错误

时间:2015-06-17 09:40:14

标签: php forms symfony collections

我有一个Symfony表单集合,它实际上是嵌入一个表单,并且允许创建多个Specification对象,并且我收到一些不应出现的错误。

注意:

Symfony版本:2.3.30

实现:之前我已经使用过这个方法,并且根据Symfony文档完美地工作了(我甚至尝试使用这个方法从另一个项目中复制相关实体和表单,并且仍然会遇到相同的错误! )

代码:

实体:

<?php
/**
 * product
 * @package AppBundle\Entity
 * @author Pete Robinson
 **/
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Utils\Traits\Doctrine\Timestampable;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="product")
 */
class Product
{
    ~snip snip snip - ID and other text fields~

    /**
     * specifications
     * @ORM\OneToMany(targetEntity="Specification", mappedBy="product")
     * @var object ArrayCollection
     **/
    private $specifications;

子实体:

<?php
/**
 * product
 * @package AppBundle\Entity
 **/
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="specification")
 */
class Specification
{
    /**
     * product
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="specifications")
     * @var object Product
     **/
    private $product;

表格:     

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;

class ProductType extends AbstractType
{
    /**
     * set default options
     * @return void
     **/
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection' => false,
            'data_class' => 'AppBundle\Entity\Product'
        ));
    }

    /**
     * build form
     * @param FormBuilderInterface $buidler
     * @param array $options
     * @return void
     **/
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('specifications', 'collection', array(
                'type' => new SpecificationType(),
                'allow_add' => true,
                'allow_delete' => true,
                'error_bubbling' => false,
                'cascade_validation' => true
            ))
        ;

错误:

 The options "allow_add", "allow_delete", "type" do not exist. Known options
 are: "action", "attr", "auto_initialize", "block_name", "by_reference", 
"cascade_validation", "compound", "constraints", "csrf_field_name",
"csrf_message", "csrf_protection", "csrf_provider", "data", "data_class",
"disabled", "empty_data", "error_bubbling", "error_mapping",
"extra_fields_message", "inherit_data", "intention", "invalid_message",
"invalid_message_parameters", "label", "label_attr", "mapped", "max_length",
"method", "pattern", "post_max_size_message", "property_path", "read_only",
"required", "translation_domain", "trim", "validation_groups", "virtual"

据我所知,我已经正确地遵循了关于嵌入式表格的Symfony文档,就像我一直一样 - 然而我遇到了这个问题。有没有人见过这个?

最后一个FYI:Composer.json

    {
    "name": "project",
    "license": "proprietary",
    "type": "project",
    "autoload": {
        "psr-0": {
            "": "src/"
        }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "2.3.*",
        "doctrine/orm": "~2.2,>=2.2.3,<2.5",
        "doctrine/dbal": "<2.5",
        "doctrine/doctrine-bundle": "~1.2",
        "twig/extensions": "1.0.*",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~2.3",
        "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
        "sensio/generator-bundle": "~2.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/user-bundle": "~2.0@dev",
        "doctrine/doctrine-fixtures-bundle": "2.2.*@dev",
        "gregwar/image-bundle": "dev-master",
        "gedmo/doctrine-extensions": "dev-master"

    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "minimum-stability": "stable",
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        }
    }
}

编辑:下面的SpecificationType - 与ProductType

具有相同名称空间的同一目录
    <?php
/**
 * specification form
 **/
namespace AdminBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;

class SpecificationType extends AbstractType
{
    /**
     * set default options
     * @return void
     **/
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'csrf_protection' => false,
            'data_class' => 'AppBundle\Entity\Specification'
        ));
    }

    /**
     * build form
     * @param FormBuilderInterface $buidler
     * @param array $options
     * @return void
     **/
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('value', 'text', array(
                'label' => 'Value *',
                'label_attr' => array(
                    'class' => 'col-sm-2 control-label'
                ),
                'attr' => array(
                    'class' => 'form-control'
                ),
                'constraints' => array(
                    new NotBlank(array(
                        'message' => 'Specification value is required'
                    ))
                )
            ))
            ->add('remove', 'button', array(
                'attr' => array(
                    'class' => 'btn btn-danger right padded remove_item'
                )
            ))
        ;
    }

1 个答案:

答案 0 :(得分:1)

解决。我有一个名为Collection的教义实体,Product表格用于从中选择一个项目(在这个项目中,Collection本质上是一个Category)。出于某种原因,这在我希望使用symfony表单集合的表单对象中进一步搞乱。它都是命名空间,所以不应该有类冲突,但似乎Symfony不喜欢名为Collection的实体。

我将实体重命名为ProductCollection,并修复了尝试将嵌入表单用作集合的问题。

感谢您的建议。