How to make multiply select box from entity in a form in Symfony 2 - forms

I'm working on a form in Symfony 2. The form is for my projects and each of my project can be an artistic, technic or both categorie of project. At first, I tried doing a select, butt it only allow me to choose one of those two categories.
Than I tough that I could generate a select box for each categories in my entity. That way, I could choose as much categories I want.
-Now, how do I do that?
-I would like that the checkbox be selected when I pass a project to the form (if the project is techic project, the technic checkbox is selected)... I don't now if the form would take care of that by itself.
Here is my form:
<?php
namespace AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Class ProjetType extends AbstractType
{
public function buildForm(FormBuilderInterface $constructeur, array $options)
{
$constructeur
->add('image', 'text')
->add('technologie', 'text')
->add('annee', 'text', array('label'=>'année'))
->add('type', 'entity', [
'class'=>'PublicBundle\Entity\Type',
'property'=>'nom',
'required'=>true,
])
->add('fichier', 'text')
->add('largeur', 'text')
->add('hauteur', 'text')
//What I had before
/*->add('categories', 'entity', [
'label'=>'catégorie',
'class'=>'PublicBundle\Entity\Categorie',
'property'=>'tag',
'required'=>true,
])*/
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdminBundle\Entity\Projet',
));
}
public function getName()
{
return 'projet';
}
}
My project entity:
<?php
namespace PublicBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Projet
*
* #ORM\Table(name="pt_projet");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\ProjetDepot")
*/
class Projet
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID du projet
protected $id;
/**
* #ORM\OneToMany(targetEntity="ProjetInt", mappedBy="projet", orphanRemoval=true)
*/
protected $descriptions;
/**
* #ORM\Column(name="pro_img", type="string", length=64, unique=true)
*/
//Nom du fichier de l'image du projet
protected $image;
/**
* #ORM\Column(name="pro_technologie_utilisee", type="text", length=200)
*/
//Text qui liste tout les technologies utilisées pour le projet
protected $technologie;
/**
* #ORM\Column(name="pro_annee", type="integer", length=4)
*/
//Année de réalisation du projet
protected $annee;
/**
* #ORM\ManyToOne(targetEntity="Type", inversedBy="projets")
* #ORM\JoinColumn(name="pro_type", referencedColumnName="id", nullable=false)
*/
//Clef étrangère du type de projet
//Le type de projet ne correspond pas à la catégore. Il peu être Unity, flash, image, vidéo, etc. Il permet de savoir quelle page charger pour pouvoir intégrer le projet dans le portfolio.
protected $type;
/**
* #ORM\Column(name="pro_fichier", type="string", length=64, unique=true)
*/
//Nom du fichier du projet
private $fichier;
/**
* #ORM\Column(name="pro_largeur", type="integer")
*/
//Largeur du projet
protected $largeur;
/**
* #ORM\Column(name="pro_hauteur", type="integer")
*/
//Hauteur du projet
protected $hauteur;
/**
* #ORM\ManyToMany(targetEntity="Categorie", cascade={"persist"})
*/
//La ou les catégories du projet
private $categories;
/**
* Constructor
*/
public function __construct()
{
$this->descriptions=new ArrayCollection();
$this->categories=new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set image
*
* #param string $image
* #return Projet
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* #return string
*/
public function getImage()
{
return $this->image;
}
/**
* Set technologie
*
* #param string $technologie
* #return Projet
*/
public function setTechnologie($technologie)
{
$this->technologie = $technologie;
return $this;
}
/**
* Get technologie
*
* #return string
*/
public function getTechnologie()
{
return $this->technologie;
}
/**
* Set annee
*
* #param integer $annee
* #return Projet
*/
public function setAnnee($annee)
{
$this->annee = $annee;
return $this;
}
/**
* Get annee
*
* #return integer
*/
public function getAnnee()
{
return $this->annee;
}
/**
* Set fichier
*
* #param string $fichier
* #return Projet
*/
public function setFichier($fichier)
{
$this->fichier = $fichier;
return $this;
}
/**
* Get fichier
*
* #return string
*/
public function getFichier()
{
return $this->fichier;
}
/**
* Set largeur
*
* #param integer $largeur
* #return Projet
*/
public function setLargeur($largeur)
{
$this->largeur = $largeur;
return $this;
}
/**
* Get largeur
*
* #return integer
*/
public function getLargeur()
{
return $this->largeur;
}
/**
* Set hauteur
*
* #param integer $hauteur
* #return Projet
*/
public function setHauteur($hauteur)
{
$this->hauteur = $hauteur;
return $this;
}
/**
* Get hauteur
*
* #return integer
*/
public function getHauteur()
{
return $this->hauteur;
}
/**
* Add descriptions
*
* #param \PublicBundle\Entity\ProjetInt $descriptions
* #return Projet
*/
public function addDescription(\PublicBundle\Entity\ProjetInt $descriptions)
{
$this->descriptions[] = $descriptions;
return $this;
}
/**
* Remove descriptions
*
* #param \PublicBundle\Entity\ProjetInt $descriptions
*/
public function removeDescription(\PublicBundle\Entity\ProjetInt $descriptions)
{
$this->descriptions->removeElement($descriptions);
}
/**
* Get descriptions
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getDescriptions()
{
return $this->descriptions;
}
/**
* Set type
*
* #param \PublicBundle\Entity\Type $type
* #return Projet
*/
public function setType(\PublicBundle\Entity\Type $type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return \PublicBundle\Entity\Type
*/
public function getType()
{
return $this->type;
}
/**
* Add categories
*
* #param \PublicBundle\Entity\Categorie $categories
* #return Projet
*/
public function addCategory(\PublicBundle\Entity\Categorie $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* #param \PublicBundle\Entity\Categorie $categories
*/
public function removeCategory(\PublicBundle\Entity\Categorie $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
}
Category entity:
<?php
namespace PublicBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Catégorie
*
* #ORM\Table(name="pt_categorie");
* #ORM\Entity
* #ORM\Entity(repositoryClass="PublicBundle\Entity\CategorieDepot")
*/
class Categorie
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
//ID de la catégorie
protected $id;
/**
* #ORM\Column(name="cat_tag", type="string",length=255, unique=true)
*/
//Tag de la catégorie
protected $tag;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set tag
*
* #param string $tag
* #return Categorie
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return string
*/
public function getTag()
{
return $this->tag;
}
}

Use multiple and expanded options of entity field type:
->add('categories', 'entity', [
'label'=>'catégorie',
'class'=>'PublicBundle\Entity\Categorie',
'property'=>'tag',
'required'=>true,
'multiple'=>true,
'expanded'=>true,
])
Documentation on topic: http://symfony.com/doc/current/reference/forms/types/choice.html#select-tag-checkboxes-or-radio-buttons

Related

How embed fields of an other entity in sonata form?

I have two entities Sport and Tarif (translated by price) linked by ManyToOne relation.
I would like to have just one admin form (in Sonata Admin Bundle) to create or delete a Sport with three fields :
libelle ( = name)
valeurDeBase ( = price value) which is a number attribute of Tarif entity
conditionDeReduction ( = condition to have a discount) which is a text attribute of Tarif entity
I'm searching a way to do that and I found the use of CollectionType (https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html) to embed the Tarif fields in SportAdmin form but that's not working as you can see below :
Here are the entities :
Sport.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sport
*
* #ORM\Table(name="sport")
* #ORM\Entity(repositoryClass="AppBundle\Repository\SportRepository")
*/
class Sport
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="libelle", type="string", length=255, unique=true)
*/
private $libelle;
/**
* Un Sport est lié à 1 et 1 seul Tarif
* #ORM\ManyToOne(targetEntity="Tarif")
* #ORM\JoinColumn(nullable=false)
*/
private $tarif;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* #param string $libelle
* #return Sport
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* #return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Constructor
*/
public function __construct()
{
$this->licences = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add licence
*
* #param \AppBundle\Entity\Licence $licence
*
* #return Sport
*/
public function addLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences[] = $licence;
return $this;
}
/**
* Remove licence
*
* #param \AppBundle\Entity\Licence $licence
*/
public function removeLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences->removeElement($licence);
}
/**
* Get licences
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getLicences()
{
return $this->licences;
}
/**
* Set tarif
*
* #param \AppBundle\Entity\Tarif $tarif
*
* #return Sport
*/
public function setTarif(\AppBundle\Entity\Tarif $tarif)
{
$this->tarif = $tarif;
return $this;
}
/**
* Get tarif
*
* #return \AppBundle\Entity\Tarif
*/
public function getTarif()
{
return $this->tarif;
}
}
Tarif.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tarif
*
* #ORM\Table(name="tarif")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TarifRepository")
*/
class Tarif
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="valeurDeBase", type="decimal", precision=10, scale=2)
*/
private $valeurDeBase;
/**
* #var string
*
* #ORM\Column(name="conditionReduction", type="text", nullable=true)
*/
private $conditionReduction;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set valeurDeBase
*
* #param string $valeurDeBase
* #return Tarif
*/
public function setValeurDeBase($valeurDeBase)
{
$this->valeurDeBase = $valeurDeBase;
return $this;
}
/**
* Get valeurDeBase
*
* #return string
*/
public function getValeurDeBase()
{
return $this->valeurDeBase;
}
/**
* Set conditionReduction
*
* #param string $conditionReduction
* #return Tarif
*/
public function setConditionReduction($conditionReduction)
{
$this->conditionReduction = $conditionReduction;
return $this;
}
/**
* Get conditionReduction
*
* #return string
*/
public function getConditionReduction()
{
return $this->conditionReduction;
}
}
SportAdmin.php
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', CollectionType::class, array(
'by_reference' => false
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
TarifAdmin.php
<?php
// src/AppBundle/Admin/TarifAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class TarifAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('valeurDeBase', 'number');
$formMapper->add('conditionReduction', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
}
protected function configureListFields(ListMapper $listMapper)
{
}
public function toString($object)
{
return $object instanceof Tarif
? $object->getTitle()
: 'Tarif'; // shown in the breadcrumb on the create view
}
}
Thank you for your help.
Finally I have created an admin block for Tarif and embed it in SportAdmin with sonata_type_admin. It works perfectly.
Here the SportAdmin.php
<?php
// src/AppBundle/Admin/SportAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', 'sonata_type_admin', array(), array(
'admin_code' => 'admin.tarif'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}

