Symfony2 how to persist entity with oneToOne relationship

How can i persist contact data with the relationship with User

I created the entity USER the Entity Contact

Contact and data are on relationship OneToOne

the code of the Entity USER

<?php

namespace tuto\welcomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;



/**
 * User
 *
 * @ORM\Table("user")
 * @ORM\Entity(repositoryClass="tuto\welcomeBundle\Entity\UserRepository")
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @ORM\OneToOne(targetEntity="tuto\welcomeBundle\Entity\Contact")
     * @ORM\JoinColumn(nullable=false)
     */
    private $contact;

    /**
     * @var string
     *
     * @ORM\Column(name="login", type="string", length=255)
     */
    private $login;

    /**
     * @var string
     *
     * @ORM\Column(name="pass", type="string", length=255)
     */
    private $pass;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set login
     *
     * @param string $login
     * @return User
     */
    public function setLogin($login)
    {
        $this->login = $login;

        return $this;
    }

    /**
     * Get login
     *
     * @return string 
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * Set pass
     *
     * @param string $pass
     * @return User
     */
    public function setPass($pass)
    {
        $this->pass = $pass;

        return $this;
    }

    /**
     * Get pass
     *
     * @return string 
     */
    public function getPass()
    {
        return $this->pass;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->contact = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add contact
     *
     * @param \tuto\welcomeBundle\Entity\Contact $contact
     * @return User
     */
    public function addContact(\tuto\welcomeBundle\Entity\Contact $contact)
    {
        $this->contact[] = $contact;

        return $this;
    }

    /**
     * Remove contact
     *
     * @param \tuto\welcomeBundle\Entity\Contact $contact
     */
    public function removeContact(\tuto\welcomeBundle\Entity\Contact $contact)
    {
        $this->contact->removeElement($contact);
    }

    /**
     * Get contact
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getContact()
    {
        return $this->contact;
    }


}

the code of the Entity Contact

namespace tuto\welcomeBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Contact
 *
 * @ORM\Table("contact")
 * @ORM\Entity(repositoryClass="tuto\welcomeBundle\Entity\ContactRepository")
 */
class Contact
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\oneToOne(targetEntity="tuto\welcomeBundle\Entity\User")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user ;
    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    private $email;

    /**
     * @var string
     * @ORM\Column(name="sujet", type="string", length=255)
     */
    private $sujet;

    /**
     * @var string
     *
     * @ORM\Column(name="texte", type="string", length=255)
     */
    private $texte;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Contact
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set sujet
     *
     * @param string $sujet
     * @return Contact
     */
    public function setSujet($sujet)
    {
        $this->sujet = $sujet;

        return $this;
    }

    /**
     * Get sujet
     *
     * @return string 
     */
    public function getSujet()
    {
        return $this->sujet;
    }

    /**
     * Set texte
     *
     * @param string $texte
     * @return Contact
     */
    public function setTexte($texte)
    {
        $this->texte = $texte;

        return $this;
    }

    /**
     * Get texte
     *
     * @return string 
     */
    public function getTexte()
    {
        return $this->texte;
    }



    /**
     * Set user
     *
     * @param \tuto\welcomeBundle\Entity\User $user
     * @return Contact
     */
    public function setUser(\tuto\welcomeBundle\Entity\User $user)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \tuto\welcomeBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }
}

the code of the FormType of UserType

<?php

namespace tuto\welcomeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('login','text',array('attr'=>array('class'=>'form-control'),
                'label_attr'=>array('class'=>'test')))
            ->add('pass','text',array('attr'=>array('class'=>'form-control'),
                'label_attr'=>array('class'=>'test')))
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'tuto\welcomeBundle\Entity\User'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'tuto_welcomebundle_user';
    }
}

the code of the Form UserType

<?php

namespace tuto\welcomeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ContactType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('email','text')
            ->add('sujet','text')
            ->add('texte','textarea')
            ->add('user', new UserType())
            ->add('envoyer','submit',array('attr' => array('class'=>'btn btn-primary')));
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'tuto\welcomeBundle\Entity\Contact'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'tuto_welcomebundle_contact';`enter code here`
    }
}

Answers


I won't discuss the logic of your relation, but It looks bidirectional and you have to define the owner and inverse sides using the mappedBy and inversedBy options.

I will answer your question straightforward:

to persist the related object in a OneToOne relation; user in this case, in your contact class add the cascad option like this:

/**
 * @ORM\OneToOne(targetEntity="tuto\welcomeBundle\Entity\User", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
 private $user ; 

Need Your Help

How to concatenate in SQL Server

sql sql-server tsql concatenation

My database doesn't have a specific column so I created a column in my query by switch. What I need is to concatenate this column with another column in the database:

PHP Fatal Error Call to Undefined. Stop it breaking page?

php wordpress

Please forgive me if this is a duplicate question. I have searched but cant come up with an answer.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.