2
votes

I have a problem with the Platform API (https://api-platform.com/) when I have an entity that inherits from another entity. For example, Worker entity that inherits from User entity. When I go to the Platform API documentation all the Worker properties appear in the User class


It is better a schema than an explanation. Here are my two entities and the result of the documentation with the problem

/**
* 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;
}

}

And here is the problem. We see that the properties of the Worker child class appear in the model of the User class. And that when I test sending the POST method it returns a User entity that contains the properties of the Worker entity

The result to see the problem

1
show your api doc annotation please(for this endpoint) - Petro Popelyshko
I have just this Php file with annotation - ascodix
So the Worker endpoint is not linked to another table ? Extra column are stored in the User table ? - Nicolas Janel
@ascodix did you find a solution? I have the same issue - user2002836

1 Answers

0
votes

Your serialization needs to go an additional level of depth to support the worker properties being serialized in the response. I'm not sure how to specifically make it work for your implementation.

With JMS Serialization bundle, that looks like this:

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