I've overridden successfully FOSUserbundle's User class adding a bunch of properties. But When I tried to add 'types' property as you can see that below, I'm having the following exception:
Neither the property "types" nor one of the methods "addTyp()"/"removeTyp()", "addType()"/"removeType()", "setTypes()", "types()", "__set()" or "__call()" exist and have public access in class "Advertproject\UserBundle\Entity\User".
Type is a separate entity with a ManyToMany relationship with my custumized Entity/User.
The idea is to insert a radio button form inside the main form. I've read many of the answers provided here for similar error message, but nothing works. Actually my case is similar to this (answer provided by @Mick then). But the advices provided don't work in my case because I did exactly what was recommended there. Can anyone help me out with this? Note that the form is being rendered properly with the radio button form, and the exception is being returned after I hit submit button.
Advertproject/UserBundle/Entity/User:
namespace Advertproject\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* @ORM\Table(name="fos_user")
*@ORM\Entity(repositoryClass="Advertproject\UserBundle\Entity\UserRepository")
*/ class User extends BaseUser { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter the company name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=255,
* minMessage="The company name is too short.",
* maxMessage="The company name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $companyName;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your phone.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=8,
* max=255,
* minMessage="The phone value is too short.",
* maxMessage="The phone value is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please add details about the person we can contact.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=30,
* max=255,
* minMessage="The details info is too short.",
* maxMessage="The details info is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $details;
/**
* @var
* @ORM\ManyToMany(targetEntity="Advertproject\UserBundle\Entity\Type", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
protected $types;
/**
* @var \DateTime
*
* @ORM\Column(name="date", type="datetime")
*/
private $date;
/**
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
public function __construct()
{
parent::__construct();
$this->date = new \Datetime();
$this->types = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set companyName
*
* @param string $companyName
* @return User
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
return $this;
}
/**
* Get companyName
*
* @return string
*/
public function getCompanyName()
{
return $this->companyName;
}
/**
* Set phone
*
* @param string $phone
* @return User
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set details
*
* @param string $details
* @return User
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* Get details
*
* @return string
*/
public function getDetails()
{
return $this->details;
}
public function setEmail($email)
{
$email = is_null($email) ? '' : $email;
parent::setEmail($email);
$this->setUsername($email);
return $this;
}
public function setEmailCanonical($emailCanonical)
{
$this->emailCanonical = $emailCanonical;
$this->usernameCanonical = $emailCanonical;
}
/**
* Get types
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTypes()
{
return $this->types;
}
/**
* Set date
*
* @param \DateTime $date
* @return User
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return User
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Add Type
*
* @param \Advertproject\UserBundle\Entity\Type $type
* @return User
*/
public function addType(Type $type)
{
$this->types[] = $type;
return $this;
}
/**
* Remove types
*
* @param \Advertproject\UserBundle\Entity\Type $types
*/
public function removeType(Type $types)
{
$this->types->removeElement($types);
}
}
Advertproject/UserBundle/Form/Type/RegistrationFormType:
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', 'text')
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('phone', 'text')
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
->add('details', 'textarea')
->add('types', 'entity', array(
'class' => 'APUserBundle:Type',
'property' => 'name',
'required' => true,
'expanded' => true,
'multiple' => false,
))
;
}
Advertproject/UserBundle/Entity/Type:
namespace Advertproject\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Type
*
* @ORM\Table(name="type")
* @ORM\Entity(repositoryClass="Advertproject\UserBundle\Entity\TypeRepository")
*/
class Type
{
/**
* @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)
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Type
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
}