我想在我的管理类中添加个人资料图片,但是我收到了这个错误: 没有与上下文相关的主要类别:默认 我搜索了很多但没有解决方案。这是我的代码 的 config.yml
# app/config/config.yml
sonata_media:
class:
media: Application\Sonata\MediaBundle\Entity\Media
gallery: Application\Sonata\MediaBundle\Entity\Gallery
gallery_has_media: Application\Sonata\MediaBundle\Entity\GalleryHasMedia
category: Application\Sonata\ClassificationBundle\Entity\Category
default_context: default # you need to set a context
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
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}
# other contexts here
engine:
providers:
- sonata.media.provider.image
formats:
preview: { width: 100, quality: 100}
small: { width: 200, quality: 100}
large: { width: 600, quality: 100}
cdn:
server:
path: /uploads/media # http://media.sonata-project.org/
filesystem:
local:
directory: "%kernel.root_dir%/../web/uploads/media"
create: false
providers:
image:
service: sonata.media.provider.image
resizer: sonata.media.resizer.square # sonata.media.resizer.square
filesystem: sonata.media.filesystem.local
cdn: sonata.media.cdn.server
generator: sonata.media.generator.default
thumbnail: sonata.media.thumbnail.format
allowed_extensions: ['jpg', 'png', 'jpeg']
allowed_mime_types: ['image/pjpeg', 'image/jpeg', 'image/png', 'image/x-png']
file:
service: sonata.media.provider.file
resizer: false
filesystem: sonata.media.filesystem.local
cdn: sonata.media.cdn.server
generator: sonata.media.generator.default
thumbnail: sonata.media.thumbnail.format
allowed_extensions: ['pdf', 'txt', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'odg', 'odp', 'ods', 'odc', 'odf', 'odb', 'csv', 'xml', 'sql']
allowed_mime_types: ['text/csv','application/pdf', 'application/x-pdf', 'application/rtf', 'application/octet-stream', 'text/html', 'text/rtf','application/sql', 'text/plain','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel','application/x-iwork-numbers-sffnumbers','application/vnd.apple.numbers']
doctrine:
orm:
entity_managers:
default:
mappings:
FOSUserBundle: ~
SonataMediaBundle: ~
dbal:
types: #this is about this line and line below
json: \Doctrine\DBAL\Types\StringType
userAdmin
/**
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper
*
* @return void
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('username')
->add('email')
->add('enabled')
->add('firstname')
->add('lastname')
->add('plainPassword', 'password', array(
'required' => (!$this->getSubject() || is_null($this->getSubject()->getId())),
))
->add('media', 'sonata_media_type',
array(
'provider' => 'sonata.media.provider.image',
'context' => 'default',
//'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',
'required' => false)
)
->end();
}
用户实体
class User extends BaseUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var
*
* @ORM\Column(name="firstname", type="string")
*/
protected $firstname;
/**
* @var
*
* @ORM\Column(name="lastname", type="string")
*/
protected $lastname;
/**
* @var \Application\Sonata\MediaBundle\Entity\Media
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
*/
protected $media;
/**
* Set media
*
* @param \Application\Sonata\MediaBundle\Entity\Media $media
* @return User
*/
public function setMedia(\Application\Sonata\MediaBundle\Entity\Media $media = null)
{
$this->media = $media;
return $this;
}
/**
* Get media
*
* @return \Application\Sonata\MediaBundle\Entity\Media
*/
public function getMedia()
{
return $this->media;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return String
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* @param String $firstname
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
/**
* @return String
*/
public function getLastname()
{
return $this->lastname;
}
/**
* @param String $lastname
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
}
任何帮助?抱歉英语。
答案 0 :(得分:3)
在Github页面上有an issue where a contributor suggests运行此命令:
php app/console sonata:classification:fix-context
您收到此错误的原因是您尝试在上下文default
下添加媒体,但上下文与类别无关(链接)。
答案 1 :(得分:0)