How to populate one-to-many/many-to-one tables with symfony2 forms?

I'm new in Symfony and I'm stuck with populating one-to-many/many-to-one tables.
I managed to display the form but when I submit it I got the following error:
Found entity of type AppBundle\Entity\Products on association AppBundle\Entity\Customers#customers_products, but expecting AppBundle\Entity\CustomersProducts
Here is my code:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\CustomersProducts;
/**
* Customers
*
* #ORM\Table(name="customers")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CustomersRepository")
*/
class Customers
{
/** #ORM\OneToMany(targetEntity="CustomersProducts", mappedBy="customers") */
protected $customers_products;
public function __construct()
{
$this->customers_products = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getCustomersProducts()
{
return $this->customers_products->toArray();
}
public function setCustomersProducts($customers_products)
{
if (count($customers_products) > 0) {
foreach ($customers_products as $i) {
$this->addCustomersProducts($i);
}
}
return $this;
}
public function addCustomersProducts( $customers_products)
{
$this->customers_products->add($customers_products);
}
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="ip", type="string", length=255)
*/
private $ip;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Customers
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set ip
*
* #param string $ip
* #return Customers
*/
public function setIp($ip)
{
$this->ip = $ip;
return $this;
}
/**
* Get ip
*
* #return string
*/
public function getIp()
{
return $this->ip;
}
}
Products.php:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Products
*
* #ORM\Table(name="products")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ProductsRepository")
*/
class Products
{
/** #ORM\OneToMany(targetEntity="CustomersProducts", mappedBy="products") */
protected $customers_products;
public function __construct()
{
$this->customers_products = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Products
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
CustomersProducts.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* CustomersProducts
*
* #ORM\Table(name="customers_products")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CustomersProductsRepository")
*/
class CustomersProducts
{
/**
*
* #ORM\ManyToOne(targetEntity="Customers", inversedBy="customers_products")
* #ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=false)
*/
protected $customers;
/**
*
* #ORM\ManyToOne(targetEntity="Products", inversedBy="customers_products")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)
*/
protected $products;
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="version", type="string", length=255)
*/
private $version;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set version
*
* #param string $version
* #return CustomersProducts
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
}
/**
* Get version
*
* #return string
*/
public function getVersion()
{
return $this->version;
}
}
CustomersType.php
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CustomersType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('ip')
->add('customers_products', 'entity', array(
'class' => 'AppBundle:Products',
'multiple' =>true,
'expanded' => true,
'property' => 'name'
));
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Customers'
));
}
}

