Symfony 4 中的 Sonata Media Bundle 模板
Sonata Media Bundle Template in Symfony 4
我在 Symfony 4 中安装了 Sonata Media Bundle,一切正常,但在 Symfony 3 方面有些不同。
我在 Sonata Admin 中看不到服务,当我将 sonata media bundle 字段添加到 Admin 时 Class 这显示了不同的模板。
这里是图片:
Sonata Media Bundle template - Symfony 4, in User Entity
Sonata Media Bundle template - Symfony 3, in User Entity
Sonata Media Bundle template - Symfony 3, Adding new image
如您所见,该模板在 Symfony 4 中无法正常工作,或者我的代码中遗漏了某些内容。
我的奏鸣曲媒体配置
sonata_media.yaml
sonata_media:
class:
media: App\Application\Sonata\MediaBundle\Entity\Media
gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
default_context: default
contexts:
default:
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
cdn:
server:
path: /upload/media
filesystem:
local:
# Directory for uploads should be writable
directory: "%kernel.project_dir%/public/upload/media"
create: false
providers:
# ...
file:
# the resizer must be set to false, otherwhise this can delete icon files from the fs
resizer: false
image:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
vimeo:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
youtube:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
dailymotion:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
我的用户管理员Class
// src/Admin/OgaUsersAdmin.php
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstName', TextType::class)
->add('userCollection', MediaType::class, array(
'provider' => 'sonata.media.provider.image',
'context' => 'default'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('userFirstName');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('userFirstName');
}
}
我的用户实体和媒体包字段
namespace App\Entity;
use Application\Sonata\MediaBundle\Entity\Media;
use Doctrine\ORM\Mapping as ORM;
/**
* OgaUsers
*
* @ORM\Table(name="oga_users", indexes={@ORM\Index(name="memb_id_idx", columns={"memb_id"}), @ORM\Index(name="comp_id_idx", columns={"comp_id"}), @ORM\Index(name="u_ui_id_idx", columns={"user_collection"})})
* @ORM\Entity
*/
class OgaUsers
{
/**
* @var int
*
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Media")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userCollection", referencedColumnName="id")
* })
*/
private $userCollection;
Getter 和塞特
public function getUserCollection(): ?\App\Application\Sonata\MediaBundle\Entity\Media
{
return $this->userCollection;
}
public function setUserCollection(?\App\Application\Sonata\MediaBundle\Entity\Media $userCollection): self
{
$this->userCollection = $userCollection;
return $this;
}
谢谢
经过几周的搜索。
我有答案。
首先我需要将此行添加到我的 sonata_media.yaml,以便在管理仪表板中查看媒体库。
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
在Admin中configureFormFields后,只需要在media字段添加ModellistType::class即可。
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstname', TextType::class)
->add('userImage', ModelListType::class);
}
希望对您有所帮助!!
我在 Symfony 4 中安装了 Sonata Media Bundle,一切正常,但在 Symfony 3 方面有些不同。
我在 Sonata Admin 中看不到服务,当我将 sonata media bundle 字段添加到 Admin 时 Class 这显示了不同的模板。
这里是图片:
Sonata Media Bundle template - Symfony 4, in User Entity
Sonata Media Bundle template - Symfony 3, in User Entity
Sonata Media Bundle template - Symfony 3, Adding new image
如您所见,该模板在 Symfony 4 中无法正常工作,或者我的代码中遗漏了某些内容。
我的奏鸣曲媒体配置
sonata_media.yaml
sonata_media:
class:
media: App\Application\Sonata\MediaBundle\Entity\Media
gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
default_context: default
contexts:
default:
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
cdn:
server:
path: /upload/media
filesystem:
local:
# Directory for uploads should be writable
directory: "%kernel.project_dir%/public/upload/media"
create: false
providers:
# ...
file:
# the resizer must be set to false, otherwhise this can delete icon files from the fs
resizer: false
image:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
vimeo:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
youtube:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
dailymotion:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
我的用户管理员Class
// src/Admin/OgaUsersAdmin.php
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstName', TextType::class)
->add('userCollection', MediaType::class, array(
'provider' => 'sonata.media.provider.image',
'context' => 'default'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('userFirstName');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('userFirstName');
}
}
我的用户实体和媒体包字段
namespace App\Entity;
use Application\Sonata\MediaBundle\Entity\Media;
use Doctrine\ORM\Mapping as ORM;
/**
* OgaUsers
*
* @ORM\Table(name="oga_users", indexes={@ORM\Index(name="memb_id_idx", columns={"memb_id"}), @ORM\Index(name="comp_id_idx", columns={"comp_id"}), @ORM\Index(name="u_ui_id_idx", columns={"user_collection"})})
* @ORM\Entity
*/
class OgaUsers
{
/**
* @var int
*
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Media")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userCollection", referencedColumnName="id")
* })
*/
private $userCollection;
Getter 和塞特
public function getUserCollection(): ?\App\Application\Sonata\MediaBundle\Entity\Media
{
return $this->userCollection;
}
public function setUserCollection(?\App\Application\Sonata\MediaBundle\Entity\Media $userCollection): self
{
$this->userCollection = $userCollection;
return $this;
}
谢谢
经过几周的搜索。
我有答案。
首先我需要将此行添加到我的 sonata_media.yaml,以便在管理仪表板中查看媒体库。
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
在Admin中configureFormFields后,只需要在media字段添加ModellistType::class即可。
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstname', TextType::class)
->add('userImage', ModelListType::class);
}
希望对您有所帮助!!