I have 2 models, and 2 from types. FormType "EventSchedule" is a subform of "Event". When i tried to use $this->createForm(new EventType(), $event); in my controller, I got the form but EventSchedules(that are the part of Event) have no links to related events.
/**
* Event
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Voulidance\SharedBundle\Repository\EventRepository")
*/
class Event extends BaseEntity {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
* #Assert\Length(max = "50", maxMessage="The value is too long. It should have maximum {{ limit }} characters")
* #Assert\NotBlank()
*/
private $name;
/**
*
* #var \DateTime
*
* #ORM\Column(name="start_date", type="datetime")
* #Assert\DateTime(message = "The date should be in format 'mm/dd/yyyy'.")
* #Assert\NotBlank(message = "Start date field should not be blank.")
*/
private $startDate;
/**
*
* #var \DateTime
*
* #ORM\Column(name="end_date", type="datetime")
* #Assert\DateTime(message = "The date should be in format 'mm/dd/yyyy'.")
* #Assert\NotBlank(message = "End date field should not be blank.")
*/
private $endDate;
/**
* #ORM\OneToMany(targetEntity="EventSchedule", mappedBy="event", cascade={"persist"})
* #ORM\OrderBy({"dayOfWeek" = "ASC", "startTime" = "ASC"})
*/
protected $eventSchedules;
public function __construct()
{
$this->eventSchedules = new ArrayCollection();
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getStartDate() {
return $this->startDate;
}
public function getEndDate() {
return $this->endDate;
}
public function getEventSchedules() {
return $this->eventSchedules;
}
public function setId($id) {
$this->id = $id;
}
public function setName($name) {
$this->name = $name;
}
public function setStartDate($startDate) {
$this->startDate = $startDate;
}
public function setEndDate($endDate) {
$this->endDate = $endDate;
}
public function setEventSchedules($eventSchedules) {
$this->eventSchedules = $eventSchedules;
}
public function addEventSchedule(EventSchedule $eventSchedule)
{
$this->eventSchedules->add($eventSchedule);
}
public function removeEventSchedule(EventSchedule $eventSchedule)
{
$this->eventSchedules->removeElement($eventSchedule);
}
/**
* EventSchedule
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Voulidance\SharedBundle\Repository\EventScheduleRepository")
*/
class EventSchedule extends BaseEntity{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="startTime", type="time", nullable=true)
* #Assert\Time()
* #Assert\NotBlank(message="Please, fill in all fields.")
*/
private $startTime;
/**
* #var \DateTime
*
* #ORM\Column(name="endTime", type="time", nullable=true)
* #Assert\Time()
* #Assert\NotBlank(message="Please, fill in all fields.")
*/
private $endTime;
/**
* #var integer
*
* #ORM\Column(name="dayOfWeek", type="integer")
* #Assert\NotBlank(message="Please, fill in all fields.")
*/
private $dayOfWeek;
/**
* #ORM\ManyToOne(targetEntity="Event", inversedBy="eventSchedules")
* #ORM\JoinColumn(name="event_id", referencedColumnName="id")
* #Assert\Valid
* #Assert\NotBlank(message="Please, fill in all fields.")
*/
protected $event;
//Assert\NotBlank(message="Please, fill in all fields.")
public function getId() {
return $this->id;
}
public function getStartTime() {
return $this->startTime;
}
public function getEndTime() {
return $this->endTime;
}
public function getDayOfWeek() {
return $this->dayOfWeek;
}
public function getEvent() {
return $this->event;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function setStartTime($startTime) {
$this->startTime = $startTime;
return $this;
}
public function setEndTime($endTime) {
$this->endTime = $endTime;
return $this;
}
public function setDayOfWeek($dayOfWeek) {
$this->dayOfWeek = $dayOfWeek;
return $this;
}
public function setEvent($event) {
$this->event = $event;
return $this;
}
}
class EventType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('name', null, array(
'label' => false,
'attr' => array('id'=>"page-name", 'size'=>"24", 'class'=>'auth'),
'required'=>true
))->add('startDate', 'date', array(
'widget' => 'single_text',
'label' => false,
'format' => 'MM/dd/yyyy',
'invalid_message' => "Start date format should be 'mm/dd/yyyy'.",
'attr' => array('data-date'=>"", 'placeholder'=>"mm/dd/yyyy", 'size'=>"18",'class'=>"auth date holiday-start-date hasDatepicker", 'id'=>"holiday-start-date")
))->add('endDate', 'date', array(
'widget' => 'single_text',
'label' => false,
'format' => 'MM/dd/yyyy',
'invalid_message' => "End date format should be 'mm/dd/yyyy'.",
'attr' => array('data-date'=>"", 'placeholder'=>"mm/dd/yyyy", 'size'=>"18",'class'=>"auth date holiday-start-date hasDatepicker", 'id'=>"holiday-start-date")
))->add('eventSchedules', 'collection', array(
'type' => new EventScheduleType(),
'cascade_validation' => true,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'error_bubbling'=>false
))->add('id', 'hidden');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Voulidance\SharedBundle\Entity\Event',
'cascade_validation' => false,
'validation_groups' => false
));
}
public function getName()
{
return 'event';
}
}
class EventScheduleType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('startTime', 'us_time', array(
'widget' => 'text',
'label' => 'From',
'attr' => array('maxlength' => '2', 'placeholder' => 'hh:mm'),
'invalid_message' => 'Time value is not valid.',
'error_bubbling' => false
))->add('endTime', 'us_time', array(
'widget' => 'text',
'label' => 'To',
'attr' => array('maxlength' => '2', 'placeholder' => 'hh:mm'),
'invalid_message' => 'Time value is not valid.',
'error_bubbling' => false
))->add('dayOfWeek', 'choice', array(
'choices' => array(
1 => 'Everyday',
2 => 'Sunday',
3 => 'Monday',
4 => 'Tuesday',
5 => 'Wednesday',
6 => 'Thursday',
7 => 'Friday',
8 => 'Saturday',
),
'label' => false,
'attr' => array('size'=>'15'),
'multiple' => false,
'error_bubbling' => false
))->add('id', 'hidden', array('label' => false));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Voulidance\SharedBundle\Entity\EventSchedule',
'cascade_validation' => false,
'validation_groups' => false
));
}
public function getName() {
return 'eventSchedule';
}
}
/**
* Save event
*
* #Route("/save", name="admin_event_save")
* #Template("VoulidanceAdminBundle:Event:index.html.twig")
*/
public function saveAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$eventRepository = $em->getRepository("VoulidanceSharedBundle:Event");
$event = new Event();
$form = $this->createForm(new EventType(), $event);
$form->handleRequest($request);
$schedules = $form->get('eventSchedules')->getData();
foreach($schedules as $schedule){
$schedule->setEvent($event);
}
if ($form->isValid()) {
$errors = $this->validateSchedules($schedules);
if(count($errors) > 0){
return new JsonResponse(array('subformError'=>true, 'errors'=>$errors));
}
$em->persist($event);
$em->flush();
return new JsonResponse(array('success' => true, 'events'=>$eventRepository->findAll()));
}
return new JsonResponse(array('error' => true, 'errors'=>(string)$form->getErrors(true)));
}
Although I can't test the code right now, I think I can see the problem and answer your question so that maybe you can work on it.
$schedules = $form->get('eventSchedules')->getData();
should be:
$schedules = $event-> getEventSchedules();
because you need the mapped data which $form->handleRequest($request); is filling into $event object.
Solution is to set event to eventSchedule in the addEventSchedule method of event class.
public function addEventSchedule(EventSchedule $eventSchedule)
{
$eventSchedule->addEvent($this);
$this->eventSchedules->add($eventSchedule);
}
Related
I have this error on form submit:
Expected argument of type "Bl\CoreBundle\Entity\UsersChannelBrand", "instance of Bl\CoreBundle\Entity\ChannelBrand" given.
UsersType.php
class UsersType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, array('label' => 'Email'))
->add('username', TextType::class, array('label' => 'Usuario'))
->add('name', TextType::class, array('label' => 'Nombre'))
->add('lastname', TextType::class, array('label' => 'Apellido'))
->add('roles', CollectionType::class, array('label' => 'Roles'))
->add('channelbrand_isolation', CheckboxType::class, array('label' => 'Aislado', 'required' => false))
->add('userschannelbrand', EntityType::class, array(
'class' => 'Bl\CoreBundle\Entity\ChannelBrand',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('cb')
->where('cb.isActive =:active')->setParameter('active', true)
->orderBy('cb.name', 'ASC');
},
'choice_label' => 'name',
'multiple' => true,
'label' => 'Canal',
'required' => false,
))
->add('enabled', CheckboxType::class, array('label' => 'Activo', 'required' => false))
;
}
// 'attr' => array(
// 'class' => 'datepicker',
// 'data-provide' => 'datepicker',
// 'data-date-format' => 'yyyy-mm-dd'
// )
// public function getParent()
// {
// return 'FOS\UserBundle\Form\Type\RegistrationFormType';
// }
public function getBlockPrefix()
{
return 'app_user_registration';
}
//
public function getName()
{
return $this->getBlockPrefix();
}
// public function setDefaultOptions(OptionsResolverInterface $resolver) {
// $resolver->setDefaults(array(
// 'data_class' => 'Bl\CoreBundle\Entity\Users',
// 'validation_groups' => array('edit'),
// ));
// }
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Bl\CoreBundle\Entity\Users',
'validation_groups' => array('edit')
));
}
}
Users.php
class Users extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
$this->addRole("ROLE_USER");
}
/**
* #ORM\Column(name="name", type="string", nullable=true)
*/
protected $name;
/**
* #ORM\Column(name="lastname", type="string",nullable=true)
*/
protected $lastname;
/**
* #ORM\Column(name="channelbrand_isolation", type="boolean")
*/
protected $channelbrandIsolation = false;
/**
* Ids de los channelBrands habilitados para el usuario
* #var array
*/
/**
* Devuelve todos los channelBrands habilitados para el usuario
* #ORM\OneToMany(targetEntity="UsersChannelBrand", mappedBy="users", cascade={"persist"})
*/
protected $userschannelbrand;
public function getChb(): array
{
$chb = [];
foreach ($this->getUserschannelbrand()->getIterator() as $res) {
$chb[] = $res->getChannelbrand()->getId();
}
return $chb;
}
public function hasChb(int $id): bool
{
return in_array($id, $this->chb);
}
//
// /**
// *
// * #param int $id
// * #return \self
// */
// public function addChb(int $id): self
// {
// if (!$this->hasChb($id)) {
// $this->chb[] = $id;
// }
// return $this;
// }
function getName()
{
return $this->name;
}
function getLastname()
{
return $this->lastname;
}
function setName($name)
{
$this->name = $name;
}
function setLastname($lastname)
{
$this->lastname = $lastname;
}
/**
* Add userschannelbrand.
*
* #param \Bl\CoreBundle\Entity\UsersChannelBrand $userschannelbrand
*
* #return Users
*/
public function addUserschannelbrand(\Bl\CoreBundle\Entity\UsersChannelBrand $userschannelbrand)
{
$this->userschannelbrand[] = $userschannelbrand;
return $this;
}
/**
* Remove userschannelbrand.
*
* #param \Bl\CoreBundle\Entity\UsersChannelBrand $userschannelbrand
*
* #return boolean TRUE if this collection contained the specified element, FALSE otherwise.
*/
public function removeUserschannelbrand(\Bl\CoreBundle\Entity\UsersChannelBrand $userschannelbrand)
{
return $this->userschannelbrand->removeElement($userschannelbrand);
}
/**
* Get userschannelbrand.
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUserschannelbrand()
{
return $this->userschannelbrand;
}
/**
* Set channelbrandIsolation.
*
* #param bool $channelbrandIsolation
*
* #return Users
*/
public function setChannelbrandIsolation($channelbrandIsolation)
{
$this->channelbrandIsolation = $channelbrandIsolation;
return $this;
}
/**
* Get channelbrandIsolation.
*
* #return bool
*/
public function getChannelbrandIsolation()
{
return $this->channelbrandIsolation;
}
}
I am calling UsersChannelBrand on UsersType.php because this entity have a list of active brands.. but i have this error on submit, how can i fix it?
Regards!
I have crud with 3 entities. Meal, Product and ProductsQuantity. Between Meal and ProductQuantity is relation many to many. Adding data is working fine, all data are saving to entities but problem is when in want to edit form. Then I got error:
The form's view data is expected to be an instance of class MealBundle\Entity\ProductsQuantity, but is an instance of class Doctrine\ORM\PersistentCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\ORM\PersistentCollection to an instance of MealBundle\Entity\ProductsQuantity.
I tried with data_class option to null, and setting fetch="EAGER" on the relation but it doesn't solved the problem.
Meal:
/**
* #Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #var string
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name = "";
/**
* #var ProductsQuantity[]|Collection
* #ORM\ManyToMany(targetEntity="ProductsQuantity", inversedBy="meal", cascade={"persist"}, fetch="EAGER")
* #ORM\JoinTable(name="meal_products_quantity_relations",
* joinColumns={#ORM\JoinColumn(name="meal_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="products_quantity_id", referencedColumnName="id")}
* )
*/
protected $productsQuantity;
/**
* Meal constructor.
*/
public function __construct()
{
$this->productsQuantity = new ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
* #return Meal
*/
public function setId(int $id): Meal
{
$this->id = $id;
return $this;
}
/**
* #return string
*/
public function getName(): string
{
return $this->name;
}
/**
* #param string $name
* #return Meal
*/
public function setName(string $name): Meal
{
$this->name = $name;
return $this;
}
/**
* #return Collection|ProductsQuantity[]
*/
public function getProductsQuantity()
{
return $this->productsQuantity;
}
/**
* #param Collection|ProductsQuantity[] $productsQuantity
* #return Meal
*/
public function setProductsQuantity($productsQuantity)
{
$this->productsQuantity = $productsQuantity;
return $this;
}
ProductQuantity:
/**
* #Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #var Product
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* #ORM\ManyToOne(targetEntity="Product", inversedBy="productsQuantity")
*/
protected $product;
/**
* #var integer
* #ORM\Column(name="amount", type="integer")
*/
protected $amount;
/**
* #var $meal
* #ORM\ManyToMany(targetEntity="MealBundle\Entity\Meal", mappedBy="productsQuantity")
*/
protected $meal;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
/**
* #param mixed $id
* #return ProductsQuantity
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* #return Product
*/
public function getProduct(): ?Product
{
return $this->product;
}
/**
* #param Product $product
* #return ProductsQuantity
*/
public function setProduct(Product $product): ProductsQuantity
{
$this->product = $product;
return $this;
}
/**
* #return int
*/
public function getAmount(): ?int
{
return $this->amount;
}
/**
* #param int $amount
*/
public function setAmount(int $amount): void
{
$this->amount = $amount;
}
/**
* #return mixed
*/
public function getMeal()
{
return $this->meal;
}
/**
* #param mixed $meal
* #return ProductsQuantity
*/
public function setMeal($meal)
{
$this->meal = $meal;
return $this;
}
Meal form:
class MealType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options = [])
{
parent::buildForm($builder, $options);
/** #var Meal $meal */
$meal = $options['data'];
$data = null;
if(!empty($meal)) {
$data = $meal->getProductsQuantity();
} else {
$data = new ProductsQuantity();
}
$builder
->add('name', TextType::class,[
'label' => 'Nazwa Dania',
'required' => true
])->add('productsQuantity', CollectionType::class, [
'data_class' => null,
'label' => 'Produkty',
'entry_type' => ProductsQuantityType::class,
'allow_add' => true,
'data' => ['productsQuantity' => $data],
'prototype_name' => '__product__',
'entry_options' => [
'allow_extra_fields' => true,
'label' => false
],
'prototype' => true
])->add('addProduct', ButtonType::class, [
'label' => 'Dodaj kolejny produkt',
'attr' => [
'class' => 'btn-default addProductEntry'
]
])->add('submit', SubmitType::class, [
'label' => 'Dodaj'
]);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired('productsQuantity');
$resolver->setDefaults([
'data_class' => Meal::class
]);
}
}
ProductsQuantity form:
class ProductsQuantityType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('product', EntityType::class,[
'class' => Product::class,
'label' => 'Nazwa produktu',
])
->add('amount', NumberType::class, [
'label' => 'Ilość',
'required' => true,
'attr' => [
'placeholder' => 'ilość'
]
])
->add('removeProduct', ButtonType::class, [
'label' => 'X',
'attr' => [
'class' => 'btn-danger removeProductEntry'
]
]);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ProductsQuantity::class,
]);
}
}
If I change 'data' => ['productsQuantity' => $data] to 'data' => ['productsQuantity' => new ProductsQuantity()] there is no error but have empty ProductsQuantity part of MealType form. Can anyone tell me how to fix this?
I am overriding the form type to register a user. All looks ok, but when I submit my form the new fields are not persisted in database.
I followed the documentation.
My ProfileType:
<?php
namespace Application\Sonata\UserBundle\Form\Type;
//use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
//use Sonata\UserBundle\Model\UserInterface;
use Sonata\UserBundle\Form\Type\ProfileType as BaseType;
class ProfileType extends BaseType
{
private $class;
/**
* #param string $class The User class name
*/
public function __construct($class)
{
$this->class = $class;
}
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->add('username', null, array(
'label' => 'Pseudo',
'required' => false
))
->add('firstname', null, array(
'label' => 'Prénom'
))
->add('lastname', null, array(
'label' => 'Nom'
))
->add('email', 'email', array(
'label' => 'Email'
))
->add('dateOfBirth', 'birthday', array(
'label' => 'Date d\'anniversaire',
'required' => false,
'data' => new \DateTime("01/01/1980")
))
->add('plainPassword', 'password', array(
'label' => 'Password'
))
->add('phone', null, array(
'label' => 'Téléphone',
'required' => false
))
->add('adress', null, array(
'label' => 'Adresse',
'required' => false
))
->add('zip', null, array(
'label' => 'Code postale',
'required' => false
))
->add('city', null, array(
'label' => 'Ville',
'required' => false
))
->add('newsletter', 'checkbox', array(
'label' => 'newsletter',
'required' => false
))
#hidden
->add('website', 'hidden', array(
'label' => 'website',
'required' => false
))
->add('biography', 'hidden', array(
'label' => 'biography',
'required' => false
))
->add('locale', 'hidden', array(
'label' => 'locale',
'required' => false
))
->add('timezone', 'hidden', array(
'label' => 'Timezone',
'required' => false
))
->add('gender', 'hidden', array(
'label' => 'Civilité',
'required' => false
))
;
// var_dump($builder);
}
/**
* {#inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Application\Sonata\UserBundle\Entity\User',
'intention' => 'profile',
'label' => 'Edit Profile'
));
}
// public function getParent()
// {
// return 'fos_user_registration';
// }
/**
* {#inheritdoc}
*/
public function getName()
{
return 'application_sonata_user_profile';
}
}
My user class:
<?php
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
/**
* Application\Sonata\UserBundle\Entity\User
*
* #ORM\Table(name="fos_user_user", indexes={#ORM\Index(name="search_idx", columns={"username", "email"})}))
* #ORM\Entity()
* #DoctrineAssert\UniqueEntity(fields={"username"}, message="username.already.exist" )
* #DoctrineAssert\UniqueEntity(fields={"email"}, message="email.already.exist" )
*/
class User extends BaseUser
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="zip", type="string", length=255, nullable=true)
*/
protected $zip;
/**
* #var string
*
* #ORM\Column(name="adress", type="text", nullable=true)
*/
protected $adress;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=255, nullable=true)
*/
protected $city;
/**
* #var boolean
*
* #ORM\Column(name="newsletter", type="boolean", nullable=true)
*/
private $newsletter;
/**
* Get id
*
* #return integer $id
*/
public function getId()
{
return $this->id;
}
/**
* Set zip
*
* #param string $zip
* #return FosUserUser
*/
public function setZip($zip)
{
$this->zip = $zip;
return $this;
}
/**
* Get zip
*
* #return string
*/
public function getZip()
{
return $this->zip;
}
/**
* Set adress
*
* #param string $adress
* #return FosUserUser
*/
public function setAdress($adress)
{
$this->adress = $adress;
return $this;
}
/**
* Get adress
*
* #return string
*/
public function getAdress()
{
return $this->adress;
}
/**
* Set city
*
* #param string $city
* #return FosUserUser
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Set Newsletter
*
* #param boolean $newsletter
* #return FosUserUser
*/
public function setNewsletter($newsletter)
{
$this->newsletter = $newsletter;
return $this;
}
/**
* Get Newsletter
*
* #return boolean
*/
public function getNewsletter()
{
return $this->newsletter;
}
}
Thank you for your help.
If you have a just created properties of the entity and you are using the metadata cache, the doctrine still doesn't aware about these new properties. Just try to clear the metadata cache.
I am using zendframework 2 and doctrine 2. My addAction doesn't work i don't have any error but when i valid my form no row created in my database !!
i think that i have problem in populating foreign key !
this is my Form:
<?php
// filename : module/Users/src/Users/Form/addForm.php
namespace Vehicules\Form;
use Zend\Form\Form;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
class VehiculeForm extends form implements ObjectManagerAwareInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function getObjectManager()
{
return $this->objectManager;
}
//public function init()
public function __construct(ObjectManager $objectManager)
{
parent::__construct('add');
$this->objectManager = $objectManager;
$this->init();
}
public function init(){
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/formdata');
$this->add(array(
'name' => 'matricule',
'attributes' => array(
'type' => 'text',
'required' => true
),
'options' => array(
'label' => 'Matricule',
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'carburant',
'options' => array(
'label' => 'Carburant',
'value_options' => array(
'0' => 'Essence',
'1' => 'Gasoil',
'2' => 'Hybride',
),
)
));
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'option',
'options' => array(
'label' => 'Options Véhicule',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Vehicules\Entity\optionsvehicule',
'property' => 'libellee',
)));
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'categorie',
'options' => array(
'label' => 'categorie',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Vehicules\Entity\categorie',
'property' => 'idcat',
)
));
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'modele',
'options' => array(
'label' => 'Modèle',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Vehicules\Entity\modele',
'property' => 'nom',
)
));
/*$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'modele',
'options' => array(
'label' => 'Modèle',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Vehicules\Entity\modele',
'property' => 'nom',
'is_method' => true,
'find_method' => array(
'name' => 'findBy',
'params' => array(
'criteria' => array('active' => 1),
// Use key 'orderBy' if using ORM
'orderBy' => array('lastname' => 'ASC'),
// Use key 'sort' if using ODM
'sort' => array('lastname' => 'ASC')
),
),
),
));*/
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'marque',
'options' => array(
'label' => 'Marque',
'object_manager' => $this->getObjectManager(),
'target_class' => 'Vehicules\Entity\marque',
'property' => 'nom',
)
));
$this->add(array(
'name' => 'dateMiseCirculation',
'attributes' => array(
'type' => 'Zend\Form\Element\Date',
),
'options' => array(
'label' => 'Date de Mise en Circulation',
),
));
$this->add(array(
'name' => 'numChasis',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Numero de Chasis',
),
));
$this->add(array(
'name' => "Prix d'achat",
'attributes' => array(
'type' => 'int',
),
'options' => array(
'label' => "Prix d'achat",
),
));
$this->add(array(
'name' => 'concessionnaire',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'concessionnaire',
),
));
$this->add(array(
'name' => 'souslocation',
'attributes' => array(
'type' => 'string',
),
'options' => array(
'label' => 'Sous-location',
),
));
$this->add(array(
'name' => 'remarque',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'remarque',
),
));
$this->add(array(
'name' => 'puisfiscal',
'attributes' => array(
'type' => 'int',
),
'options' => array(
'label' => "puissance fiscale",
),
));
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'nbreport',
'options' => array(
'label' => 'Nombre de portes',
'value_options' => array(
'0' => '4',
'1' => '2',
'2' => '5',
'3' => '6',
'4' => '7',
'5' => '7',
),
)
));
$this->add(array(
'name' => 'dernierKm',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Dernier kilométrage',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Valider'
),
));
}}
and this is my Entity Vehicule:
<?php
namespace Vehicules\Entity;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;
use Doctrine\ORM\Mapping as ORM;
/**
* Vehicule
*
* #ORM\Table(name="vehicule", uniqueConstraints={#ORM\UniqueConstraint(name="VEHICULE_PK", columns={"idVeh"})}, indexes={#ORM\Index(name="ASSOCIATION11_FK", columns={"idCat"}), #ORM\Index(name="ASSOCIATION13_FK", columns={"idMod"})})
* #ORM\Entity
*/
class Vehicule
{ protected $inputFilter;
/**
* #var integer
*
* #ORM\Column(name="idVeh", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idveh;
/**
* #var string
*
* #ORM\Column(name="matricule", type="string", length=254, nullable=false)
*/
private $matricule;
/**
* #var string
*
* #ORM\Column(name="dateMiseCirculation", type="string", length=254, nullable=true)
*/
private $datemisecirculation;
/**
* #var string
*
* #ORM\Column(name="numChasis", type="string", length=254, nullable=false)
*/
private $numchasis;
/**
* #var string
*
* #ORM\Column(name="carburant", type="string", length=254, nullable=true)
*/
private $carburant;
/**
* #var string
*
* #ORM\Column(name="dernierKm", type="decimal", precision=10, scale=0, nullable=false)
*/
private $dernierkm;
/**
* #var integer
*
* #ORM\Column(name="prixachat", type="integer", precision=10, scale=0, nullable=false)
*/
private $prixachat;
/**
* #var string
*
* #ORM\Column(name="concessionnaire", type="string", length=254, nullable=true)
*/
private $concessionnaire;
/**
* #var integer
*
* #ORM\Column(name="sousLocation", type="smallint", nullable=true)
*/
private $souslocation;
/**
* #var string
*
* #ORM\Column(name="remarque", type="string", length=254, nullable=true)
*/
private $remarque;
/**
* #var integer
*
* #ORM\Column(name="puisFiscal", type="integer", nullable=true)
*/
private $puisfiscal;
/**
* #var integer
*
* #ORM\Column(name="nbrePort", type="integer", nullable=true)
*/
private $nbreport;
/**
* #var \Vehicules\Entity\Categorie
*
* #ORM\ManyToOne(targetEntity="Vehicules\Entity\Categorie")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idCat", referencedColumnName="idCat")
* })
*/
private $idcat;
/**
* #var \Vehicules\Entity\Modele
*
* #ORM\ManyToOne(targetEntity="Vehicules\Entity\Modele")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idMod", referencedColumnName="idMod")
* })
*/
private $idmod;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Vehicules\Entity\Optionsvehicule", inversedBy="idveh")
* #ORM\JoinTable(name="veh_option",
* joinColumns={
* #ORM\JoinColumn(name="idVeh", referencedColumnName="idVeh")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="idOptVeh", referencedColumnName="idOptVeh")
* }
* )
*/
private $idoptveh;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Vehicules\Entity\Vehiculestatut", inversedBy="idveh")
* #ORM\JoinTable(name="veh_status",
* joinColumns={
* #ORM\JoinColumn(name="idVeh", referencedColumnName="idVeh")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="idStatut", referencedColumnName="idStatut")
* }
* )
*/
private $idstatut;
/**
* Constructor
*/
public function __construct()
{
$this->idoptveh = new \Doctrine\Common\Collections\ArrayCollection();
$this->idstatut = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get idveh
*
* #return integer
*/
public function getIdveh()
{
return $this->idveh;
}
/**
* Set matricule
*
* #param string $matricule
* #return Vehicule
*/
public function setMatricule($matricule)
{
$this->matricule = $matricule;
return $this;
}
/**
* Get matricule
*
* #return string
*/
public function getMatricule()
{
return $this->matricule;
}
/**
* Set datemisecirculation
*
* #param string $datemisecirculation
* #return Vehicule
*/
public function setDatemisecirculation($datemisecirculation)
{
$this->datemisecirculation = $datemisecirculation;
return $this;
}
/**
* Get datemisecirculation
*
* #return string
*/
public function getDatemisecirculation()
{
return $this->datemisecirculation;
}
/**
* Set numchasis
*
* #param string $numchasis
* #return Vehicule
*/
public function setNumchasis($numchasis)
{
$this->numchasis = $numchasis;
return $this;
}
/**
* Get numchasis
*
* #return string
*/
public function getNumchasis()
{
return $this->numchasis;
}
/**
* Set carburant
*
* #param string $carburant
* #return Vehicule
*/
public function setCarburant($carburant)
{
$this->carburant = $carburant;
return $this;
}
/**
* Get carburant
*
* #return string
*/
public function getCarburant()
{
return $this->carburant;
}
/**
* Set dernierkm
*
* #param string $dernierkm
* #return Vehicule
*/
public function setDernierkm($dernierkm)
{
$this->dernierkm = $dernierkm;
return $this;
}
/**
* Get dernierkm
*
* #return string
*/
public function getDernierkm()
{
return $this->dernierkm;
}
/**
* Set prixachat
*
* #param integer $prixachat
* #return Vehicule
*/
public function setPrixachat($prixachat)
{
$this->prixachat = $prixachat;
return $this;
}
/**
* Get prixachat
*
* #return integer
*/
public function getPrixachat()
{
return $this->prixachat;
}
/**
* Set concessionnaire
*
* #param string $concessionnaire
* #return Vehicule
*/
public function setConcessionnaire($concessionnaire)
{
$this->concessionnaire = $concessionnaire;
return $this;
}
/**
* Get concessionnaire
*
* #return string
*/
public function getConcessionnaire()
{
return $this->concessionnaire;
}
/**
* Set souslocation
*
* #param integer $souslocation
* #return Vehicule
*/
public function setSouslocation($souslocation)
{
$this->souslocation = $souslocation;
return $this;
}
/**
* Get souslocation
*
* #return integer
*/
public function getSouslocation()
{
return $this->souslocation;
}
/**
* Set remarque
*
* #param string $remarque
* #return Vehicule
*/
public function setRemarque($remarque)
{
$this->remarque = $remarque;
return $this;
}
/**
* Get remarque
*
* #return string
*/
public function getRemarque()
{
return $this->remarque;
}
/**
* Set puisfiscal
*
* #param integer $puisfiscal
* #return Vehicule
*/
public function setPuisfiscal($puisfiscal)
{
$this->puisfiscal = $puisfiscal;
return $this;
}
/**
* Get puisfiscal
*
* #return integer
*/
public function getPuisfiscal()
{
return $this->puisfiscal;
}
/**
* Set nbreport
*
* #param integer $nbreport
* #return Vehicule
*/
public function setNbreport($nbreport)
{
$this->nbreport = $nbreport;
return $this;
}
/**
* Get nbreport
*
* #return integer
*/
public function getNbreport()
{
return $this->nbreport;
}
/**
* Set idcat
*
* #param \Vehicules\Entity\Categorie $idcat
* #return Vehicule
*/
public function setIdcat(\Vehicules\Entity\Categorie $idcat = null)
{
$this->idcat = $idcat;
return $this;
}
/**
* Get idcat
*
* #return \Vehicules\Entity\Categorie
*/
public function getIdcat()
{
return $this->idcat;
}
/**
* Set idmod
*
* #param \Vehicules\Entity\Modele $idmod
* #return Vehicule
*/
public function setIdmod(\Vehicules\Entity\Modele $idmod = null)
{
$this->idmod = $idmod;
return $this;
}
/**
* Get idmod
*
* #return \Vehicules\Entity\Modele
*/
public function getIdmod()
{
return $this->idmod;
}
/**
* Add idoptveh
*
* #param \Vehicules\Entity\Optionsvehicule $idoptveh
* #return Vehicule
*/
public function addIdoptveh(\Vehicules\Entity\Optionsvehicule $idoptveh)
{
$this->idoptveh[] = $idoptveh;
return $this;
}
/**
* Remove idoptveh
*
* #param \Vehicules\Entity\Optionsvehicule $idoptveh
*/
public function removeIdoptveh(\Vehicules\Entity\Optionsvehicule $idoptveh)
{
$this->idoptveh->removeElement($idoptveh);
}
/**
* Get idoptveh
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getIdoptveh()
{
return $this->idoptveh;
}
/**
* Add idstatut
*
* #param \Vehicules\Entity\Vehiculestatut $idstatut
* #return Vehicule
*/
public function addIdstatut(\Vehicules\Entity\Vehiculestatut $idstatut)
{
$this->idstatut[] = $idstatut;
return $this;
}
/**
* Remove idstatut
*
* #param \Vehicules\Entity\Vehiculestatut $idstatut
*/
public function removeIdstatut(\Vehicules\Entity\Vehiculestatut $idstatut)
{
$this->idstatut->removeElement($idstatut);
}
/**
* Get idstatut
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getIdstatut()
{
return $this->idstatut;
}
public function populate($data) {
$this->setMatricule($data['matricule']) ;
$this->setDatemisecirculation($data['dateMiseCirculation']) ;
$this->setNumchasis($data['numChasis']) ;
$this->setCarburant($data['carburant']) ;
$this->setDernierkm($data['dernierKm']) ;
$this->setPrixachat($data["Prix d'achat"]) ;
$this->setConcessionnaire($data['concessionnaire']) ;
$this->setSouslocation($data['souslocation']) ;
$this->setRemarque($data['remarque']) ;
$this->setPuisfiscal($data['puisfiscal']) ;
$this->setNbreport($data['nbreport']) ;
//$this->addIdoptveh($data['option']) ; /* select................*/
//$this->setIdmod() ; /* select................*/
//$this->addIdstatut() ; /*ghanakhd l option dyal libre */
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'matricule',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 4,
'max' => 14,
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'option',
'required' => false,
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
this is my controller VehiculeController:
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* #link http://github.com/zendframework/Vehicules for the canonical source repository
* #copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* #license http://framework.zend.com/license/new-bsd New BSD License
*
*/
namespace Vehicules\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Vehicules\Form\VehiculeForm;
use Vehicules\Entity\Vehicule;
class VehiculesController extends AbstractActionController
{
/**
* #var Doctrine\ORM\EntityManager
*/
protected $_objectManager;
protected function getObjectManager()
{
if (!$this->_objectManager) {
$this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->_objectManager;
}
public function indexAction()
{
$vehicules = $this->getObjectManager()->getRepository('Vehicules\Entity\Vehicule')->findAll();
return new ViewModel(array('vehicules' => $vehicules));
}
public function addAction()
{ $_objectManager=$this->getObjectManager();
$form = new VehiculeForm($_objectManager);
$request = $this->getRequest();
$post = $this->request->getPost();
if ($this->request->isPost()) {
$Vehicule= new Vehicule();
$form->setData($post);
$form->setInputFilter($Vehicule->getInputFilter());
if ($form->isValid()) {
$f=$form->getData();
$Vehicule->populate($f);
$cat = $this->getObjectManager()->getRepository('Vehicules\Entity\categorie')->findAll();
foreach ($cat as $c){
if($c->getIdcat()==$f['categorie'] ){
$Vehicule->setIdcat($c) ;
exit;
}
}
$mod = $this->getObjectManager()->getRepository('Vehicules\Entity\modele')->findAll();
foreach ($mod as $m){
if($m->getNom()==$f['modele'] ){
$Vehicule->setIdmod($m->getIdmod()) ;
exit;
}
}
$objectManager = $this->getObjectManager();
$objectManager->persist($Vehicule);
$objectManager->flush();
$id=$Vehicule->getIdveh();
var_dump($id);
$viewModel = new ViewModel(array('form' =>$form,'donne'=>$id));
return $viewModel;
}
}
$viewModel = new ViewModel(array('form' =>$form));
return $viewModel;
}
public function editAction()
{
$id = (int) $this->getEvent()->getRouteMatch()->getParam('id');
if (!$id) {
return $this->redirect()->toRoute('vehicules/default', array('controller'=>'vehicules','action'=>'add'));
}
$vehicule = $this->getObjectManager()->find('Vehicules\Entity\vehicule', $id);
$objectManager= $this->getObjectManager();
$form = new VehiculeForm($objectManager);
$form->setBindOnValidate(false);
$form->bind($vehicule);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->post());
if ($form->isValid()) {
$form->bindValues();
$this->getEntityManager()->flush();
// Redirect to list of vehicules
return $this->redirect()->toRoute('vehicules/default', array('controller'=>'vehicules','action'=>'index'));
}
}
return array(
'id' => $id,
'form' => $form,
);
}
public function deleteAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('idVeh');
if (!$id) {
return $this->redirect()->toRoute('vehicules');
}
$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->post()->get('del', 'No');
if ($del == 'Yes') {
$id = (int)$request->post()->get('id');
$vehicule = $this->getEntityManager()->find('Vehicules\Entity\vehicule', $id);
if ($vehicule) {
$this->getEntityManager()->remove($vehicule);
$this->getEntityManager()->flush();
}
}
// Redirect to list of albums
return $this->redirect()->toRoute('default', array(
'controller' => 'vehicules',
'action' => 'index',
));
}
return array(
'id' => $id,
'vehicule' => $this->getEntityManager()->find('Vehicules\Entity\vehicule', $id)->getArrayCopy()
);
}
}
to populate forgnein key idMod and idCat i tried with this:
$mod = $this->getObjectManager()->getRepository('Vehicules\Entity\modele')->findAll();
foreach ($mod as $m){
if($m->getNom()==$f['modele'] ){
$Vehicule->setIdmod($m->getIdmod()) ;
exit;
}
}
but id doesn't work :/
Your issue is the form/entity hydration. You don't need to reinvent the wheel here as Zend\Form already takes care of getting the data in and out of a form.
Currently your form uses the default ArraySerializable hydrator, so when the form is validated and you fetch the data (using $form->getData()) you are given an array. What you want is a already populated Vehicule entity.
The DoctrineModule\Stdlib\Hydrator\DoctrineObject is a hydrator specifically for Doctrine. If you attach it as the hydrator to your form it will return hydrated entity.
To solve this you need to STOP creating the form using new
$form = new VehiculeForm($_objectManager);
Instead create it via the ServiceManager
$form = $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
Now create the form from a service factory; allowing the injection of the form dependencies, including the hydrator.
MyModule/Factory/Form/VehiculeFormFactory.php
namespace MyModule\Factory\Form;
use MyModule\Entity;
use MyModule\Form;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
use DoctrineModule\Stdlib\Hydrator;
class VehiculeFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $sl)
{
$objectManager = $sl->get('Doctrine\ORM\EntityManager');
$form = new Form\VehiculeForm($objectManager);
$vehicule = new Entity\Vehicule();
// create the hydrator; this could also be done via the
// hydrator manager but create here for the example
$hydrator = new DoctrineObject($objectManager);
$form->setHydrator($hydrator);
$form->setObject($vehicule);
// We can also take care of the input filter here too!
// meaning less code in the controller and only
// one place to modify should it change
$form->setInputFilter($Vehicule->getInputFilter());
return $form;
}
}
Module.php
public function getFormElementConfig()
{
return array(
'factories' => array(
'MyModule\Form\VehiculeForm' => 'MyModule\Factory\Form\VehiculeForm' // path to our new factory
),
);
}
You can now get rid of your populate() method and allot of the controller code.
FooController.php
//...
protected function getVehiculeForm()
{
return $this->getServiceLocator()->get('MyModule\Form\VehiculeForm');
}
public function addAction()
{
$request = $this->getRequest();
$form = $this->getVehiculeForm();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$vehicule = $form->getData(); // returns the entity!
if ($vehicule instanceof Vehicule) {
/// Fully hydrated entity!
var_dump($vehicule);
}
}
}
//...
}
//...
I'm trying to upload several pictures in Symfony 4.4 but I got this error:
Call to a member function guessExtension() on string
I have ManyToOne relation between Event and Picture.
Each Event can be associated with many Pictures but, each picture can be associated with only one Event.
My entity Event :
/**
* #ORM\Entity(repositoryClass="App\Repository\EventRepository")
*/
class Event
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $title;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Picture", mappedBy="event")
*/
private $pictures;
/**
* getter and setter for $this->title
*/
public function __construct()
{
$this->pictures = new ArrayCollection();
}
/**
* #return Collection|Picture[]
*/
public function getPictures()
{
return $this->pictures;
}
public function addPicture(Picture $picture)
{
if (!$this->pictures->contains($picture)) {
$this->pictures[] = $picture;
$picture->setEvent($this);
}
return $this;
}
public function removePicture(Picture $picture)
{
if ($this->pictures->contains($picture)) {
$this->pictures->removeElement($picture);
// set the owning side to null (unless already changed)
if ($picture->getEvent() === $this) {
$picture->setEvent(null);
}
}
return $this;
}
}
My entity Picture :
/**
* #ORM\Entity(repositoryClass="App\Repository\PictureRepository")
*/
class Picture
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="pictures")
* #ORM\JoinColumn(nullable=false)
*/
private $event;
public function getId(): ?int
{
return $this->id;
}
/**
* getter and setter for $this->name
*/
public function getEvent()
{
return $this->event;
}
public function setEvent(?Event $event)
{
$this->event = $event;
return $this;
}
}
EventType Form :
$builder
->add('title', TextType::class)
->add('pictures', CollectionType::class, [
'entry_type' => PictureType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'label' => false
])
;
PictureType Form
$builder
->add('name', FileType::class, [
'data_class' => null,
'label' => ' '
])
;
My controller
/**
* #Route("/new", name="admin-spectacle-new")
*/
public function new(Request $request)
{
$event = new Event();
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$images = $form->get('pictures')->getData();
foreach ($images as $image) {
$fileName = md5(uniqid()).'.'.$image->getName()->guessExtension();
$image->move($this->getParameter('image_spectacle'), $fileName);
$image->setName($fileName);
}
//...
}
return $this->render(...);
}
Any ideas why I am getting this error?
dump for $images
object(Doctrine\Common\Collections\ArrayCollection)[1082]
private 'elements' =>
array (size=2)
0 =>
object(App\Entity\Picture)[1183]
private 'id' => null
private 'name' => string 'C:\wamp64\tmp\php35B3.tmp' (length=25)
private 'event' =>
object(App\Entity\Event)[705]
...
dump for $image
object(App\Entity\Picture)[1581]
private 'id' => null
private 'name' => string 'C:\wamp64\tmp\phpD132.tmp' (length=25)
private 'event' =>
object(App\Entity\Event)[1103]
private 'id' => null
private 'title' => string 'azed' (length=4)
private 'description' => null
private 'age' => null
private 'synopsis' => null
private 'resume' => null
private 'details' => null
private 'pdf' => null
private 'address' => null
private 'schedule' => null
private 'minia_picture' => string 'azed-5e620d41cbcc8.jpeg' (length=23)
private 'header_picture' => string 'azed-5e620d41cc71e.jpeg' (length=23)
private 'cover_picture' => string 'azed-5e620d41cce11.png' (length=22)
private 'is_active' => int 1
private 'categoryEvent' => null
private 'pictures' =>
object(Doctrine\Common\Collections\ArrayCollection)[1104]
private 'elements' =>
array (size=2)
...
Any idea?
guessExtension() is meant to works on a File object, not a simple string path.
use Symfony\Component\HttpFoundation\File\File;
foreach ($images as $image) {
$file=new File($image->getName());
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getParameter('image_spectacle'), $fileName);
$image->setName($fileName);
}