embedded to form symfony2 - forms

i work with symfony2 and when i embedded two form (person in group) have under problem:
Neither the property "PersonEntity" nor one of the methods "getPersonEntity()", "isPersonEntity()", "hasPersonEntity()", "_get()" or "_call()" exist and have public access in class "S118\ebrahimiBundle\Entity\GroupEntity".
my PersonEntity:
<?php
namespace S118\ebrahimiBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class PersonEntity
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100, nullable=false)
*/
private $f_name;
/**
* #ORM\Column(type="string", length=100, nullable=false)
*/
private $l_name;
/**
* #ORM\Column(type="string", length=100, nullable=true)
*/
private $tell;
/**
* #ORM\Column(type="string", length=100, nullable=true)
*/
private $fileName;
/**
* #ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\BuyEntity", mappedBy="PersonEntity")
*/
private $BuyEntities;
/**
* #ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\UsageEntity", mappedBy="PersonEntity")
*/
private $UsageEntities;
/**
* #ORM\ManyToOne(targetEntity="S118\ebrahimiBundle\Entity\GroupEntity", inversedBy="PersonEntities")
* #ORM\JoinColumn(name="Gid", referencedColumnName="id", nullable=false)
*/
private $GroupEntity;
/**
* Constructor
*/
public function __construct()
{
$this->BuyEntities = new \Doctrine\Common\Collections\ArrayCollection();
$this->UsageEntities = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set f_name
*
* #param string $fName
* #return PersonEntity
*/
public function setFName($fName)
{
$this->f_name = $fName;
return $this;
}
/**
* Get f_name
*
* #return string
*/
public function getFName()
{
return $this->f_name;
}
/**
* Set l_name
*
* #param string $lName
* #return PersonEntity
*/
public function setLName($lName)
{
$this->l_name = $lName;
return $this;
}
/**
* Get l_name
*
* #return string
*/
public function getLName()
{
return $this->l_name;
}
/**
* Set tell
*
* #param string $tell
* #return PersonEntity
*/
public function setTell($tell)
{
$this->tell = $tell;
return $this;
}
/**
* Get tell
*
* #return string
*/
public function getTell()
{
return $this->tell;
}
/**
* Set fileName
*
* #param string $fileName
* #return PersonEntity
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
return $this;
}
/**
* Get fileName
*
* #return string
*/
public function getFileName()
{
return $this->fileName;
}
/**
* Add BuyEntities
*
* #param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
* #return PersonEntity
*/
public function addBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
{
$this->BuyEntities[] = $buyEntities;
return $this;
}
/**
* Remove BuyEntities
*
* #param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
*/
public function removeBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
{
$this->BuyEntities->removeElement($buyEntities);
}
/**
* Get BuyEntities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuyEntities()
{
return $this->BuyEntities;
}
/**
* Add UsageEntities
*
* #param \S118\ebrahimiBundle\Entity\UsageEntity $usageEntities
* #return PersonEntity
*/
public function addUsageEntitie(\S118\ebrahimiBundle\Entity\UsageEntity $usageEntities)
{
$this->UsageEntities[] = $usageEntities;
return $this;
}
/**
* Remove UsageEntities
*
* #param \S118\ebrahimiBundle\Entity\UsageEntity $usageEntities
*/
public function removeUsageEntitie(\S118\ebrahimiBundle\Entity\UsageEntity $usageEntities)
{
$this->UsageEntities->removeElement($usageEntities);
}
/**
* Get UsageEntities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getUsageEntities()
{
return $this->UsageEntities;
}
/**
* Set GroupEntity
*
* #param \S118\ebrahimiBundle\Entity\GroupEntity $groupEntity
* #return PersonEntity
*/
public function setGroupEntity(\S118\ebrahimiBundle\Entity\GroupEntity $groupEntity)
{
$this->GroupEntity = $groupEntity;
return $this;
}
/**
* Get GroupEntity
*
* #return \S118\ebrahimiBundle\Entity\GroupEntity
*/
public function getGroupEntity()
{
return $this->GroupEntity;
}
public function __toString()
{
return $this->f_name.' '.$this->l_name;
}
}
and my group entity:
<?php
namespace S118\ebrahimiBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
*/
class GroupEntity
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\BuyEntity", mappedBy="GroupEntity")
*/
private $BuyEntities;
/**
* #ORM\OneToMany(targetEntity="S118\ebrahimiBundle\Entity\PersonEntity", mappedBy="GroupEntity")
*/
private $PersonEntities;
/**
* Constructor
*/
public function __construct()
{
$this->BuyEntities = new \Doctrine\Common\Collections\ArrayCollection();
$this->PersonEntities = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return GroupEntity
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Add BuyEntities
*
* #param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
* #return GroupEntity
*/
public function addBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
{
$this->BuyEntities[] = $buyEntities;
return $this;
}
/**
* Remove BuyEntities
*
* #param \S118\ebrahimiBundle\Entity\BuyEntity $buyEntities
*/
public function removeBuyEntitie(\S118\ebrahimiBundle\Entity\BuyEntity $buyEntities)
{
$this->BuyEntities->removeElement($buyEntities);
}
/**
* Get BuyEntities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBuyEntities()
{
return $this->BuyEntities;
}
/**
* Add PersonEntities
*
* #param \S118\ebrahimiBundle\Entity\PersonEntity $personEntities
* #return GroupEntity
*/
public function addPersonEntitie(\S118\ebrahimiBundle\Entity\PersonEntity $personEntities)
{
$this->PersonEntities[] = $personEntities;
return $this;
}
/**
* Remove PersonEntities
*
* #param \S118\ebrahimiBundle\Entity\PersonEntity $personEntities
*/
public function removePersonEntitie(\S118\ebrahimiBundle\Entity\PersonEntity $personEntities)
{
$this->PersonEntities->removeElement($personEntities);
}
/**
* Get PersonEntities
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getPersonEntities()
{
return $this->PersonEntities;
}
public function __toString()
{
return $this->name;
}
}
and my group view :
{% extends '::base.html.twig' %}
{% block body -%}
<h1>GroupEntity creation</h1>
{{ form(form) }}
<ul class="record_actions">
<li>
<a href="{{ path('group') }}">
Back to the list
</a>
</li>
</ul>
{% endblock %}
and my person form:
<?php
namespace S118\ebrahimiBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PersonEntityType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('f_name')
->add('l_name')
->add('tell')
->add('fileName')
->add('GroupEntity')
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'S118\ebrahimiBundle\Entity\PersonEntity'
));
}
/**
* #return string
*/
public function getName()
{
return 's118_ebrahimibundle_personentity';
}
}
and my group form:
<?php
namespace S118\ebrahimiBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class GroupEntityType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('PersonEntity','collection', array('type' => new PersonEntityType()))
;
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'S118\ebrahimiBundle\Entity\GroupEntity'
));
}
/**
* #return string
*/
public function getName()
{
return 's118_ebrahimibundle_groupentity';
}
}
why???

