Symfony3 - 序列化嵌套实体

时间:2017-04-23 09:15:13

标签: symfony doctrine

所以我有几个学说实体,一个订阅和一个订阅者。单个订户有许多订阅(manyToOne)。我为两个实体编写了自定义规范化程序,但是在订阅服务器标准化为JSON后,无法让订阅服务器显示在订阅服务中。

我能够让它发挥作用的唯一方法就是通过“订阅者”#39;规范化到“订阅”#39; normailizer。看起来我应该能够使用SerializerAwareNormalizer Trait或类似的东西来让Symfony递归地规范化我的相关实体。

<?php
namespace acme\MarketingBundle\Normalizer;

use acme\MarketingBundle\Entity\Subscription;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class SubscriptionNormalizer implements NormalizerInterface
{


    private $subscriberNormalizer;

    public function __construct($subscriberNormalizer)
    {
        $this->subscriberNormalizer = $subscriberNormalizer;
    }

    public function normalize($subscription, $format = null, array $context = [])
    {

        /* @var $subscription Subscription */
        $subscriber = $subscription->getSubscriber();

        return [
            "id" => $subscription->getId(),
            "subscriber" => $this->subscriberNormalizer->normalize($subscriber, $format)
        ];
    }



    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof Subscription;
    }

}

和规范化器......

UPDATE SASDB.TEMP_VALIDATE_FIN_ENTRIES 
set (VALIDATION_REMARKS, VALIDATION_STATUS)=
(

CASE  
WHEN  LENGTH(TRIM(NVL(ACCOUNT_ID, '0')) = 16 AND TRIM(TRANSLATE(NVL(ACCOUNT_ID, '0'),'0123456789',' '))=''
    AND LENGTH(TRIM(NVL(SOL_ID, '0'))) = 4 AND TRIM(TRANSLATE(NVL(SOL_ID, '0'),'0123456789',' '))=''
    AND TRIM(TRANSLATE(NVL(DEBIT_TRANS_AMT, '0'),'0123456789,.',' '))=''
    AND TRIM(TRANSLATE(NVL(CREDIT_TRANS_AMT, '0'),'0123456789,.',' '))='' THEN 'ACCOUNTS ARE VALIDATED SUCCESSFULLY'
WHEN LENGTH(TRIM(NVL(ACCOUNT_ID, '0'))) <> 16 THEN 'ACCOUNT_ID SHOULD BE LENGTH  16'
WHEN LENGTH(TRIM(NVL(SOL_ID, '0'))) <> 4        THEN 'SOL_ID HAS NOT LENGTH  4'
WHEN TRIM(TRANSLATE(NVL(SOL_ID, '0'),'0123456789',' '))<>'' THEN 'SOL_ID HAS ALPHABET OR SPECAIL CHARACTER'
WHEN TRIM(TRANSLATE(NVL(ACCOUNT_ID, '0'),'0123456789',' '))<>'' THEN 'ACCOUNT_ID HAS ALPHABET OR SPECAIL CHARACTER'
WHEN TRIM(TRANSLATE(NVL(CREDIT_TRANS_AMT, '0'),'0123456789,.',' '))<>'' THEN 'CREDIT TRANASTION AMOUNT HAS SPECIAL CHARACTER'
WHEN TRIM(TRANSLATE(NVL(DEBIT_TRANS_AMT, '0'),'0123456789,.',' '))<>'' THEN 'DEBIT TRANASTION AMOUNT HAS SPECIAL CHARACTER'
WHEN ACCOUNT_EXIT_FLAG IS NULL THEN 'ACCOUNT DOESNOT EXIST IN DATABASE'
ELSE 'CASE NOT TRAITED' 
END,

CASE  
WHEN  LENGTH(TRIM(NVL(ACCOUNT_ID, '0')) = 16 AND TRIM(TRANSLATE(NVL(ACCOUNT_ID, '0'),'0123456789',' '))=''
    AND LENGTH(TRIM(NVL(SOL_ID, '0'))) = 4 AND TRIM(TRANSLATE(NVL(SOL_ID, '0'),'0123456789',' '))=''
    AND TRIM(TRANSLATE(NVL(DEBIT_TRANS_AMT, '0'),'0123456789,.',' '))=''
    AND TRIM(TRANSLATE(NVL(CREDIT_TRANS_AMT, '0'),'0123456789,.',' '))='' THEN 'YES'
WHEN 
LENGTH(TRIM(NVL(ACCOUNT_ID, '0'))) <> 16 OR 
LENGTH(TRIM(NVL(SOL_ID, '0'))) <> 4 OR
TRIM(TRANSLATE(NVL(SOL_ID, '0'),'0123456789',' '))<>'' OR
TRIM(TRANSLATE(NVL(ACCOUNT_ID, '0'),'0123456789',' '))<>'' OR
TRIM(TRANSLATE(NVL(CREDIT_TRANS_AMT, '0'),'0123456789,.',' '))<>'' OR
TRIM(TRANSLATE(NVL(DEBIT_TRANS_AMT, '0'),'0123456789,.',' '))<>'' OR
ACCOUNT_EXIT_FLAG IS NULL THEN 'NO' THEN 'NO'
ELSE 'CASE NOT TRAITED' 
END

)

有没有更好的方法来实现这一目标?

1 个答案:

答案 0 :(得分:9)

在谷歌上花了几个小时,无法弄明白。在SO上发布,5分钟后点击右边的谷歌链接:(。答案似乎是在自定义规范化器上实现NormalizerAwareInterface,然后使用NormalizerAwareTrait来访问嵌套实体的规范化器。

<?php
namespace acme\MarketingBundle\Normalizer;

use acme\MarketingBundle\Entity\Subscription;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class SubscriptionNormalizer implements NormalizerInterface, NormalizerAwareInterface
{

    use NormalizerAwareTrait;

    public function normalize($subscription, $format = null, array $context = [])
    {

        return [
            "id" => $subscription->getId(),
            "subscriber" => $this->normalizer->normalize($subscription->getSubscriber())
        ];
    }



    public function supportsNormalization($data, $format = null)
    {
        return $data instanceof Subscription;
    }

}