Symfony -> SonataMediaBundle -> Галерея |Галерея сохранила только одну галереюHasMedia - PullRequest
0 голосов
/ 01 ноября 2019

Мой проект включает в себя: framework symfony, sonataMediaBundle, доктрину и т. Д. И я не могу сохранить более 1 фотографии (galleryHasMedia) для галереи.

Шаг 1. Открыть галерею
Шаг 2. Добавить изображение в галерею
Шаг 3. Нажмите «Сохранить». Галерея сохранила только одно изображение.

vendor / sonata-project / media-bundle / src / Admin / GalleryAdmin.php

class GalleryAdmin extends AbstractAdmin {
/**
 * @var Pool
 */
protected $pool;

protected $classnameLabel = 'Gallery';

/**
 * @param string $code
 * @param string $class
 * @param string $baseControllerName
 * @param Pool   $pool
 */
public function __construct($code, $class, $baseControllerName, Pool $pool)
{
    parent::__construct($code, $class, $baseControllerName);

    $this->pool = $pool;
}

/**
 * {@inheritdoc}
 */
public function prePersist($gallery)
{
    $parameters = $this->getPersistentParameters();
    $gallery->setContext($parameters['context']);

    foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
        $galleryHasMedia->setGallery($gallery);
    }
}

/**
 * {@inheritdoc}
 */
public function preUpdate($gallery)
{
    foreach ($gallery->getGalleryHasMedias() as $galleryHasMedia) {
        $galleryHasMedia->setGallery($gallery);
    }
}

/**
 * {@inheritdoc}
 */
public function postUpdate($gallery)
{
    $gallery->reorderGalleryHasMedia();
}

/**
 * {@inheritdoc}
 */
public function getPersistentParameters()
{
    $parameters = parent::getPersistentParameters();

    if (!$this->hasRequest()) {
        return $parameters;
    }

    return array_merge($parameters, [
        'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
    ]);
}

/**
 * {@inheritdoc}
 */
public function getNewInstance()
{
    $gallery = parent::getNewInstance();

    if ($this->hasRequest()) {
        $gallery->setContext($this->getRequest()->get('context'));
    }

    return $gallery;
}

/**
 * {@inheritdoc}
 */
protected function configureFormFields(FormMapper $formMapper)
{
    // define group zoning
    $formMapper
        ->with('Gallery', ['class' => 'col-md-9'])->end()
        ->with('Options', ['class' => 'col-md-3'])->end()
    ;

    $context = $this->getPersistentParameter('context');

    if (!$context) {
        $context = $this->pool->getDefaultContext();
    }

    $formats = [];
    foreach ((array) $this->pool->getFormatNamesByContext($context) as $name => $options) {
        $formats[$name] = $name;
    }

    $contexts = [];
    foreach ((array) $this->pool->getContexts() as $contextItem => $format) {
        $contexts[$contextItem] = $contextItem;
    }


    $formMapper
        ->with('Options')
            ->add('context', ChoiceType::class, ['choices' => $contexts])
            ->add('enabled', null, ['required' => false])
            ->add('name')
            ->ifTrue($formats)
                ->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
            ->ifEnd()
        ->end()
        ->with('Gallery')
            ->add('galleryHasMedias', CollectionType::class,
                [
                    'by_reference' => false,
                    'required' => false,
                ], [
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable' => 'position',
                    'link_parameters' => ['context' => $context],
                    'admin_code' => 'sonata.media.admin.gallery_has_media'
            )
        ->end()
    ;
}

/**
 * {@inheritdoc}
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('name')
        ->add('enabled', 'boolean', ['editable' => true])
        ->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
        ->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle'])
    ;
}

/**
 * {@inheritdoc}
 */
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('name')
        ->add('enabled')
        ->add('context', null, [
            'show_filter' => false,
        ])
    ;
}

}

vendor /соната-проект / media-bundle / src / Admin / GalleryHasMediaAdmin.php

class GalleryHasMediaAdmin extends AbstractAdmin {
/**
 * {@inheritdoc}
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $link_parameters = [];

    if ($this->hasParentFieldDescription()) {
        $link_parameters = $this->getParentFieldDescription()->getOption('link_parameters', []);
    }

    if ($this->hasRequest()) {
        $context = $this->getRequest()->get('context', null);

        if (null !== $context) {
            $link_parameters['context'] = $context;
        }
    }

    $formMapper
        ->add('media', ModelListType::class, ['required' => false], [
            'link_parameters' => $link_parameters,
        ])
        ->add('enabled', null, ['required' => false])
        ->add('position', HiddenType::class)
    ;
}

/**
 * {@inheritdoc}
 */
protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->add('media')
        ->add('gallery')
        ->add('position')
        ->add('enabled')
    ;
}

}