The target-entity cannot be found

I know this question has already been asked more than once, but I still can't figure what goes wrong.
I'm currently trying to learn Symfony2 (using version 2.7.4), and I'm getting the following error when I'm trying to reach a page that's supposed to have an image in it :
The target-entity Entity\Image cannot be found in 'OC\PlatformBundle\Entity\Advert#Image'.
Here is my code:
**advert.php**
<?php
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="OC\PlatformBundle\Entity\AdvertRepository")
*/
class Advert
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="published", type="boolean")
*/
private $published = true;
/**
* #ORM\OneToOne(targetEntity="Entity\Image", cascade={"persist"})
*/
private $image;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* Set image
*
* #param \OC\PlatformBundle\Entity\Image $image
*
* #return Advert
*/
public function setImage(\OC\PlatformBundle\Entity\Image $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get Image
*
* #return \OC\PlatformBundle\Entity\Image
*/
public function getImage()
{
return $this->Image;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
public function __construct()
{
// Par défaut, la date de l'annonce est la date d'aujourd'hui
$this->date = new \Datetime();
}
/**
* Set date
*
* #param \DateTime $date
*
* #return Advert
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* #return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set title
*
* #param string $title
*
* #return Advert
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
*
* #return Advert
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set content
*
* #param string $content
*
* #return Advert
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set published
*
* #param boolean $published
*
* #return Advert
*/
public function setPublished($published)
{
$this->published = $published;
return $this;
}
/**
* Get published
*
* #return boolean
*/
public function getPublished()
{
return $this->published;
}
}
file Image :
<?php
// src/OC/PlatformBundle/Entity/Image
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Image
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* #ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
}
I understood that this problem mainly comes from a typo in the call of the entity, but I triple-checked everything, and it seems alright to me... Is there something I didn't see ?
Thank you in advance
You should fix your annotation:
/**
* #ORM\OneToOne(targetEntity="OC\PlatformBundle\Entity\Image", cascade={"persist"})
*/
private $image;

