我尝试在我的用户实体中添加个人资料图片,但我失败了,我使用symfony 3和fosuserbundle,为了完成这项工作,我使用了一个列表器,这里是我的整个代码:
我UserEntity
的代码:
namespace Forum\ForumBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Util\SecureRandom;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
* @Assert\Image(
*
* )
*/
private $brochure;
public function getBrochure()
{
return $this->brochure;
}
public function setBrochure(File $file = null)
{
$this->brochure = $file;
return $this;
}
public function __construct()
{
parent::__construct();
$this->test = false;
// $this->uploadProfilePicture();
// your own logic
}
}
我的听众:
namespace Forum\ForumBundle\EventListener;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Forum\ForumBundle\Entity\User;
use Forum\ForumBundle\FileUploader;
class BrochureUploadListener
{
private $uploader;
public function __construct(FileUploader $uploader)
{
$this->uploader = $uploader;
}
public function prePersist(LifecycleEventArgs $args)
{die("good");
$entity = $args->getEntity();
$this->uploadFile($entity);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
$this->uploadFile($entity);
}
private function uploadFile($entity)
{
// upload only works for Product entities
if (!$entity instanceof Product) {
return;
}
$file = $entity->getBrochure();
// only upload new files
if (!$file instanceof UploadedFile) {
return;
}
$fileName = $this->uploader->upload($file);
$entity->setBrochure($fileName);
}
}
监听器使用Uploader文件:
namespace Forum\ForumBundle;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->targetDir, $fileName);
return $fileName;
}
}
我也配置我的服务
app.brochure_uploader:
class: Forum\ForumBundle\FileUploader
arguments: ['%brochures_directory%']
app.doctrine_brochure_listener:
class: Forum\ForumBundle\EventListener\BrochureUploadListener
arguments: ['@app.brochure_uploader']
tags:
- { name: doctrine.event_listener, event: prePersist }
- { name: doctrine.event_listener, event: preUpdate }
我声明参数:config.yml中的brochure-directory
parameters:
locale: fr
brochures_directory: 'web/uploads/brochures'
问题是当我更新我的实体时,兄弟
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Controller;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* Controller managing the user profile
*
* @author Christophe Coevoet <stof@notk.org>
*/
class ProfileController extends Controller
{
/**
* Show the user
*/
public function showAction()
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('FOSUserBundle:Profile:show.html.twig', array(
'user' => $user
));
}
/**
* Edit the user
*/
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
'form' => $form->createView()
));
}
}
答案 0 :(得分:0)
您是否尝试过将config.yml更改为:
parameters:
locale: fr
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'
也许这就是问题所在?先试试吧。
编辑#2。 其次,您可以编辑app / config / parameters.yml并添加以下参数:
brochures_directory: uploads/brochures
还要确保每次都清除缓存:
php bin/console cache:clear --env=prod
看看是否有效。
编辑#3。 我注意到你的配置与默认配置完全不同,我试图复制设置,但是这样做有很多工作要做。
我发现使用和使用的内容有所不同,这可能也会影响您所看到的问题:
parameters:
locale: fr
brochures_directory: uploads/brochures
换句话说,删除引号并删除网页&#39;字首。还要确保您已经创建了这些文件夹,包括&#34;上传&#34;和&#34;小册子&#34;在它之下。
看看是否有效。
就像我说我试图复制一样,然后我得到了#34;请将产品手册上传为PDF文件。&#34;在&#34;注册&#34;页面,但我尝试复制你的设置仍然会有相当多的工作。
我认为这很简单,基于我收到这条消息的事实。