vendor / соната-проект / media-bundle / src / Model / Gallery.php

abstract class Gallery implements GalleryInterface, GalleryMediaCollectionInterface {
/**
 * @var string
 */
protected $context;

/**
 * @var string
 */
protected $name;

/**
 * @var bool
 */
protected $enabled;

/**
 * @var \DateTime|null
 */
protected $updatedAt;

/**
 * @var \DateTime|null
 */
protected $createdAt;

/**
 * @var string
 */
protected $defaultFormat = MediaProviderInterface::FORMAT_REFERENCE;

/**
 * @var GalleryHasMediaInterface[]|Collection
 */
protected $galleryHasMedias;

public function __construct()
{
    $this->galleryHasMedias = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * {@inheritdoc}
 */
public function __toString()
{
    return $this->getName() ?: '-';
}

/**
 * {@inheritdoc}
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * {@inheritdoc}
 */
public function getName()
{
    return $this->name;
}

/**
 * {@inheritdoc}
 */
public function setEnabled($enabled)
{
    $this->enabled = $enabled;
}

/**
 * {@inheritdoc}
 */
public function getEnabled()
{
    return $this->enabled;
}

/**
 * {@inheritdoc}
 */
public function setUpdatedAt(\DateTime $updatedAt = null)
{
    $this->updatedAt = $updatedAt;
}

/**
 * {@inheritdoc}
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

/**
 * {@inheritdoc}
 */
public function setCreatedAt(\DateTime $createdAt = null)
{
    $this->createdAt = $createdAt;
}

/**
 * {@inheritdoc}
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * {@inheritdoc}
 */
public function setDefaultFormat($defaultFormat)
{
    $this->defaultFormat = $defaultFormat;
}

/**
 * {@inheritdoc}
 */
public function getDefaultFormat()
{
    return $this->defaultFormat;
}

/**
 * {@inheritdoc}
 */
public function setGalleryHasMedias($galleryHasMedias)
{
    $this->galleryHasMedias = new ArrayCollection();

    foreach ($galleryHasMedias as $galleryHasMedia) {
        $this->addGalleryHasMedia($galleryHasMedia);
    }
}

/**
 * {@inheritdoc}
 */
public function getGalleryHasMedias()
{
    return $this->galleryHasMedias;
}

/**
 * {@inheritdoc}
 */
public function addGalleryHasMedia(GalleryHasMediaInterface $galleryHasMedia)
{
    $this->galleryHasMedias[] = $galleryHasMedia;

    $galleryHasMedia->setGallery($this);
}

/**
 * {@inheritdoc}
 */
public function removeGalleryHasMedia(GalleryHasMediaInterface $galleryHasMedia)
{
    $this->galleryHasMedias->removeElement($galleryHasMedia);

    $galleryHasMedia->setGallery();
}

/**
 * {@inheritdoc}
 *
 * @deprecated use addGalleryHasMedia method instead
 * NEXT_MAJOR: remove this method with the next major release
 */
public function addGalleryHasMedias(GalleryHasMediaInterface $galleryHasMedia)
{
    @trigger_error(
        'The '.__METHOD__.' is deprecated and will be removed with next major release.'
        .'Use `addGalleryHasMedia` method instead.',
        E_USER_DEPRECATED
    );

    $this->addGalleryHasMedia($galleryHasMedia);
}

/**
 * {@inheritdoc}
 */
public function setContext($context)
{
    $this->context = $context;
}

/**
 * {@inheritdoc}
 */
public function getContext()
{
    return $this->context;
}

/**
 * Reorders $galleryHasMedia items based on their position.
 */
public function reorderGalleryHasMedia()
{
    if ($this->getGalleryHasMedias() && $this->getGalleryHasMedias() instanceof \IteratorAggregate) {
        // reorder
        $iterator = $this->getGalleryHasMedias()->getIterator();

        $iterator->uasort(static function ($a, $b) {
            if ($a->getPosition() === $b->getPosition()) {
                return 0;
            }

            return $a->getPosition() > $b->getPosition() ? 1 : -1;
        });

        $this->setGalleryHasMedias($iterator);
    }
}

}

...