I get error when I created one form with 2 entities mapped

I have 2 entities, Topic.php and Post.php I would like to have this:
TopicType.php :
<?php
namespace BISSAP\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TopicType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text', array(
'label' => 'Titre'))
->add('subForm', new PostType())
->add('save', 'submit', array(
'attr' => array(
'class' => 'btn right-flt')))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BISSAP\ForumBundle\Entity\Topic'
));
}
/**
* #return string
*/
public function getName()
{
return 'bissap_forumbundle_topic';
}
}
PostType.php :
<?php
namespace BISSAP\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PostType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', 'ckeditor', array(
'label' => 'Votre message',
'config_name' => 'my_custom_config',
'config' => array('language' => 'fr')))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'BISSAP\ForumBundle\Entity\Post'
));
}
/**
* #return string
*/
public function getName()
{
return 'bissap_forumbundle_post';
}
}
Topic.php :
<?php
namespace BISSAP\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/*use BISSAP\BodyConcept\Entity\Forum;
*/
/**
* Topic
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\TopicRepository")
*/
class Topic
{
/**
* #ORM\ManyToOne(targetEntity="Forum", inversedBy="Topics", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $forum;
/**
* #var ArrayCollection $Posts
*
* #ORM\OneToMany(targetEntity="Post", mappedBy="Topic", cascade={"persist", "remove", "merge"})
*/
private $posts;
/**
* #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;
/**
* #ORM\ManyToOne(targetEntity="BISSAP\UserBundle\Entity\User", inversedBy="Topics", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #var integer
*
* #ORM\Column(name="view_count", type="integer")
*/
private $viewCount;
/**
* #var \DateTime
*
* #ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* #var integer
*
* #ORM\Column(name="reply_count", type="integer")
*/
private $replyCount;
/**
* #var string
*
* #ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* #var string
*
* #ORM\Column(name="genre", type="string", length=255)
*/
private $genre;
/**
* #var integer
*
* #ORM\Column(name="last_post", type="integer")
*/
private $lastPost;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Topic
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set user
*
* #param integer $user
* #return Topic
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return integer
*/
public function getUser()
{
return $this->user;
}
/**
* Set viewCount
*
* #param integer $viewCount
* #return Topic
*/
public function setViewCount($viewCount)
{
$this->viewCount = $viewCount;
return $this;
}
/**
* Get viewCount
*
* #return integer
*/
public function getViewCount()
{
return $this->viewCount;
}
/**
* Set dateCreation
*
* #param \DateTime $dateCreation
* #return Topic
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set replyCount
*
* #param integer $replyCount
* #return Topic
*/
public function setReplyCount($replyCount)
{
$this->replyCount = $replyCount;
return $this;
}
/**
* Get replyCount
*
* #return integer
*/
public function getReplyCount()
{
return $this->replyCount;
}
/**
* Set slug
*
* #param string $slug
* #return Topic
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set genre
*
* #param string $genre
* #return Topic
*/
public function setGenre($genre)
{
$this->genre = $genre;
return $this;
}
/**
* Get genre
*
* #return string
*/
public function getGenre()
{
return $this->genre;
}
/**
* Set lastPost
*
* #param integer $lastPost
* #return Topic
*/
public function setLastPost($lastPost)
{
$this->lastPost = $lastPost;
return $this;
}
/**
* Get lastPost
*
* #return integer
*/
public function getLastPost()
{
return $this->lastPost;
}
/**
* Set content
*
* #param string $content
* #return Topic
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set forum
*
* #param Forum $forum
* #return Topic
*/
/*public function setForum(\BISSAP\BodyConceptBundle\Entity\Forum $forum)*/
public function setForum(Forum $forum)
{
$this->forum = $forum;
return $this;
}
/**
* Get forum
*
* #return \BISSAP\BodyConceptBundle\Entity\Forum
*/
public function getForum()
{
return $this->forum;
}
/**
* Constructor
*/
public function __construct()
{
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
$this->dateCreation = new \DateTime();
}
/**
* Add posts
*
* #param \BISSAP\ForumBundle\Entity\Post $posts
* #return Topic
*/
public function addPost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts[] = $posts;
return $this;
}
/**
* Remove posts
*
* #param \BISSAP\ForumBundle\Entity\Post $posts
*/
public function removePost(\BISSAP\ForumBundle\Entity\Post $posts)
{
$this->posts->removeElement($posts);
}
/**
* Get posts
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
Post.php :
<?php
namespace BISSAP\ForumBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="BISSAP\ForumBundle\Entity\PostRepository")
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="BISSAP\UserBundle\Entity\User", inversedBy="Posts", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="date_creation", type="datetime")
*/
private $dateCreation;
/**
* #var \DateTime
*
* #ORM\Column(name="date_modif", type="datetime")
*/
private $dateModif;
/**
* #var string
*
* #ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* #ORM\ManyToOne(targetEntity="Topic", inversedBy="Posts", cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $topic;
/**
* Constructor
*/
public function __construct()
{
$this->dateCreation = new \DateTime();
$this->dateModif = new \DateTime();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* 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 dateCreation
*
* #param \DateTime $dateCreation
* #return Post
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* #return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set dateModif
*
* #param \DateTime $dateModif
* #return Post
*/
public function setDateModif($dateModif)
{
$this->dateModif = $dateModif;
return $this;
}
/**
* Get dateModif
*
* #return \DateTime
*/
public function getDateModif()
{
return $this->dateModif;
}
/**
* Set slug
*
* #param string $slug
* #return Post
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set Topic
*
* #param Topic $topic
* #return Post
*/
/*public function setTopic(\BISSAP\ForumBundle\Entity\Topic $topic)*/
public function setTopic(Topic $topic)
{
$this->topic = $topic;
return $this;
}
/**
* Get Topic
*
* #return \BISSAP\ForumBundle\Entity\Topic
*/
public function getTopic()
{
return $this->topic;
}
/**
* Set user
*
* #param \BISSAP\ForumBundle\Entity\User $user
* #return Post
*/
public function setUser(\BISSAP\UserBundle\Entity\User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \BISSAP\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
And this is my part of my controller, here i call and use the form:
public function categoryAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$topic = new \BISSAP\ForumBundle\Entity\Topic();
$form = $this->createForm(new TopicType(), $topic);
$user = $this->getUser();
$forum = $em->getRepository('BISSAPForumBundle:Forum')->find(1);
if ($form->handleRequest($request)->isValid()) {
$topic->setUser($user);
$topic->setForum($forum);
$topic->setViewCount(23);
$topic->setReplyCount(123);
$topic->setLastPost(25);
$topic->setSlug('slug_sluggg');
$topic->setGenre('genre');
$em->persist($topic);
$em->flush();
return $this->render('BISSAPForumBundle:F:topic-forum.html.twig', array('user' => $user, 'topic' => $topic));
}
return $this->render('BISSAPForumBundle:F:category-forum.html.twig', array('listTopics' => $listTopics, 'catId' => $catId, 'form' => $form->createView(), 'user' => $user));
}
I have this error : Neither the property "subForm" nor one of the methods "getSubForm()", "isSubForm()", "hasSubForm()", "__get()" exist and have public access in class "BISSAP\ForumBundle\Entity\Topic".
Sur the way is wrong, cause i think, I need to give "$topic objexct" and "$post object" when i used :
$form = $this->createForm(new TopicType(), $topic);
And I had tried, with add(collection) i get similar error!
Thanks U.
The error you get is because you don't have "subform" attribute in your topic class. That name should correspond to the name of the attribute in your topic class.
So this:
->add('subForm', new PostType())
Should be changed to
$builder->add('posts','collection', array( 'type' => new PostType()))
This would be helpful.

Fill my form with the data that I pick up from a table - Symfony2

My problem is this : I have an entity that contains user data , such as:
age
name
surname
address
phone
...
My goal is to make a form where using a query can bring all this data and fill my form with the data , so that the user can modify it if you ever change the cell for example, or changing the direction of his house.
The company and I have made the entity​​ and the form, and consulting the table will do. My problem is how to fill the form fields with the data that I get from the table.
My entity (DatosEntity):
<?php
namespace Proyecto\LavocBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Datos
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Proyecto\LavocBundle\Entity\DatosRepository")
*/
class Datos
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="empresa", type="string", length=50)
*/
private $empresa;
/**
* #var integer
*
* #ORM\Column(name="cuit", type="integer")
*/
private $cuit;
/**
* #var string
*
* #ORM\Column(name="localidad", type="string", length=50)
*/
private $localidad;
/**
* #var string
*
* #ORM\Column(name="calle", type="string", length=40)
*/
private $calle;
/**
* #var integer
*
* #ORM\Column(name="altura", type="integer")
*/
private $altura;
/**
* #var integer
*
* #ORM\Column(name="areaTel", type="integer")
*/
private $areaTel;
/**
* #var integer
*
* #ORM\Column(name="telefono", type="integer")
*/
private $telefono;
/**
* #var integer
*
* #ORM\Column(name="areaCel", type="integer")
*/
private $areaCel;
/**
* #var integer
*
* #ORM\Column(name="celular", type="integer")
*/
private $celular;
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="datos")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $personales;
/**
* #var string
*
* #ORM\Column(name="email", type="string")
*/
private $email;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set empresa
*
* #param string $empresa
* #return Datos
*/
public function setEmpresa($empresa)
{
$this->empresa = $empresa;
return $this;
}
/**
* Get empresa
*
* #return string
*/
public function getEmpresa()
{
return $this->empresa;
}
/**
* Set cuit
*
* #param integer $cuit
* #return Datos
*/
public function setCuit($cuit)
{
$this->cuit = $cuit;
return $this;
}
/**
* Get cuit
*
* #return integer
*/
public function getCuit()
{
return $this->cuit;
}
/**
* Set localidad
*
* #param string $localidad
* #return Datos
*/
public function setLocalidad($localidad)
{
$this->localidad = $localidad;
return $this;
}
/**
* Get localidad
*
* #return string
*/
public function getLocalidad()
{
return $this->localidad;
}
/**
* Set calle
*
* #param string $calle
* #return Datos
*/
public function setCalle($calle)
{
$this->calle = $calle;
return $this;
}
/**
* Get calle
*
* #return string
*/
public function getCalle()
{
return $this->calle;
}
/**
* Set altura
*
* #param integer $altura
* #return Datos
*/
public function setAltura($altura)
{
$this->altura = $altura;
return $this;
}
/**
* Get altura
*
* #return integer
*/
public function getAltura()
{
return $this->altura;
}
/**
* Set telefono
*
* #param integer $telefono
* #return Datos
*/
public function setTelefono($telefono)
{
$this->telefono = $telefono;
return $this;
}
/**
* Get telefono
*
* #return integer
*/
public function getTelefono()
{
return $this->telefono;
}
/**
* Set area
*
* #param integer $area
* #return Datos
*/
public function setAreaTel($areaTel)
{
$this->areaTel = $areaTel;
return $this;
}
/**
* Get area
*
* #return integer
*/
public function getAreaTel()
{
return $this->areaTel;
}
/**
* Set celular
*
* #param integer $celular
* #return Datos
*/
public function setCelular($celular)
{
$this->celular = $celular;
return $this;
}
/**
* Get celular
*
* #return integer
*/
public function getCelular()
{
return $this->celular;
}
/**
* Set areaCel
*
* #param integer $areaCel
* #return Datos
*/
public function setAreaCel($areaCel)
{
$this->areaCel = $areaCel;
return $this;
}
/**
* Get areaCel
*
* #return integer
*/
public function getAreaCel()
{
return $this->areaCel;
}
/**
* Set email
*
* #param integer $email
* #return Datos
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return integer
*/
public function getEmail()
{
return $this->email;
}
/**
* Set personales
*
* #param string $personales
* #return Datos
*/
public function setPersonales($personales)
{
$this->personales = $personales;
return $this;
}
/**
* Get personales
*
* #return string
*/
public function getPersonales()
{
return $this->personales;
}
}
My DatosType:
<?php
namespace Proyecto\LavocBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class DatosType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('empresa');
$builder->add('cuit');
$builder->add('localidad');
$builder->add('calle');
$builder->add('altura');
$builder->add('areaTel');
$builder->add('telefono');
$builder->add('areaCel');
$builder->add('celular');
}
public function getName()
{
return 'datos_form';
}
}
The consultation on the controller but did not know how to do it. Now , do not like taking the data I receive and dump them in the form.
Thank you very much and sorry for the trouble
I think what you want to do can be done by following these steps :
Start by collecting your User data in a variable we will call $user
Then, give this $userto be hydrated by your form, like this :
$form = $this->createForm(new UserType(), $user);
Treat your form like you would normaly do. The values found in the entity should be automaticaly filled in your form.
Hope this helps.
Your action could be something like this:
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('LavocBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$editForm = $this->createForm(new UserType(), $entity);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
);
}