I have a form where a user can upload a picture once the picture is upload it's also persist as true. I would like only the latest picture upload to be set as default (true) and the other picture of that particular user_id are set to true to be persist as false. I don't want a user to have many pictures as default.
This is what I have done:
public function avatarUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$entity = new Avatar();
$form = $this->createForm( new AvatarType(),$entity);
if ($this->get('request')->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entity->setCreatedAt(new \DateTime());
$entity->setDefaultPicture(true);
$entity->setUser($this->container->get('security.token_storage')->getToken()->getUser());
$em->persist($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('avatarUser'));
}
return $this->render('ApplicationSonataUserBundle:Profile:avatar.html.twig', array('user' => $user,'entity' => $entity,
'form' => $form->createView()));
}
.
public function avatarUserAllAction()
{
$em = $this->getDoctrine()->getManager();
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$entity = $em->getRepository('ApplicationSonataUserBundle:Avatar')->byAvatar($user);
return $this->render('ApplicationSonataUserBundle:Profile:avatar_all_image.html.twig', array('user' => $user,'entity' => $entity));
}
Routing.yml
avatarUser:
pattern: /profile/picture
defaults: { _controller: FLYBookingsBundle:Post:avatarUser }
avatarUserAll:
pattern: /profile/picture
defaults: { _controller: FLYBookingsBundle:Post:avatarUserAll }
UserRepository
public function byAvatar($user)
{
$qb = $this->createQueryBuilder('u')
->select('u')
->where('u.user = :user')
->orderBy('u.createdAt', 'DESC')
->setParameter('user', $user);
return $qb->getQuery()
->getResult();
}
Avatar.php
class Avatar
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var User
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="avatar")
*/
protected $user;
/**
* #var boolean
* #ORM\Column(name="defaultPicture", type="boolean")
*/
protected $defaultPicture;
public function __construct()
{
$this->defaultPicture = false;
$this->createAt = new \DateTime();
}
/**
* #var \DateTime
* #ORM\Column(name="createdAt", type="datetime", nullable=true)
*/
protected $createdAt;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="user_image", fileNameProperty="imageName")
*
* #var File
*/
private $imageFile;
/**
* #ORM\Column(type="string", length=255)
*
* #var string
*/
private $imageName;
/**
* #ORM\Column(type="datetime")
*
* #var \DateTime
*/
private $updatedAt;
/**
* 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 $image
*
* #return Avatar
*/
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 $this;
}
/**
* #return File|null
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* #param string $imageName
*
* #return Avatar
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
* #return string|null
*/
public function getImageName()
{
return $this->imageName;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set defaultPicture
*
* #param boolean $defaultPicture
* #return Avatar
*/
public function setDefaultPicture($defaultPicture)
{
$this->defaultPicture = $defaultPicture;
return $this;
}
/**
* Get defaultPicture
*
* #return boolean
*/
public function getDefaultPicture()
{
return $this->defaultPicture;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Avatar
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Avatar
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set user
*
* #param \Application\Sonata\UserBundle\Entity\User $user
* #return Avatar
*/
public function setUser(\Application\Sonata\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \Application\Sonata\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
Set defaultPicture to false for all user avatars before creating new one.
if ($form->isSubmitted() && $form->isValid()) {
foreach ($this->container->get('security.token_storage')->getToken()->getUser()->getAvatar() as $avatar) {
$avatar->setDefaultPicture(false);
}
...
}
Related
I have some issues with the update of an entity with a file field on it. I don't know what it can be, because I did that a lot of time, but today it won't work.
So this is the entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Consigli
*
* #ORM\Table()
* #ORM\HasLifecycleCallbacks
* #ORM\Entity(repositoryClass="AppBundle\Entity\Repository\ConsigliRepository")
*/
class Consigli
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titolo", type="string", length=255)
*/
private $titolo;
/**
* #var string
*
* #ORM\Column(name="testo", type="text")
*/
private $testo;
/**
* #var string $image
* #Assert\File( maxSize = "60000000", mimeTypesMessage = "Perfavore inserisci un'immagine valida!")
* #ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;
/**
* #ORM\ManyToOne(targetEntity="Categoria", inversedBy="categoria")
* #ORM\JoinColumn(name="categoria_id", referencedColumnName="id")
*/
protected $categoria;
/**
* #var date
*
* #ORM\Column(name="data", type="date")
*/
public $data;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titolo
*
* #param string $titolo
*
* #return Consigli
*/
public function setTitolo($titolo)
{
$this->titolo = $titolo;
return $this;
}
/**
* Get titolo
*
* #return string
*/
public function getTitolo()
{
return $this->titolo;
}
/**
* Set testo
*
* #param string $testo
*
* #return Consigli
*/
public function setTesto($testo)
{
$this->testo = $testo;
return $this;
}
/**
* Get testo
*
* #return string
*/
public function getTesto()
{
return $this->testo;
}
/**
* Set image
*
* #param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
public function getFullImagePath() {
return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}
protected function getTmpUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../web/immaginiConsigli/';
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
return null;
}
$this->setImage($this->image->getClientOriginalName());
}
/**
* #ORM\PostPersist()
*/
public function moveImage()
{
if (null === $this->image)
{
return;
}
if (!is_dir($this->getUploadRootDir()))
{
mkdir($this->getUploadRootDir());
}
copy($this->getTmpUploadRootDir() . $this->image, $this->getFullImagePath());
unlink($this->getTmpUploadRootDir() . $this->image);
}
/**
* Set data
*
* #param \DateTime $data
*
* #return Consigli
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* #return \DateTime
*/
public function getData()
{
return $this->data;
}
/**
* Set categoria
*
* #param \AppBundle\Entity\Categoria $categoria
*
* #return Consigli
*/
public function setCategoria(\AppBundle\Entity\Categoria $categoria = null)
{
$this->categoria = $categoria;
return $this;
}
/**
* Get categoria
*
* #return \AppBundle\Entity\Categoria
*/
public function getCategoria()
{
return $this->categoria;
}
}
The file is stored in a folder, that is as you can see "immaginiConsigli", and in the table of the database I have the "image" field, that stores the name of the image.
The update action in the controller is:
public function modificaconsiglioAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$consiglio = $em->getRepository('AppBundle:Consigli')->find($id);
$form = $this->createForm(new ConsiglioType($consiglio), $consiglio);
$form->handleRequest($request);
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
try
{
$em->persist($consiglio);
$em->flush();
return $this->redirect($this->generateUrl('successconsigliomodificato'));
} catch (\Exception $e)
{
$form->addError(new FormError('errore nel database: ' . $e->getMessage()));
}
if ($form->isValid())
{
$var = $consiglio;
$em->persit($var);
$em->flush();
return $this->redirect($this->generateUrl('successconsigliomodificato'));
} else
{
}
}
return $this->render('adminarea/modificaconsiglio.html.twig', array(
'consiglio' => $consiglio,
'form' => $form->createView()));
}
So what happens is: I want to update an already existing record, that of course has already the file in it. The update seems to work, but when I render the new and updated record, the image doesn't show, and in the database I always get something like this:
/tmp/php1QyeYr
That is not the name of the image.
And also, the new file/image, it isn't inserted in the folder.
So, anyone knows what I'm doing wrong?
after looking again, I think I see the problem.
Your query will return an array of matched entities, not the match itself. You need to call findOneBy instead.
public function modificaconsiglioAction( Request $request, $id ) {
$em = $this->getDoctrine()
->getManager();
$consiglio = $em->getRepository( 'AppBundle:Consigli' )
->findOneBy( [ 'id' => $id ] );
if ( null !== $consiglio ) {
$form = $this->createForm( new ConsiglioType( $consiglio ), $consiglio );
$form->handleRequest( $request );
if ( $form->isSubmitted() && $form->isValid() ) {
$em->persist( $consiglio );
$em->flush();
return $this->redirect( $this->generateUrl( 'successconsigliomodificato' ) );
}
} else {
// do something about letting the user know the id matched nothing
}
return $this->render( 'adminarea/modificaconsiglio.html.twig', [
'consiglio' => $consiglio,
'form' => $form->createView(),
] );
}
Its also worth noting that you can get away without making your own query to the database. If you change your method signature to:
public function modificaconsiglioAction( Request $request, Consigli $consigli ) {
Symfony will automatically query the database for you based on the $id sent to the controller in the route.
I need to merge table 'fos_user' and 'institution'.And I need to display registration form from both entities.
I have a problem with FOSUserBundle.
I created new properties in User class
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
protected $workPhone;
protected $adminPhone;
protected $name;
protected $adress;
public function __construct()
{
parent::__construct();
}
public function setAdress($adress)
{
$this->adress = $adress;
}
public function setName($name)
{
$this->name = $name;
}
public function setWorkPhone($workPhone)
{
$this->workPhone = $workPhone;
}
public function setAdminPhone($adminPhone)
{
$this->adminPhone = $adminPhone;
}
public function getName()
{
return $this->name;
}
public function getAdress()
{
return $this->adress;
}
public function getWorkPhone()
{
return $this->workPhone;
}
public function getAdminPhone()
{
return $this->adminPhone;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
And I have entity Institution, which I want merge.
/**
* #var integer
*
* #ORM\Column(name="id_institution", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idInstitution;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* #var integer
*
* #ORM\Column(name="work_phone", type="integer", nullable=false)
*/
private $workPhone;
/**
* #var integer
*
* #ORM\Column(name="admin_phone", type="integer", nullable=true)
*/
private $adminPhone;
/**
* #var string
*
* #ORM\Column(name="adress", type="string", length=255, nullable=false)
*/
private $adress;
/**
* #var \AppBundle\Entity\FosUser
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\FosUser")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_fos_user", referencedColumnName="id")
* })
*/
private $idFosUser;
/**
* Get idInstitution
*
* #return integer
*/
public function getIdInstitution()
{
return $this->idInstitution;
}
/**
* Set name
*
* #param string $name
* #return Institution
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set workPhone
*
* #param integer $workPhone
* #return Institution
*/
public function setWorkPhone($workPhone)
{
$this->workPhone = $workPhone;
return $this;
}
/**
* Get workPhone
*
* #return integer
*/
public function getWorkPhone()
{
return $this->workPhone;
}
/**
* Set adminPhone
*
* #param integer $adminPhone
* #return Institution
*/
public function setAdminPhone($adminPhone)
{
$this->adminPhone = $adminPhone;
return $this;
}
/**
* Get adminPhone
*
* #return integer
*/
public function getAdminPhone()
{
return $this->adminPhone;
}
/**
* Set adress
*
* #param string $adress
* #return Institution
*/
public function setAdress($adress)
{
$this->adress = $adress;
return $this;
}
/**
* Get adress
*
* #return string
*/
public function getAdress()
{
return $this->adress;
}
/**
* Set idFosUser
*
* #param \AppBundle\Entity\FosUser $idFosUser
* #return Institution
*/
public function setIdFosUser($idFosUser = null)
{
$this->idFosUser = $idFosUser;
return $this;
}
/**
* Get idFosUser
*
* #return \AppBundle\Entity\FosUser
*/
public function getIdFosUser()
{
return $this->idFosUser;
}
This is pert of InstitutionManager where I want save Entity, and this is like service now:
public function createNewEntity($user)
{
$entity = new Institution();
$entity->setName($user->getName());
$entity->setAdminPhone($user->getAdminPhone());
$entity->setWorkPhone($user->getWorkPhone());
$entity->setAdress($user->getAdress());
$entity->setidFosUser($user->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
And hear override RegistrationController:
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$institutionManager = $this->container->get('institution_manager');
$institution = $institutionManager->createNewEntity($user);
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
services.yml:
services:
institution_manager:
class: AppBundle\Lib\Manager\InstitutionManager
arguments: [ #doctrine.orm.default_entity_manager ]
Your InstitutationController is not being properly initialized. There is a setContainer method which the router calls to, well, set the container. getDoctrine in turn needs the container, hence the null object error.
A simple hack would be to call the setContainer method yourself:
$entity = new InstitutionController();
$entity->setContainer($this->container);
$entity->createNewEntity($user);
But it's a hack you should do some redesigning. What you are calling a controller is not a controller at all. It's a service, maybe a sort of a factory or manager. Sort of like the FOS UserManager.
So read up on how to define a service: http://symfony.com/doc/current/book/service_container.html
It takes a bit of research but once you understand the process then services will become second nature. You will inject the doctrine entity manager directly into your service.
class InstitutionManager
{
protected $entityManager;
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function createNewEntity($user)
{
$entity = new Institution();
$entity->setName($user->getName());
$entity->setAdminPhone($user->getAdminPhone());
$entity->setWorkPhone($user->getWorkPhone());
$entity->setAdress($user->getAdress());
$entity->setidFosUser($user->getId());
$this->entityManager->persist($entity);
$this->entityManager->flush();
return $entity;
}
I will leave services.yml up to you. The entity manager service id is doctrine.orm.default_entity_manager
Your controller would then look like:
$institutionManager = $this->get('institution_manager');
$institution = $institutionManager->create($user);
You will also want to rethink how you are relating the user object. The userId stuff is a no no. You will want to make a one-to-one relation between $institution and $user. But that is really a different topic.
Enjoy.
Hoping that my explanation is clear(!), i will do my best:
I am working with the Symfony framework and untill now i got it all worked out. So whats my problem?
I use a form collection (ProjectType and DocumentType):
One project can have many documents.
To get the forms i used the generate:crud command and then adjusted the entities, types, etc. like on this page: http://symfony.com/doc/current/cookbook/form/form_collections.html
This all went succesfull: I can create new projects and in the same form i can add many documents. When the submit button is pressed the data gets persisted in the MySQL database.
In my doctrines i created a foreign key in the document entity, called: project_id. The association of these are correct because when i add the id to the form the dropdown appears with existing projects.
BUT I would like that the form also persist the foreign key in my documents table (which is off course the new created project PK). So that when i creat a new project with documents, the foreign key of the documents is the PK from the new project.
Edit: When i add the foreign key manually in the database and then delete the project the documents with the foreign key alse gets deleted (just to point out that the association is correct..!)
Please help me out, thank you!
-----------------ProjectController.php:
/**
* Displays a form to create a new Project entity.
*
* #Route("/new", name="project_new")
* #Method("GET")
* #Template()
*/
public function newAction() {
$entity = new Project();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Creates a form to create a Project entity.
*
* #param Project $entity The entity
*
* #return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Project $entity) {
$form = $this->createForm(new ProjectType(), $entity, array(
'action' => $this->generateUrl('project_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create project'));
return $form;
}
/**
* Creates a new Project entity.
*
* #Route("/", name="project_create")
* #Method("POST")
* #Template("AcmeDemoBundle:Project:new.html.twig")
*/
public function createAction(Request $request) {
$entity = new Project();
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
$entity->setDateCreated(new \DateTime());
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('project_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
-----------------ProjectType.php:
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('date_executed')
->add('imageprojects', 'collection', array(
'type' => new DocumentType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Project',
'cascade_validation' => false,
));
}
/**
* #return string
*/
public function getName() {
return 'project';
}
---------------Project.php (Entity):
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*
* #ORM\Column(type="string")
*/
protected $name;
/**
*
* #ORM\Column(type="date")
*/
protected $date_executed;
/**
*
* #ORM\Column(type="date")
*/
protected $date_created;
/**
* #ORM\OneToMany(targetEntity="Document", mappedBy="project_id", cascade={"persist", "remove"})
*/
protected $imageprojects;
public function __construct() {
$this->imageprojects = new ArrayCollection();
}
function __toString() {
return $this->getName();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Project
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set date_executed
*
* #param \DateTime $dateExecuted
* #return Project
*/
public function setDateExecuted($dateExecuted)
{
$this->date_executed = $dateExecuted;
return $this;
}
/**
* Get date_executed
*
* #return \DateTime
*/
public function getDateExecuted()
{
return $this->date_executed;
}
/**
* Set date_created
*
* #param \DateTime $dateCreated
* #return Project
*/
public function setDateCreated($dateCreated)
{
$this->date_created = $dateCreated;
return $this;
}
/**
* Get date_created
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->date_created;
}
/**
* Add projectimages
*
* #param \Acme\DemoBundle\Entity\Document $projectimages
* #return Project
*/
public function addImageproject(Document $projectimages)
{
//$this->imageprojects[] = $imageprojects;
$projectimages->addProjectimage($this);
$this->imageprojects->add($projectimages);
return $this;
}
/**
* Remove projectimages
*
* #param \Acme\DemoBundle\Entity\Document $projectimages
*/
public function removeImageproject(Document $projectimages)
{
$this->imageprojects->removeElement($projectimages);
}
/**
* Get imageprojects
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getImageprojects()
{
return $this->imageprojects;
}
------------------Document.php (Entity)
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #ORM\ManyToOne(targetEntity="Project", inversedBy="imageprojects")
* #ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $project_id;
/**
* #ORM\Column(type="string", length=255)
* #Assert\NotBlank
*/
public $name;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
public function getAbsolutePath() {
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path;
}
public function getWebPath() {
return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'imgupload';
}
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
$this->path = 'initial';
}
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile() {
return $this->file;
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload() {
if (null !== $this->getFile()) {
$this->path = $this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload() {
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFile()->move(
$this->getUploadRootDir(), $this->id . '.' . $this->getFile()->guessExtension()
);
$this->setFile(null);
}
/**
* #ORM\PreRemove()
*/
public function storeFilenameForRemove() {
$this->temp = $this->getAbsolutePath();
}
/**
* #ORM\PostRemove()
*/
public function removeUpload() {
if (isset($this->temp)) {
unlink($this->temp);
}
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Document
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set path
*
* #param string $path
* #return Document
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath() {
return $this->path;
}
/**
* Add projectimages
*
* #param \Acme\DemoBundle\Entity\Project $projectimages
* #return Document
*/
public function addProjectimage(Project $projectimages) {
$this->projectimages[] = $projectimages;
/*
if (!$this->projectimages->contains($projectimages)) {
$this->projectimages->add($projectimages);
}
*/
return $this;
}
/**
* Remove projectimages
*
* #param \Acme\DemoBundle\Entity\Project $projectimages
*/
public function removeProjectimage(Project $projectimages) {
$this->projectimages->removeElement($projectimages);
}
/**
* Get projectimages
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getProjectimages() {
return $this->projectimages;
}
/**
* Set project_id
*
* #param \Acme\DemoBundle\Entity\Project $projectId
* #return Document
*/
public function setProjectId(\Acme\DemoBundle\Entity\Project $projectId = null) {
$this->project_id = $projectId;
return $this;
}
/**
* Get project_id
*
* #return \Acme\DemoBundle\Entity\Project
*/
public function getProjectId() {
return $this->project_id;
}
OK, it was a matter of 'bad reading'...! The solution was already posted for this question: [Persistence with embedded forms
What I'm trying to do is to get an Post(Post Entity) form where I can select a featured picture from another entity(File Entity).
At the end I would like to display only the files of type "featured Image" but even if I remove my query_builder I've got a exception that says:
Class "Site\Backend\Adminbundle\Entity\File" seems not to be a managed Doctrine entity. Did you forget to map it?
Here is my PostType form
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title')
->add('thumb', 'entity', array(
'class' => 'Site\Backend\Adminbundle\Entity\File',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f')
//->where('f.state = :state')
//->setParameter('state', $prms['state'])
->orderBy('f.dateUpdated', 'DESC');
}
))
}
In the other hand I have my two entities:
Here is my File entity
<?php
namespace Site\Backend\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Site\Backend\AdminBundle\Entity\File
*
* #ORM\Table()
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class File {
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
/**
* #ORM\Column(type="string", length=255)
*/
public $path;
/**
* #ORM\ManyToOne(targetEntity="TypeFile", inversedBy="files")
*/
private $type;
/**
* #var string
*
* #ORM\OneToMany(targetEntity="Site\Backend\BlogBundle\Entity\Post", mappedBy="thumb")
*/
private $posts;
public function __construct() {
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->type = new ArrayCollection();
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload() {
$this->setDateUpdated(new \DateTime());
if (null !== $this->file) {
// do whatever you want to generate a unique name
$this->path = sha1(uniqid(mt_rand(), true)) . '.' . $this->file->guessExtension();
} else {
//throwException($e);
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload() {
if (null === $this->file) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* #ORM\PostRemove()
*/
public function removeUpload() {
if ($file = $this->getAbsolutePath()) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function getAbsolutePath() {
return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path;
}
public function getWebPath() {
return null === $this->path ? null : './' . $this->getUploadDir() . '/' . $this->path;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../../web/' . $this->getUploadDir();
}
protected function getUploadDir() {
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/' . $this->type;
}
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return File
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName() {
return $this->name;
}
/**
* Set path
*
* #param string $path
* #return File
*/
public function setPath($path) {
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath() {
return $this->path;
}
public function __toString() {
return $this->name;
}
/**
* Add type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
* #return File
*/
public function addType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type[] = $type;
return $this;
}
/**
* Remove type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
*/
public function removeType(\Site\Backend\AdminBundle\Entity\TypeFile $type) {
$this->type->removeElement($type);
}
/**
* Get type
*
* #return Doctrine\Common\Collections\Collection
*/
public function getType() {
return $this->type;
}
/**
* Set type
*
* #param Site\Backend\AdminBundle\Entity\TypeFile $type
* #return File
*/
public function setType(\Site\Backend\AdminBundle\Entity\TypeFile $type = null) {
$this->type = $type;
return $this;
}
public function getFile() {
return $this->file;
}
public function setFile($file) {
$this->file = $file;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return File
*/
public function setDateCreated($dateCreated) {
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated() {
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* #param \DateTime $dateUpdated
* #return File
*/
public function setDateUpdated($dateUpdated) {
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* #return \DateTime
*/
public function getDateUpdated() {
return $this->dateUpdated;
}
/**
* Add posts
*
* #param \Site\Backend\BlogBundle\Entity\Post $posts
* #return File
*/
public function addPost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* #param \Site\Backend\BlogBundle\Entity\Post $posts
*/
public function removePost(\Site\Backend\BlogBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
And Here is my Post entity
<?php
namespace Site\Backend\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Post
*
* #ORM\Table()
* #ORM\Entity
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var \DateTime
*
* #ORM\Column(name="dateCreated", type="datetime")
*/
private $dateCreated;
/**
* #var \DateTime
*
* #ORM\Column(name="dateUpdated", type="datetime")
*/
private $dateUpdated;
/**
* #var \DateTime
*
* #ORM\Column(name="datePublished", type="datetime", nullable=true)
*/
private $datePublished;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=255)
*/
private $state;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var Author
* #ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\User", inversedBy="posts")
*/
private $author;
/**
* #var Thumb
* #ORM\ManyToOne(targetEntity="Site\Backend\AdminBundle\Entity\File", inversedBy="posts")
*/
private $thumb;
/**
* #var Comments
* #ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
private $comments;
/**
* #var Categories
* #ORM\ManyToMany(targetEntity="Category", cascade={"persist"})
*/
private $categories;
/**
* #var Tags
* #ORM\ManyToMany(targetEntity="Tag", cascade={"persist"})
*/
private $tags;
/**
* Constructeur
*/
public function __construct()
{
$this->setDateCreated(new \DateTime());
$this->setDateUpdated(new \DateTime());
$this->comments = new ArrayCollection();
$this->Categories = new ArrayCollection();
$this->tags = new ArrayCollection();
}
public function __toString(){
return $this->title;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set dateCreated
*
* #param \DateTime $dateCreated
* #return Comment
*/
public function setDateCreated($dateCreated)
{
$this->dateCreated = $dateCreated;
return $this;
}
/**
* Get dateCreated
*
* #return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateUpdated
*
* #param \DateTime $dateUpdated
* #return Comment
*/
public function setDateUpdated($dateUpdated)
{
$this->dateUpdated = $dateUpdated;
return $this;
}
/**
* Get dateUpdated
*
* #return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
/**
* Get datePublished
*
* #return \DateTime
*/
public function getDatePublished()
{
return $this->datePublished;
}
/**
* Set datePublished
*
* #param \DateTime $datePublished
* #return Comment
*/
public function setDatePublished($datePublished)
{
$this->datePublished = $datePublished;
return $this;
}
/**
* Set state
*
* #param string $state
* #return Post
*/
public function setState($state)
{
$this->state = $state;
return $this;
}
/**
* Get state
*
* #return string
*/
public function getState()
{
return $this->state;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set author
*
* #param \Site\Backend\AdminBundle\Entity\User $author
* #return Post
*/
public function setAuthor(\Site\Backend\AdminBundle\Entity\User $author = null)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return \Site\Backend\AdminBundle\Entity\User
*/
public function getAuthor()
{
return $this->author;
}
/**
* Add comments
*
* #param \Site\Backend\BlogBundle\Entity\Comment $comments
* #return Post
*/
public function addComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
/**
* Remove comments
*
* #param \Site\Backend\BlogBundle\Entity\Comment $comments
*/
public function removeComment(\Site\Backend\BlogBundle\Entity\Comment $comments)
{
$this->comments->removeElement($comments);
}
/**
* Get comments
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategory(\Site\Backend\BlogBundle\Entity\Category $category)
{
$this->categories->removeElement($category);
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategoriesNew(\Site\Backend\BlogBundle\Entity\Category $category)
{
}
/**
* Get Categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Get Categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategoriesNew()
{
//return $this->categories;
}
/**
* Add tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
* #return Post
*/
public function addTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Add tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
* #return Post
*/
public function addTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* #param \Site\Backend\BlogBundle\Entity\Tag $tags
*/
public function removeTag(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Remove Tags
*
* #param \Site\Backend\BlogBundle\Entity\tag $tags
*/
public function removeTagsNew(\Site\Backend\BlogBundle\Entity\Tag $tags)
{
//$this->tags->removeElement($tags);
}
/**
* Get Tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
/**
* Get Tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTagsNew()
{
//return $this->Tags;
}
/**
* Add Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
* #return Post
*/
public function addCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove Categories
*
* #param \Site\Backend\BlogBundle\Entity\Category $categories
*/
public function removeCategorie(\Site\Backend\BlogBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* #ORM\preUpdate
*/
public function setUpdateValue(){
$this->setDateUpdated(new \DateTime());
}
/**
* Set thumb
*
* #param \Site\Backend\AdminBundle\Entity\File $thumb
* #return Post
*/
public function setThumb(\Site\Backend\AdminBundle\Entity\File $thumb = null)
{
$this->thumb = $thumb;
return $this;
}
/**
* Get thumb
*
* #return \Site\Backend\AdminBundle\Entity\File
*/
public function getThumb()
{
return $this->thumb;
}
}
Thanks in advance
I hope I was clear, if It is not the case please tell me.
Try changing:
Site\Backend\Adminbundle\Entity\File
to
Site\Backend\AdminBundle\Entity\File
An other way to resolve the case :
->add('thumb', null, array(
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f')
->select('f')
->leftJoin('f.type', 't')
->where('t.name = :type')
->setParameter('type', 'Featured Images')
->orderBy('f.dateUpdated', 'DESC');
}
))
In case you are working with multiple connections, you might have forgotten to map your bundle. Make sure it's the case in app/config/config.yml:
orm:
entity_managers:
default:
mappings:
AppBundle: ~
SiteBackendAdminBundle: ~
Or if you want to split in several entity managers:
orm:
entity_managers:
default:
mappings:
AppBundle: ~
admin:
mappings:
SiteBackendAdminBundle: ~
Documentation Symfony/Doctrine: Multiple Entity Managers.
trying to setup a file upload form attached to a Doctrine entity, according to this cookbook recipe:
http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html
Here's my entity/model class:
<?php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Invoice
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="TechPeople\InvoiceBundle\Entity\InvoiceRepository")
* #ORM\HasLifecycleCallbacks
*/
class Invoice
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="User", inversedBy="user_invoices")
* #ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
*/
private $vendor;
/**
* #var string
*
* #ORM\Column(name="month", type="string", length=255)
*/
private $month;
/**
* #var integer
*
* #ORM\Column(name="year", type="smallint")
*/
private $year;
/**
* #var boolean
*
* #ORM\Column(name="expenses", type="boolean")
*/
private $expenses;
/**
* #var \DateTime
*
* #ORM\Column(name="due", type="date")
*/
private $due;
/**
* #var \DateTime
*
* #ORM\Column(name="paid", type="date")
*/
private $paid;
/**
* #var \DateTime
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var float
*
* #ORM\Column(name="expense_amount", type="decimal")
*/
private $expense_amount;
/**
* #var float
*
* #ORM\Column(name="total_amount", type="decimal")
*/
private $total_amount;
/**
* #var UploadedFile
*
* #Assert\File(maxSize="6000000")
*/
public $attachment;
/**
* #var string
*
* #ORM\Column(name="attachment_path", type="string", length=255)
*/
private $attachment_path;
/**
* #ORM\OneToMany(targetEntity="InvoiceItem", mappedBy="invoice")
*/
protected $invoice_items;
/**
* Constructor for Invoice Entity class
*/
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set month
*
* #param string $month
* #return Invoice
*/
public function setMonth($month)
{
$this->month = $month;
return $this;
}
/**
* Get month
*
* #return string
*/
public function getMonth()
{
return $this->month;
}
/**
* Set year
*
* #param integer $year
* #return Invoice
*/
public function setYear($year)
{
$this->year = $year;
return $this;
}
/**
* Get year
*
* #return integer
*/
public function getYear()
{
return $this->year;
}
/**
* Set expenses
*
* #param boolean $expenses
* #return Invoice
*/
public function setExpenses($expenses)
{
$this->expenses = $expenses;
return $this;
}
/**
* Get expenses
*
* #return boolean
*/
public function getExpenses()
{
return $this->expenses;
}
/**
* Set due
*
* #param \DateTime $due
* #return Invoice
*/
public function setDue($due)
{
$this->due = $due;
return $this;
}
/**
* Get due
*
* #return \DateTime
*/
public function getDue()
{
return $this->due;
}
/**
* Set paid
*
* #param \DateTime $paid
* #return Invoice
*/
public function setPaid($paid)
{
$this->paid = $paid;
return $this;
}
/**
* Get paid
*
* #return \DateTime
*/
public function getPaid()
{
return $this->paid;
}
/**
* Set created
*
* #param \DateTime $created
* #return Invoice
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* #return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set expense_amount
*
* #param float $expenseAmount
* #return Invoice
*/
public function setExpenseAmount($expenseAmount)
{
$this->expense_amount = $expenseAmount;
return $this;
}
/**
* Get expense_amount
*
* #return float
*/
public function getExpenseAmount()
{
return $this->expense_amount;
}
/**
* Set total_amount
*
* #param float $totalAmount
* #return Invoice
*/
public function setTotalAmount($totalAmount)
{
$this->total_amount = $totalAmount;
return $this;
}
/**
* Get total_amount
*
* #return float
*/
public function getTotalAmount()
{
return $this->total_amount;
}
/**
* Set attachment
*
* #param string $attachment
* #return Invoice
*/
public function setAttachment($attachment)
{
$this->attachment = $attachment;
return $this;
}
/**
* Get attachment
*
* #return string
*/
public function getAttachment()
{
return $this->attachment;
}
/**
* Set vendor
*
* #param \TechPeople\InvoiceBundle\Entity\User $vendor
* #return Invoice
*/
public function setVendor(\TechPeople\InvoiceBundle\Entity\User $vendor = null)
{
$this->vendor = $vendor;
return $this;
}
/**
* Get vendor
*
* #return \TechPeople\InvoiceBundle\Entity\User
*/
public function getVendor()
{
return $this->vendor;
}
/**
* Add invoice_items
*
* #param \TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems
* #return Invoice
*/
public function addInvoiceItem(\TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems)
{
$this->invoice_items[] = $invoiceItems;
return $this;
}
/**
* Remove invoice_items
*
* #param \TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems
*/
public function removeInvoiceItem(\TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems)
{
$this->invoice_items->removeElement($invoiceItems);
}
/**
* Get invoice_items
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getInvoiceItems()
{
return $this->invoice_items;
}
/**
* Get path for attachment
*
* #param string $type Can return web path, or absolute path, web is default
* #return null|string
*/
public function getAttachmentPath($type='web')
{
if($type == 'absolute') {
return null === $this->attachment_path
? null
: $this->getUploadRootDir().'/'.$this->attachment_path;
} else {
return null === $this->attachment_path
? null
: $this->getUploadDir().'/'.$this->attachment_path;
}
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/invoice/attachments';
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->attachment) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->attachment = $filename.'.'.$this->attachment->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->attachment) {
return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->attachment->move($this->getUploadRootDir(), $this->path);
unset($this->attachment);
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAttachmentPath('absolute')) {
unlink($file);
}
}
}
I get the following error when I try to submit an edit form (the edit form was generated by doctrine:generate:crud):
Fatal error: Call to a member function move() on a non-object
Might be the same as:
Symfony forms. File upload
But that question isn't answered. I hope it doesn't violate etiquette to post a second question.
I'm pretty new to Symfony2, just trying to learn my way around, and this has me stumped. I can post any other info you need, just let me know what.
Here's the form class:
<?php
namespace TechPeople\InvoiceBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class InvoiceType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('month')
->add('year')
->add('expenses')
->add('due')
->add('paid')
->add('created')
->add('expense_amount')
->add('total_amount')
->add('attachment')
->add('vendor')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'TechPeople\InvoiceBundle\Entity\Invoice'
));
}
public function getName()
{
return 'techpeople_invoicebundle_invoicetype';
}
}
And here's the controller:
<?php
namespace TechPeople\InvoiceBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use TechPeople\InvoiceBundle\Entity\Invoice;
use TechPeople\InvoiceBundle\Form\InvoiceType;
/**
* Invoice controller.
*
*/
class InvoiceController extends Controller
{
/**
* Lists all Invoice entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('TechPeopleInvoiceBundle:Invoice')->findAll();
return $this->render('TechPeopleInvoiceBundle:Invoice:index.html.twig', array(
'entities' => $entities,
));
}
/**
* Finds and displays a Invoice entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('TechPeopleInvoiceBundle:Invoice')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render('TechPeopleInvoiceBundle:Invoice:show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(), ));
}
/**
* Displays a form to create a new Invoice entity.
*
*/
public function newAction()
{
$entity = new Invoice();
$form = $this->createForm(new InvoiceType(), $entity);
return $this->render('TechPeopleInvoiceBundle:Invoice:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a new Invoice entity.
*
*/
public function createAction(Request $request)
{
$entity = new Invoice();
$form = $this->createForm(new InvoiceType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('invoice_show', array('id' => $entity->getId())));
}
return $this->render('TechPeopleInvoiceBundle:Invoice:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Displays a form to edit an existing Invoice entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('TechPeopleInvoiceBundle:Invoice')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$editForm = $this->createForm(new InvoiceType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render('TechPeopleInvoiceBundle:Invoice:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Edits an existing Invoice entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('TechPeopleInvoiceBundle:Invoice')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new InvoiceType(), $entity);
$editForm->bind($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('invoice_edit', array('id' => $id)));
}
return $this->render('TechPeopleInvoiceBundle:Invoice:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a Invoice entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('TechPeopleInvoiceBundle:Invoice')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('invoice'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
It's all pretty basic stuff, straight from the docs, because, as I said, I'm just learning Symfony. Any help much appreciated.
$this->attachment must be an UploadedFile object.
But in your method preUpload() you do a mistake and override it for a string:
$this->attachment = $filename.'.'.$this->attachment->guessExtension();
Then, upload() method is called and you check if $this->attachment if not null, that's true because it's a string:
if (null === $this->attachment) {
return;
}
And executing ->move() on a string display an error ;)
So to answer, in preUpload() method replace this line:
$this->attachment= $filename.'.'.$this->attachment->guessExtension();
For:
$this->path = $filename.'.'.$this->attachment->guessExtension();