ikucheriavenko
7/28/2017 - 1:19 PM

Better to look at on DoctrineExtension implementation. The best tutorial ever

Better to look at on DoctrineExtension implementation. The best tutorial ever

  public function getObserveFields($observable)
    {
        $em = $this->container->get('doctrine.orm.entity_manager');
        $reader = $this->container->get('annotation_reader');

        $meta = $em->getClassMetadata($observable);

        $class = $meta->getReflectionClass();
        if (!$class) {
            $class = new \ReflectionClass($class->name);
        }

        foreach ($class->getProperties() as $field) {
            if ($meta->isMappedSuperclass) {
                continue;
            }

            // versioned field
            if ($reader->getPropertyAnnotation($field, 'AppBundle\\Annotation\\Notification\\Observe')) {


                $mappedByField = null;
                foreach ($this->reader->getPropertyAnnotations($field) as $annotation) {
                    if ($annotation instanceof \Doctrine\ORM\Mapping\Annotation) {
                        $qw = 2;
                        $mappedByField = $annotation->mappedBy;
                    }
                };

                $fieldClass = $meta->getAssociationTargetClass($field->getName());
                //todo: mappedByField
                $props = $this->em->getRepository($fieldClass)->findAllByBrandId($notification->getObservableId());
                foreach ($props as $prop) {
                    $logEntryRepo = $this->em->getRepository('Gedmo\Loggable\Entity\LogEntry');

                    $lastRun = $notification->getLastRun();

                    if (null == $lastRun) {
                        $lastRun = $notification->getCreatedAt();
                    }

                    $logs = $logEntryRepo->getLogEntriesAfterDate($prop, $lastRun);
                    if (empty($logs)) {
                        continue;
                    }

                    $notificationObject->get($field->getName())->add($prop);
                }
            }
        }
    }