2017-08-14 34 views
1

當我有一個從另一個實體繼承的實體時,Platform API(https://api-platform.com/)存在問題。例如,從User實體繼承的Worker實體。當我去到平臺API文檔的所有Worker屬性出現在用戶類帶API平臺的繼承實體Symfony


勝於解釋的模式。這裏是我的兩個實體和問題

/** 
* User 
* 
* @ORM\Table(name="user") 
* @ORM\InheritanceType("SINGLE_TABLE") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"user" = "User", "worker" = "Worker"}) 
* @ApiResource 
* 
*/ 
class User 
{ 
/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** 
* @var string 
* 
* @ORM\Column(name="lastname", type="string", length=255) 
*/ 
protected $lastname; 

/** 
* @var string 
* 
* @ORM\Column(name="firstname", type="string", length=255) 
*/ 
protected $firstname; 


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

/** 
* Set lastname 
* 
* @param string $lastname 
* @return User 
*/ 
public function setLastname($lastname) 
{ 
    $this->lastname = $lastname; 

    return $this; 
} 

/** 
* Get lastname 
* 
* @return string 
*/ 
public function getLastname() 
{ 
    return $this->lastname; 
} 

/** 
* Set firstname 
* 
* @param string $firstname 
* @return User 
*/ 
public function setFirstname($firstname) 
{ 
    $this->firstname = $firstname; 

    return $this; 
} 

/** 
* Get firstname 
* 
* @return string 
*/ 
public function getFirstname() 
{ 
    return $this->firstname; 
} 
} 


/** 
* Worker 
* 
* @ApiResource 
*/ 
class Worker extends User 
{ 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="birthday", type="datetime") 
* @Assert\NotNull() 
* @Assert\DateTime() 
*/ 
private $birthday; 

/** 
* @var string 
* 
* @ORM\Column(name="mobilePhone", type="string", length=255, nullable=true) 
*/ 
private $mobilePhone; 

/** 
* @return \DateTime 
*/ 
public function getBirthday(): \DateTime 
{ 
    return $this->birthday; 
} 

/** 
* @param \DateTime $birthday 
*/ 
public function setBirthday(\DateTime $birthday) 
{ 
    $this->birthday = $birthday; 
} 

/** 
* @return string 
*/ 
public function getMobilePhone(): string 
{ 
    return ($this->mobilePhone == null) ? '' : $this->mobilePhone; 
} 

/** 
* @param string $mobilePhone 
*/ 
public function setMobilePhone(string $mobilePhone) 
{ 
    $this->mobilePhone = $mobilePhone; 
} 

} 

文檔的結果,這是問題。我們看到Worker類的屬性出現在User類的模型中。而且,當我測試發送它返回一個包含工人實體的

The result to see the problem

謝謝所有

+0

顯示您的API文檔註釋請(此端點) –

+0

我只是註釋此PHP文件 – ascodix

回答

0

你的序列化需要去深度將增加一個級別的屬性的用戶實體POST方法支持響應中序列化的工作人員屬性。我不確定如何專門使其適用於您的實施。

隨着JMS Serialization bundle,看起來像這樣:

/** 
* Get the Birthday.. 
* 
* @MaxDepth(2) 
* @return \DateTime 
*/ 
public function getBirthday(): \DateTime 
{ 
    return $this->birthday; 
}