4

我有两个以一对一关系关联的教义实体。我试图反序列化从请求返回的 json 对象,但 JMS 没有设置关联的反面。我已经解决了几个小时没有解决方案。

代码如下。

<?php
namespace MyNamspace\Entity;

//Annotations namespaces
use JMS\SerializerBundle\Annotation as JMS;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="MyNamspace\Repository\Policy")
 * @ORM\Table(name="policies")
 * @JMS\ExclusionPolicy("all")
 */
class Policy
{
    /**
     * [$id description]
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @JMS\Expose

     * @JMS\AccessType("public_method")
     */
    protected $id;

    /**
     * [$name description]
     * @var string
     * @ORM\Column(type="string", length=360)
     * @JMS\Expose
     * @JMS\AccessType("public_method")
     * @JMS\Type("string")
     */
    protected $name;

    /**
     * [$policydata description]
     *
     * @ORM\OneToOne(targetEntity="PolicyData",mappedBy="policy",cascade={"persist","remove"})
     * @JMS\Type("MyNamspace\Entity\PolicyData")
     * @JMS\AccessType("public_method")
     * @JMS\Expose
     */
    protected $policydata;
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getName()
    {
        return $this->name;
    }
public function setPolicyData(\MyNamspace\Entity\PolicyData $policydata)
    {
        $this->policydata = $policydata;
        return $this;
    }

    public function getPolicyData(){
        return $this->policydata;
    }

第二个实体在下面;

<?php
namespace MyNamspace\Entity;

//Annotations namespaces
use JMS\SerializerBundle\Annotation as JMS;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="MyNamspace\Repository\PolicyData")
 * @ORM\Table(name="policies_data")
 * @JMS\ExclusionPolicy("all")
 */
class PolicyData {

    /**
     * [$id description]
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     * @JMS\Expose

     * @JMS\AccessType("public_method")
     */
    protected $id;

    /**
     * [$effect description]
     * @var integer
     * @ORM\Column(type="integer")
     * @JMS\Expose
     * @JMS\AccessType("public_method")
     * @JMS\Type("integer")
     *
     */

    protected $effect;

    /**
     * @ORM\OneToOne(targetEntity="Policy",inversedBy="policydata")
     * @ORM\JoinColumn(name="policy_id", referencedColumnName="id")
     * @JMS\AccessType("public_method")
     * @JMS\Type("MyNamspace\Entity\Policy")
     * @JMS\Expose
     */
    protected $policy;

    /**
     * [$startdate description]
     * @var datetime
     * @ORM\Column(type="datetime",nullable=true)
     * @JMS\Expose
     * @JMS\AccessType("public_method")
     * @JMS\Type("DateTime")
     *
     */

    protected $startdate;

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }
    public function getEffect()
    {
        return $this->effect;
    }

    public function setEffect($effect)
    {
        $this->effect = $effect;
        return $this;
    }

    public function setPolicy(\MyNamspace\Entity\Policy $policy)
    {
        $this->policy = $policy;
    }

    public function getPolicy()
    {
        return $this->policy;
    }

    public function getStartDate()
    {
        return $this->startdate;
    }

    public function setStartDate($startdate)
    {
        $this->startdate = $startdate;
        return $this;
    }

我正在使用 Silex,控制器提取代码是;

public function connect(Application $app)
    {
        //New controller from the factory
        $controller = $app['controllers_factory'];

        //Create a policy (either empty or complete or maybe halfway between the 2)
        $controller->post('/', function(Request $request) use ($app){

         // Need to intercept the request to format the dates or else JMS will not deserialize
            if($request->isMethod('POST'))
            {
                $jsonData = $request->getContent();
                $dateHandler = new DateHandler();
                $data = $dateHandler->formatDate($jsonData);
            }
            print_r($data);
            $policy = $app['serializer']->deserialize($data, 'Verloc\Entity\Policy', 'json');
print_r($policy); die;
            $policy = $app['service.rights']->addPolicy($policy);

            return new Response(
                $app['serializer']->serialize($policy, 'json'),
                200
                );
        });

假设被序列化的数据是;

{
    "name": "Policy1",
    "policydata": [
        {
            "effect": "1",
            "startdate": "2012-10-21T00:00:00+0100"
        }
    ]
}

序列化后得到的是这个;

MyNamspace\Entity\Policy Object
(
    [id:protected] => 
    [name:protected] => Policy1
    [policydata:protected] => MyNamspace\Entity\PolicyData Object
        (
            [id:protected] => 
            [effect:protected] => 
            [policy:protected] => 
            [startdate:protected] => 
        )

)

可以看出,在反序列化主 Policy 对象的同时,PolicyData 对象为空。

4

0 回答 0