это моя сущность
Код: Выделить всё
class Notifications
{
use TraitTimestampable;
const GROUP_POST_NOTIFICATION = 'post_notifications';
const GROUP_GET_NOTIFICATIONS = 'get_notifications';
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Annotation\Groups({
* "get_notifications"
* })
*/
private $id;
/**
* @var object
*
* @Annotation\Groups({
* "get_notifications"
* })
*/
private $providerEntity;
Код: Выделить всё
class Questions implements NotificationInterface
{
use TraitTimestampable;
const GROUP_POST = 'post_question';
const GROUP_GET = 'get_question';
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Annotation\Groups({
* "get_question"
* })
*/
private $id;
/**
* @return array
*/
public static function getGetGroup()
{
return [self::GROUP_GET];
}
Код: Выделить всё
public function onPreSerializeNotifications(PreSerializeEvent $event)
{
/** @var Notifications $notifications*/
$notifications = $event->getObject();
$provider = $notifications->getProvider(); //this is Questions::class
$this->providerEntity = $this->entityManager->getRepository($provider)
->findOneBy(['id' => $notifications->getProviderId()]);
$notifications->setProviderEntity($this->providerEntity);
$attr = $event->getContext()->attributes;
$groups = array_merge($attr->all()['groups'], $provider::getGetGroup());
$attr->set('groups', $groups);
}
Код: Выделить всё
"notifications": [
{
"id": 2,
"provider_entity": {}
}
]
Подробнее здесь: https://stackoverflow.com/questions/495 ... -condition
Мобильная версия