You GroupEntityType form type is looking for a 'PersonEntity' field in your GroupEntity entity. You have a oneToMany PersonEntities instead.
Try to replace PersonEntity to PersonEntities in your GroupEntityType.

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'
));
}
}

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.

How to show a collection form with the exact number of records available in a table

I am using symfony2 and doctrine.
I have a list of contacts and they are classified in a number of groups.
These groups are stored in a table.
Then I have users in my application and for each users I want to specify their rights: no/read/write.
In order to achieve this I have created a userspermission table as such :
<?php
namespace Curuba\contactsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use APY\DataGridBundle\Grid\Mapping as GRID;
/**
* userspermissions
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Curuba\contactsBundle\Entity\userspermissionsRepository")
*/
class userspermissions
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="permission", type="string", length=10)
*
* #GRID\Column(title="Permission")
*
*/
private $permission;
/**
* #ORM\ManyToOne(targetEntity="groupes", inversedBy="permissions")
* #ORM\JoinColumn(nullable=false)
*
* #GRID\Column(title="Groupes")
*/
private $groupe;
/**
* #ORM\ManyToOne(targetEntity="users", inversedBy="permissions")
* #ORM\JoinColumn(nullable=false)
*
* #GRID\Column(title="Permission")
*/
private $user;
Now in my user form, I would need to show a table with all available groups and a drop-down list in front of them.
But I dont know how to achieve that without a collection and then asking the user to add a group permission, select a group and the according permission.
Thanks in advance.
You can use an ManyToOne relation.
For example with an User and Group
<?php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="groups")
*/
class Group
{
const CLASS_NAME = __CLASS__;
/**
* #var integer
*
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id",type="integer")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="label",type="string",length=255)
*/
private $label;
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param string $label
*/
public function setLabel($label)
{
$this->label = $label;
}
/**
* #return string
*/
public function getLabel()
{
return $this->label;
}
/**
* #return string
*/
public function __toString()
{
return $this->getLabel();
}
}
and the User entity
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
*
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User
{
const CLASS_NAME = __CLASS__;
/**
* #var integer
*
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(name="id",type="integer")
*/
private $id;
/**
* #var string
*
* #ORM\Column(type="string", name="label", length=255)
*/
private $label;
/**
* #ORM\ManyToMany(targetEntity="Group")
* #ORM\JoinTable(name="user_group",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
**/
private $groups;
public function __construct()
{
$this->groups = new ArrayCollection();
}
/**
* #param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* #param string $label
*/
public function setLabel($label)
{
$this->label = $label;
}
/**
* #return string
*/
public function getLabel()
{
return $this->label;
}
/**
* #param Group $group
*
* #return bool
*/
public function hasGroup(Group $group)
{
return $this->groups->contains($group);
}
/**
* #param Group $group
*
* #return $this
*/
public function addGroup(Group $group)
{
if (false === $this->hasGroup($group)) {
$this->groups->add($group);
}
return $this;
}
/**
* #return \Doctrine\Common\Collections\ArrayCollection
*/
public function getGroups()
{
return $this->groups;
}
/**
* #return string
*/
public function __toString()
{
return $this->getLabel();
}
}
and finally create an form type like this :
class UserType extends AbstractType
{
const NAME = 'userType';
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('label', 'text');
$builder->add('groups', 'entity', array(
'class' => Group::CLASS_NAME,
'multiple' => true,
'expanded' => true,
));
}
/**
* #param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::CLASS_NAME,
));
}
/**
* Returns the name of this type.
*
* #return string The name of this type
*/
public function getName()
{
return self::NAME;
}
}
I have found how to do this. This is in fact very simple and I was probably expecting something more complex...
I have added a function in my controller (could be in my entity...) and I cal this function when I create a new user entity or when I edit one (in case a groupe has been added in the mean time.
Here is the code:
private function autocompleteForm(users $user) {
$em = $this->getDoctrine()->getManager();
$groupes = $em->getRepository('contactsBundle:groupes')->findAll();
if ($user) {
foreach ($groupes as $groupe) {
$permission = $em->getRepository('contactsBundle:userspermissions')->findOneBy(array('groupe' => $groupe, 'user' => $user));
if(!$permission) {
$permission = new userspermissions();
$permission->setGroupe($groupe);
$user->addPermission($permission);
}
}
}
return $user;
}
I am happy to explain more if someone needs...

Lifecycle Callbacks not triggering in my embedded form

It would appear my lifecycle callbacks are not triggered when the form to upload my file is embedded in another form. If the upload form is on its own the lifecycle callbacks are triggered.
I have a form that creates a 'user' entity, for this user I have a one-on-one relationship with the 'ProfilePicture' entity which has the lifecycle callbacks, I want to upload the profilepicture file on the same form. I followed the "How to handle File Uploads" cookbook, but it doesn't explain how to handle embedded forms.
UserType
<?php
namespace CashBack\AdminBundle\Form\Type;
use CashBack\DefaultBundle\Entity\ProfilePicture;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('username')
->add('password')
->add('firstname')
->add('lastname')
->add('email')
->add('gender')
->add('dateOfBirth','birthday')
->add('customrole')
->add('profilepicture', new ProfilePictureType())
->add('save', 'submit');
}
/* Identifier */
public function getName()
{
return 'User';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\User',
'cascade_validation' => true,
));
}
}
ProfilePictureType
<?php
namespace CashBack\AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ProfilePictureType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
/* additional fields that should not be saved in this object need ->add('agreeWithTerms', null,array('mapped' => false))*/
$builder
->add('id','hidden',array('mapped' => false))
->add('file','file');
}
/* Identifier */
public function getName()
{
return 'ProfilePicture';
}
/* Makes sure the form doesn't need to guess the date type */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CashBack\DefaultBundle\Entity\ProfilePicture',
));
}
}
ProfilePicture Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class ProfilePicture
{
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $path;
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 up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
private $temp;
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->path = $filename . '.' . $this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
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->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir() . '/' . $this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* #param string $path
*
* #return ProfilePicture
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* #return string
*/
public function getPath()
{
return $this->path;
}
/**
* #ORM\OneToOne(targetEntity="User", inversedBy="profilepicture")
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
*/
protected $user;
/**
* Set user
*
* #param \CashBack\DefaultBundle\Entity\User $user
*
* #return ProfilePicture
*/
public function setUser(\CashBack\DefaultBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \CashBack\DefaultBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
User Entity
<?php
namespace CashBack\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity
*/
class User
{
/**
* #var string
*
* #ORM\Column(name="FirstName", type="string", length=10, nullable=true)
*/
private $firstname;
/**
* #var string
*
* #ORM\Column(name="LastName", type="string", length=20, nullable=true)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="Gender", type="string", length=1, nullable=true)
*/
private $gender;
/**
* #var string
*
* #ORM\Column(name="Email", type="string", length=50, nullable=false)
*/
private $email;
/**
* #var \DateTime
*
* #ORM\Column(name="DateOfBirth", type="date", nullable=false)
*/
private $dateofbirth;
/**
* #var string
*
* #ORM\Column(name="Username", type="string", length=50, nullable=false)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="Password", type="string", length=100, nullable=false)
*/
private $password;
/**
* #var integer
*
* #ORM\Column(name="Id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Tag", inversedBy="user")
* #ORM\JoinTable(name="user_tag",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="tag_id", referencedColumnName="Id")
* }
* )
*/
private $tag;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Customrole", inversedBy="user")
* #ORM\JoinTable(name="user_customrole",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="customrole_id", referencedColumnName="Id")
* }
* )
*/
private $customrole;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="CashBack\DefaultBundle\Entity\Shop", inversedBy="user")
* #ORM\JoinTable(name="user_shop",
* joinColumns={
* #ORM\JoinColumn(name="user_id", referencedColumnName="Id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="shop_id", referencedColumnName="Id")
* }
* )
*/
private $shop;
/**
* Constructor
*/
public function __construct()
{
$this->tag = new \Doctrine\Common\Collections\ArrayCollection();
$this->customrole = new \Doctrine\Common\Collections\ArrayCollection();
$this->shop = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set firstname
*
* #param string $firstname
*
* #return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* #return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* #param string $lastname
*
* #return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* #return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set gender
*
* #param string $gender
*
* #return User
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* #return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set email
*
* #param string $email
*
* #return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* #return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set dateofbirth
*
* #param \DateTime $dateofbirth
*
* #return User
*/
public function setDateofbirth($dateofbirth)
{
$this->dateofbirth = $dateofbirth;
return $this;
}
/**
* Get dateofbirth
*
* #return \DateTime
*/
public function getDateofbirth()
{
return $this->dateofbirth;
}
/**
* Set username
*
* #param string $username
*
* #return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* #return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set password
*
* #param string $password
*
* #return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* #return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*
* #return User
*/
public function addTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag[] = $tag;
return $this;
}
/**
* Remove tag
*
* #param \CashBack\DefaultBundle\Entity\Tag $tag
*/
public function removeTag(\CashBack\DefaultBundle\Entity\Tag $tag)
{
$this->tag->removeElement($tag);
}
/**
* Get tag
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTag()
{
return $this->tag;
}
/**
* Add customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*
* #return User
*/
public function addCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole[] = $customrole;
return $this;
}
/**
* Remove customrole
*
* #param \CashBack\DefaultBundle\Entity\Customrole $customrole
*/
public function removeCustomrole(\CashBack\DefaultBundle\Entity\Customrole $customrole)
{
$this->customrole->removeElement($customrole);
}
/**
* Get customrole
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCustomrole()
{
return $this->customrole;
}
/**
* Add shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*
* #return User
*/
public function addShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop[] = $shop;
return $this;
}
/**
* Remove shop
*
* #param \CashBack\DefaultBundle\Entity\Shop $shop
*/
public function removeShop(\CashBack\DefaultBundle\Entity\Shop $shop)
{
$this->shop->removeElement($shop);
}
/**
* Get shop
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getShop()
{
return $this->shop;
}
/** #ORM\OneToOne(targetEntity="ProfilePicture", mappedBy="user", cascade={"persist", "all"}) */
protected $profilepicture;
/**
* Set profilepicture
*
* #param \CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture
*
* #return User
*/
public function setProfilepicture(\CashBack\DefaultBundle\Entity\ProfilePicture $profilepicture)
{
$this->profilepicture = $profilepicture;
$profilepicture->setUser($this);
return $this;
}
/**
* Get profilepicture
*
* #return \CashBack\DefaultBundle\Entity\ProfilePicture
*/
public function getProfilepicture()
{
return $this->profilepicture;
}
}
User controller
//adds a new entity from data received via Ajax, no redirect
public function addAjaxAction(Request $request)
{
$user = new User();
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);
$user = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
//prepare the response, e.g.
$response = array("code" => 100, "success" => true);
//you can return result as JSON , remember to 'use' Response!
return new Response(json_encode($response));
}
EDIT: when checking the profiler I saw that the following object is submitted in the form:
If i check the profiler I see the following object being submitted in the form: {"username":"test","password":"test","firstname":"test","lastname":"test","email":"test","gender":"t","dateOfBirth":{"month":"1","day":"1","year":"1902"},"customrole":["2"],"id":"","profilepicture":{"id":""},"_token":"YUDiZLi8dY6jtmEhZWk6ivnH3vsQIpnM_fxQ3ClJ2Gw"}
Profile picture is thus empty.