我对Doctrine2和两个关系实体有问题。
有一个用户实体可以(不一定)拥有一个或一组社交实体,其中包含一个社交网络链接。
我不控制学说,我仍在学习关系。
我想添加/不添加社交网络链接的用户。
经过多次研究和测试,我仍无法找到解决方案。
这是我的用户实体
<?php
//...
/**
* User
*
* @ORM\Table(name="admin_users")
* @ORM\Entity(repositoryClass="UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=25, unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=40)
*/
private $password;
/**
*
* @var string
*
* @Assert\NotBlank
*/
private $plainPassword;
//...
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Social", mappedBy="user", cascade={"persist","remove"})
* @ORM\JoinColumn(nullable=true)
*/
private $socials;
public function __construct()
{
$this->socials = new ArrayCollection();
}
//Some getters setters
/**
* Add socials
*
* @param Social $socials
* @return User
*/
public function addSocials(Social $socials)
{
$this->socials[] = $socials;
$socials->setUser($this);
return $this;
}
/**
* Remove socials
*
* @param Social $socials
*/
public function removeSocials(Social $socials)
{
$this->socials->removeElement($socials);
}
/**
* Get socials
*
* @return Collection
*/
public function getSocials()
{
return $this->socials;
}
}
这是社交实体
<?php
/**
* Social
*
* @ORM\Table(name="admin_social")
* @ORM\Entity(repositoryClass="SocialRepository")
*/
class Social
{
/**
* @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=20, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255, nullable=true)
*/
private $url;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="socials", cascade={"persist","remove"})
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
*/
private $user;
//getters setters
/**
* Set user
*
* @param User $user
* @return Social
*/
public function setUser(User $user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return User
*/
public function getUser()
{
return $this->user;
}
}
userType代码如下所示:
$builder
->add('username', 'text', array(
'attr'=> array('class' => 'span6',),
'label_attr' => array('class' => 'control-label'),
)
)
// ....
->add('sociaux', 'collection', array('type' => new SocialType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,))
;
最后是控制器代码:
public function addAction()
{
$user = new User;
// Create the form
$form = $this->createForm( new UserType, $user );
// Gets the request
$request = $this->getRequest();
// Checks if the request have type POST
if ( $request->getMethod() == 'POST' ) {
// Links the request and the form
$form->bind( $request );
// Checks if all input values are correct
if ( $form->isValid() ) {
// Save user object in database
$em = $this->getDoctrine()->getManager();
// Persist entity user
$em->persist( $user );
$em->flush();
//...
}
}
//...
}
当我尝试添加没有社交实体的用户时,我没有错误,但在数据库中,我在社交表中有一个空值的行。请帮忙。
更新
在用户实体中我添加了这个:
if( !( $socials->getName() === null && $socials->getUrl() === null ) )
{
$this->socials[] = $socials;
$socials->setUser($this);
}
现在social
表中没有插入行,但是当我尝试编辑用户时,我有两个收集字段(重复)。
请参阅screenshot
这是我的模板文件(Twig):
<div class="widget-body">
{{ form_start(form, { 'action': path('acme_admin_edit_user', {'id': userId}), 'attr': {'class': 'form-horizontal'} }) }}
<div class="control-group">
{{ form_errors(form.username) }}
{{ form_label(form.username) }}
<div class="controls">
{{ form_widget(form.username) }}
</div>
</div>
<!-- ... -->
<div id="acme_adminbundle_useredittype_socials" data-prototype="{{ form_row(form.socials.vars.prototype) | escape }}">
{% for social in form.socials %}
<div>
<label class="required text-primary lead">Lien n°{{ loop.index }}</label>
<div id="acme_adminbundle_useredittype_socials_{{ loop.index0 }}">
<div class="control-group">
{{ form_errors(social.name) }}
{{ form_label(social.name) }}
<div class="controls">
{{ form_widget(social.name) }}
</div>
</div>
<div class="control-group">
{{ form_errors(social.url) }}
{{ form_label(social.url) }}
<div class="controls">
{{ form_widget(social.url) }}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="txt-center well">
<input type="submit" class="auto-margin btn btn-primary btn-large" />
</div>
{{ form_end(form) }}
</div>
答案 0 :(得分:0)
尝试删除:
@ORM\JoinColumn(nullable=true)
来自User
课程的。 @JoinColumn
只应在关系的一方定义,因为Social
实体包含name
和referencedColumnName
,User
内部不需要{{1}}。