I developed an application with Symfony2 and I have problem.
I have User Entity class, in this class I have:
/**
* @ORM\OneToOne(targetEntity="UserProperty", mappedBy="user")
*/
protected $properties;
and
/**
* Add properties
*
* @param Flashwand\UserBundle\Entity\UserProperty $property
* @return User
*/
public function addProperties(\Warski\Flashwand\UserBundle\Entity\UserProperty $property)
{
$this->properties[] = $property;
return $this;
}
/**
* Remove properties
*
* @param Flashwand\UserBundle\Entity\UserProperty $propertie
*/
public function removeProperties(\Warski\Flashwand\UserBundle\Entity\UserProperty $property)
{
$this->properties->removeElement($property);
}
/**
* Get properties
*
* @return Doctrine\Common\Collections\Collection
*/
public function getProperties()
{
return $this->properties;
}
in UserProperty class I have:
/**
* @ORM\Id
* @ORM\OneToOne(targetEntity="User", inversedBy="properties")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\Column(name="phone", type="string", length=55, nullable=true)
*/
protected $phone = null;
/**
* @ORM\Column(name="fax", type="string", length=55, nullable=true)
*/
protected $fax = null;
/**
* @ORM\Column(name="company", type="string", length=150, nullable=true)
*/
protected $company = null;
/**
* @ORM\Column(name="job_description", type="string", length=255, nullable=true)
*/
protected $job_description = null;
and now, I'm trying to biuld registration form with Username, Email, Password and Company namme from UserProperty.
When User fill fields i want to create new user and propery which gave.
My form builder looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email', array(
'label' => 'Email',
'label_attr' => array('class' => 'control-label')
));
$builder->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Password don\'t match.',
'first_options' => array(
'label' => 'Password',
'label_attr' => array('class' => 'control-label')
),
'second_options' => array(
'label' => 'Retype password',
'label_attr' => array('class' => 'control-label')
),
));
$builder->add('firstname', 'text', array(
'label' => 'Firstname',
'label_attr' => array('class' => 'control-label')
));
$builder->add('lastname', 'text', array(
'label' => 'Lastname',
'label_attr' => array('class' => 'control-label')
));
$builder->add('email', 'email', array(
'label' => 'Email',
'label_attr' => array('class' => 'control-label')
));
$builder->add('properties', 'entity', array(
'class' => 'FlashwandUserBundle:UserProperty',
'property' => 'company',
'multiple' => false,
'expanded' => false,
'label' => 'Company',
'label_attr' => array('class' => 'control-label')
));
}
I want to create field "company" like a text, not select or radio button. Can I do that, if yes, how?
just look in to Data Transformers http://symfony.com/doc/2.0/cookbook/form/data_transformers.html in this Scenario they receive content by a string (in your case the company name) but of course instead of throwing an error you can just as well create an entity.
Should you create a Custom Form Field Type
like ex:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class PropertiesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('company')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Flashwand\UserBundle\Entity\UserProperty',
));
}
public function getName()
{
return 'properties';
}
}
And change your form builder
$builder->add('properties', new PropertiesType(), array(
'label' => 'Company',
'label_attr' => array('class' => 'control-label')
));
I think you can use property_path for that. BTW I see that you mistake something with OneToOne relation. If this is OneToOne why user.properties are collection/array and User entity have addProperty/removeProperty method? Here I prepared for you some small example how I will resolve that (gist).