如何通过与用户的关系来保持联系人数据
我创建了实体USER 实体联系
联系人和数据是关系OneToOne
实体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;
}
}
实体联系人的代码
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;
}
}
UserType的FormType的代码
<?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';
}
}
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`
}
}
答案 0 :(得分:1)
我不会讨论您的关系的逻辑,但它看起来是双向的,您必须使用mappedBy
和inversedBy
选项定义所有者和反面。
我会直截了当地回答你的问题:
将相关对象保留在OneToOne中 关系;在这种情况下,在您的联系人类中添加cascad选项,如下所示:
/**
* @ORM\OneToOne(targetEntity="tuto\welcomeBundle\Entity\User", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $user ;