JMS Serializer事件订阅者筛选出代理类

时间:2017-09-18 01:47:47

标签: php doctrine-orm symfony-2.3 jmsserializerbundle jms-serializer

我有一个实体Acme\Bundle\Entity\Author,它与Acme\Bundle\Entity\Article有一对多关系,我有两个实体的两个JMS序列化器事件订阅者。

Acme\Bundle\Entity\Author;

的订阅者
acm.author_serialization_listener:
    class: Acm\Bundle\Listener\AuthorSerializationListener
    tags:
        - { name: jms_serializer.event_subscriber }
class AuthorSerializationListener implements EventSubscriberInterface
    {
        /**
         * @inheritdoc
         */
        public static function getSubscribedEvents()
        {
            return array(
                array('event' => 'serializer.pre_serialize', 'class' => 'Acme\Bundle\Entity\Author' 'method' => 'onPreSerialize'),
            );
        }

    public function onPreSerialize(PreSerializeEvent $event)
    {
        if($event->getObject() instanceof Author) {
            return;
        }
    }
}

Acme\Bundle\Entity\Article;

的订阅者
acm.article_serialization_listener:
    class: Acm\Bundle\Listener\ArticleSerializationListener
    tags:
        - { name: jms_serializer.event_subscriber }
class ArticleSerializationListener implements EventSubscriberInterface
    {
        /**
         * @inheritdoc
         */
        public static function getSubscribedEvents()
        {
            return array(
                array('event' => 'serializer.pre_serialize', 'class' => 'Acme\Bundle\Entity\Article' 'method' => 'onPreSerialize'),
            );
        }

        public function onPreSerialize(PreSerializeEvent $event)
        {
            if($event->getObject() instanceof Article) {
                return;
            }
        }
    }

onPreSerialize函数在Author序列化Article以及ArticleSerializationListener订阅者类中的相关Article时出现问题,其中代理\ __ CG __ \ Acme \ Bundle \ Entity \ Article 而不是 Acme \ Bundle \ Entity \ Article 导致排除相关attribute('innerHTML')实体的实体。有什么我做错了或有解决方法吗?

1 个答案:

答案 0 :(得分:0)

代替private void takeDataFromFirebase(){ Query my_message= mRef.child(USERS_CHILD) .orderByChild(USER_MESSAGES); my_message.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String { } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(DatabaseError databaseError) { } }); } ,您应该只使用运算符get_class,因为代理继承自实体。你比较两个字符串。

instanceof

Sidenode:运营商if($event->getObject() instanceof \Acme\Bundle\Entity\Article) { return; } 也不存在。