- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
作为 Symfony 的新手,花了最后几天的时间尝试找到一个很好的解决方案,将图像上传字段集成到 Sonata Admin 中的现有实体中。我一直在谷歌搜索并遵循可用的指南,但不幸的是我无法弄清楚。
标题中指定的设置
Symfony2奏鸣曲管理员VichUploaderBundle
我目前遇到以下错误:
"Catchable Fatal Error: Argument 1 passed to Beautify\BeautiesBundle\Entity\Beauty::setImageFile() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, instance of Symfony\Component\HttpFoundation\File\File given, called in /Users/gmf/symfony/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 438 and defined in /Users/gmf/symfony/src/Beautify/BeautiesBundle/Entity/Beauty.php line 75 "
图片上传字段正确呈现,提交后收到错误。
这是第 75 行:
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile
*/
public function setImageFile(UploadedFile $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
她的是我其他密切相关的文件:
实体文件:
<?php
// src/Beautify/BeautiesBundle/Entity/Beauty.php
namespace Beautify\BeautiesBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="beauties")
* @ORM\HasLifecycleCallbacks
* @Vich\Uploadable
*/
class Beauty
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="text")
*/
protected $content;
/**
* @Vich\UploadableField(mapping="beauty_image", fileNameProperty="imageName")
* @var File $imageFile
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="image_name")
* @var string $imageName
*/
protected $imageName;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="beauties")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
public function __toString()
{
return ($this->getName()) ? : '';
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Beauty
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set content
*
* @param string $content
* @return Beauty
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set category
*
* @param \Beautify\BeautiesBundle\Entity\Category $category
* @return Beauty
*/
public function setCategory(\Beautify\BeautiesBundle\Entity\Category $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return \Beautify\BeautiesBundle\Entity\Category
*/
public function getCategory()
{
return $this->category;
}
}
#ADMIN 文件
<?php
// src/Beautify/BeautiesBundle/Admin/BeautyAdmin.php
namespace Beautify\BeautiesBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Knp\Menu\ItemInterface as MenuItemInterface;
use Beautify\CategoryBundle\Entity\Category;
class BeautyAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('name', 'text', array('label' => 'Name'))
->add('content', 'textarea', array('label' => 'Content'))
->add('imageFile', 'file', array('label' => 'Image file', 'required' => false))
->end()
->with('Category')
->add('category', 'sonata_type_model', array('expanded' => true))
->end()
;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('name')
->add('content')
->add('category')
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('content')
->add('category')
->add('_action', 'actions', array(
'actions' => array(
'show' => array(),
'edit' => array(),
'delete' => array(),
)
))
;
}
}
**#SERVICES FILE**
report.listener:
class: Beautify\BeautiesBundle\Entity\Beauty
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
**#CONFIG.YML**
# Your other blocks
vich_uploader:
db_driver: orm
mappings:
beauty_image:
uri_prefix: /images/beauties
upload_destination: %kernel.root_dir%/../images
非常感谢任何帮助,我会很高兴
最佳答案
您错过了 File
类的 use
语句:use Symfony\Component\HttpFoundation\File\File;
关于php - Symfony2 + SonataAdmin + VichUploaderBundle - 图片上传字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862408/
我正在使用Symfony2和SonataAdminBundle。 我需要知道如何在仪表板的左侧添加菜单吗?就像在屏幕上一样: 我需要在页面的左侧部分放置仪表板块(在我的屏幕上呈深灰色)..我该怎么做?
我正在尝试将项目/路线添加到侧边菜单,基本上我有用户,它在仪表板中列出了列表和添加功能,现在我也想在我的侧边栏菜单下添加这些。 我注册了一个服务: #config/services.yml a
我正在使用 SonataAdminBundle,我想知道如何在 edit 中添加自定义表单操作(类似于 Save、Update 和关闭) 似乎没有任何关于它的记录。 我正在尝试添加一个自定义输入字段,
我正在尝试将自定义 block 添加到 SonataAdminBundle 的仪表板。我按照此处的说明 (How to add custom link or button to SonataAdmin
当我尝试列出数据库中的记录时,我遇到了问题,因为我得到的值存储在行中,它是一个整数,但我想要字符串。我如何处理表格?这样: protected function configureFormFields
最近几天我一直在谷歌上疯狂地搜索(没有成功)如何覆盖 SonataAdmin 操作以捕获 session 用户名并将其保存在外键字段中。 附件管理 Controller 类: get('request
我想使用命令 sonata:admin:generate 从实体生成管理员,但每次都会出现以下错误: [RuntimeException] 没有模型经理注册。 知道为什么会这样吗? 最佳答案 我会猜测
作为 Symfony 的新手,花了最后几天的时间尝试找到一个很好的解决方案,将图像上传字段集成到 Sonata Admin 中的现有实体中。我一直在谷歌搜索并遵循可用的指南,但不幸的是我无法弄清楚。
我有一个简单的问题,如何访问 sonataAdmin 中的表单返回的原始 POST 数据? 我有一个使用 Javascript 创建表单的页面,我需要检索这些表单中的数据。问题是这些表单不是实体的属性
我有一个 Sonata Admin 类,其中包含一些表单字段,但我想使用 FormEvents::PRE_SET_DATA事件以根据绑定(bind)的数据动态添加另一个表单字段。 但是,我遇到了几个问
我有一个带有许多状态代码的实体事务。 我希望用户能够在 SonataAdmin 中将这些状态代码视为字符串。用户还应该能够根据这些状态代码进行过滤。 Entity Transaction {
我一直在尝试安装 SonataAdminBundle。我想我遵循了文档,另外我还阅读了一些关于它的博客。但我做不到。 当转到管理/仪表板页面时,我看到了 Sonata Title,我可以在右侧看到 s
在 Symfony2 中处理字符串表示和数字代码翻译的最佳方法是什么? 假设我有这样一个实体: status; } } 在前端和 SonataAdmin 后端,我不想显示数字代码,而是显示字符
首先,我不确定这是奏鸣曲问题还是 Symfony2 问题,这是我第一次使用 Sf2 表单来编辑关系。 这是问题所在: 我有两个类(class),我们称它们为旧的最爱: Car 和 Wheel 。 Ca
我对 SonataAdminBundle 有疑问,我找不到如何更改左侧面板中的“奏鸣曲项目”链接。 我有“我的标题”+一张图片,下面是“奏鸣曲项目”。 我怎样才能改变这个? 最佳答案 为了自定义 So
我正在使用 SonataAdmin(和 Symfony2)来管理我的实体。我在一个步骤和多个任务之间有一个一对多的关系。由于一个步骤可以包含多个任务,因此当我创建一个步骤时,我希望能够创建多个任务并且
我有一个 Fonctionnaire 实体类,其中包含 ManyToOne 属性以对实体类进行评分。当我想将 idGrade 字段添加到 FonctionnaireAdmin 的 ConfigureL
我是一名优秀的程序员,十分优秀!