无法将属性%name%写入数组。也许您应该将属性路径写为%name%?

时间:2016-03-22 09:41:11

标签: forms api symfony fosrestbundle

我使用FosRestBundle,我想保存带有嵌入信息的实体:

父实体:

class ServiceOffer
{
    use ORMBehaviors\SoftDeletable\SoftDeletable;

    /**
     * @var PriceLevel
     *
     * @ORM\OneToMany(targetEntity="PriceLevel", mappedBy="serviceOffer", cascade={"persist"})
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    private $priceLevels;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->priceLevels = new ArrayCollection();
    }

    /**
     * Add priceLevel
     *
     * @param PriceLevel $priceLevel
     *
     * @return ServiceOffer
     */
    public function addPriceLevel(PriceLevel $priceLevel)
    {
        $priceLevel->setServiceOffer($this);
        $this->priceLevels[] = $priceLevel;

        return $this;
    }

    /**
     * Remove priceLevel
     *
     * @param PriceLevel $priceLevel
     */
    public function removePriceLevel(PriceLevel $priceLevel)
    {
        $this->priceLevels->removeElement($priceLevel);
    }

    /**
     * Get priceLevels
     *
     * @return Collection
     */
    public function getPriceLevels()
    {
        return $this->priceLevels;
    }
}

儿童:

class PriceLevel
{

    use ORMBehaviors\Timestampable\Timestampable;
    use ORMBehaviors\SoftDeletable\SoftDeletable;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    protected $id;

    /**
     * @var ServiceOffer
     *
     * @ORM\ManyToOne(targetEntity="ServiceOffer", inversedBy="priceLevels")
     * @ORM\JoinColumn(name="service_offer_id", referencedColumnName="id")
     */
    protected $serviceOffer;

    /**
     * @ORM\ManyToOne(targetEntity="EmployeeGrade", inversedBy="priceLevels")
     * @ORM\JoinColumn(name="employee_grade_id", referencedColumnName="id")
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    protected $employeeGrade;

    /**
     * @var double
     *
     * @ORM\Column(name="price", type="float", nullable=true)
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    protected $price;

    /**
     * @var int
     *
     * @ORM\Column(name="duration", type="integer", nullable=true)
     *
     * @Serializer\Expose
     * @Serializer\Groups({"service-offer"})
     */
    protected $duration;
}

控制器:

public function putServiceOfferAction(Request $request)
    {
        try {
            $parameters = json_decode($request->getContent(), true);

            $serviceOffer = /* submit request data */;

            return $serviceOffer;
        } catch (InvalidFormException $e) {
            $serializer = $this->get('serializer');

            $response = new Response();
            $response->setStatusCode(400);
            $response->setContent($serializer->serialize($e->getForm()->getErrors(true), 'json'));
            return $response;
        }
    }

父母表格:

class ServiceOfferForm extends AbstractType
{

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'service_offer_form';
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'CoreBundle\Entity\ServiceOffer',
                'error_bubbling' => true,
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', HiddenType::class, array('mapped' => false));
        $builder->add('price_levels', CollectionType::class, array(
            'property_path' => 'priceLevels',
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => false,
            'entry_type' => ServiceOfferPriceLevelForm::class
        ));
    }
}

儿童形式:

  /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'CoreBundle\Entity\PriceLevel',
                'error_bubbling' => true,
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', HiddenType::class, array('mapped' => false));
        $builder->add('employee_grade', 'entity', array(
            'property_path' => 'employeeGrade',
            'class' => 'CoreBundle:EmployeeGrade',
        ));
        $builder->add('price', null, array(
            'property_path' => 'price',
        ));
        $builder->add('duration', null, array(
            'property_path' => 'duration',
        ));
    }
}

我发送给控制器的数据(放入有效载荷):

{
  "id": 2,
  "name": "asd",
  "price_levels": [
    {
      "id": 1,
      "employee_grade": 1,
      "price": 12.59,
      "duration": "25"
    },
    {
      "price": 360,
      "duration": "125"
    },
    {
      "price": 360,
      "duration": "125"
    }
  ]
}

作曲:

{
...
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.0.3",
        "doctrine/orm": "2.5.4",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/doctrine-cache-bundle": "^1.2",
        "symfony/swiftmailer-bundle": "^2.3",
        "symfony/monolog-bundle": "^2.8",
        "sensio/distribution-bundle": "^5.0",
        "sensio/framework-extra-bundle": "^3.0.2",
        "incenteev/composer-parameter-handler": "^2.0",
        "friendsofsymfony/user-bundle": "2.0.x-dev",
        "symfony/assetic-bundle": "^2.8",
        "twig/extensions": "^1.3",
        "knplabs/doctrine-behaviors": "^1.3",
        "vich/uploader-bundle": "^1.0",
        "jms/serializer-bundle": "^1.1",
        "liip/imagine-bundle": "^1.5",
        "nelmio/api-doc-bundle": "^2.11",
        "friendsofsymfony/jsrouting-bundle": "^1.6",
        "friendsofsymfony/rest-bundle": "^1.7",
        "twig/twig": "1.x-dev",
        "sonata-project/admin-bundle": "dev-master",
        "sonata-project/doctrine-orm-admin-bundle": "^2.3"
    }
...
}

结果(问题点): 不能写财产\" employeeGrade \"到一个数组。也许你应该把属性路径写成\" [employeeGrade] \"代替?

问题: 如何通过添加和删除来保存嵌入的信息?

如果我在DB中有一些行并尝试使用行symfony工作;如果我尝试删除行 - id未删除;如果我尝试添加行 - 它显示错误。

1 个答案:

答案 0 :(得分:0)

接下来是答案:

在表格课上改变一些时刻:

1)

use Symfony\Component\OptionsResolver\OptionsResolver;

而不是

use Symfony\Component\OptionsResolver\OptionsResolverInterface;

2)将方法从 setDefaultOptions 重命名为 configureOptions

它将symfony的版本从2.8更改为3.0.3