如何将Symfony2中的集合用于一对一和多对一关系?
我有一个包含联系人(联系人实体)、联系人亲属(联系人亲属实体)和各种场合(实体)的数据库。我首先要做的是将联系人的详细信息保存在联系人实体中。其次,我输入联系人的所有亲属(配偶、子女等),我希望能够输入最多4次(生日、周年纪念日等)。我一直在研究Symfony2和条令的关系/关联,寻找实现这一点的方法,我决定使用“集合”如何将Symfony2中的集合用于一对一和多对一关系?,symfony,doctrine-orm,symfony-2.3,Symfony,Doctrine Orm,Symfony 2.3,我有一个包含联系人(联系人实体)、联系人亲属(联系人亲属实体)和各种场合(实体)的数据库。我首先要做的是将联系人的详细信息保存在联系人实体中。其次,我输入联系人的所有亲属(配偶、子女等),我希望能够输入最多4次(生日、周年纪念日等)。我一直在研究Symfony2和条令的关系/关联,寻找实现这一点的方法,我决定使用“集合” namespace *******\***Bundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
对我来说,一切都很清楚,除了许多Annibarts to ContactRelights,我必须显示一组嵌入式Annibarts表单,并将它们保存回数据库
到目前为止,这些关系是:
- 1联系人可以有很多亲戚
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
要求:
- 我将能够查看所有联系人的出生日期、亲属及其姓名 安妮出生
<?php
// src/********/***Bundle/Entity/Contact.php
namespace ********\***Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="contact")
*/
class Contact
{
/**
* @ORM\OneToMany(targetEntity="********\***Bundle\Entity\AnniBirth", mappedBy="contact", cascade={"all"})
*/
protected $annibirths;
/**
* @ORM\OneToMany(targetEntity="********\***Bundle\Entity\ContactRelative", mappedBy="contact", cascade={"all"})
*/
protected $contactRelatives;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $firstname;
/**
* @ORM\Column(type="string", length=100)
*/
protected $lastname;
/**
* Constructor
*/
public function __construct()
{
$this->annibirths = new ArrayCollection();
$this->contactRelatives = new ArrayCollection();
}
public function __toString()
{
return $this->getFirstName();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
* @return Contact
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return Contact
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Add annibirths
*
* @param \********\***Bundle\Entity\AnniBirth $annibirths
* @return Contact
*/
public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
{
$this->annibirths[] = $annibirths;
return $this;
}
/**
* Remove annibirths
*
* @param \********\***Bundle\Entity\AnniBirth $annibirths
*/
public function removeAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
{
$this->annibirths->removeElement($annibirths);
}
/**
* Get annibirths
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAnnibirths()
{
return $this->annibirths;
}
/**
* Get contactRelatives
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getContactRelatives()
{
return $this->contactRelatives;
}
/**
* Add contactRelatives
*
* @param \********\***Bundle\Entity\ContactRelative $contactRelatives
* @return Contact
*/
public function addContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelatives)
{
$this->contactRelatives[] = $contactRelatives;
return $this;
}
/**
* Set contactRelatives
*
* @param \********\***Bundle\Entity\ContactRelative $contactRelative
* @return Contact
*/
public function setContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelative = null) {
$this->contactRelative = $contactRelative;
return $this;
}
/**
* Remove contactRelatives
*
* @param \********\***Bundle\Entity\ContactRelative $contactRelatives
*/
public function removeContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelatives)
{
$this->contactRelatives->removeElement($contactRelatives);
}
}
<?php
// src/********/***Bundle/Entity/AnniBirth.php
namespace ********\***Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="annibirth")
*/
class AnniBirth
{
/**
* @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\Contact", inversedBy="annibirths")
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
*/
protected $contact;
/**
* @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\ContactRelative", inversedBy="annibirths")
* @ORM\JoinColumn(name="contactRelative_id", referencedColumnName="id")
*/
protected $contactRelative;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="date")
*/
protected $celebrationDate;
/**
* @ORM\Column(type="boolean", nullable=false)
*/
protected $repeating;
/**
* @ORM\Column(type="string", length=30)
*/
protected $type;
public function __toString()
{
return $this->getType();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set celebrationDate
*
* @param \DateTime $celebrationDate
* @return AnniBirth
*/
public function setCelebrationDate($celebrationDate)
{
$this->celebrationDate = $celebrationDate;
return $this;
}
/**
* Get celebrationDate
*
* @return \DateTime
*/
public function getCelebrationDate()
{
return $this->celebrationDate;
}
/**
* Set contact
*
* @param \********\***Bundle\Entity\Contact $contact
* @return AnniBirth
*/
public function setContact(\********\***Bundle\Entity\Contact $contact = null)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* @return \********\***Bundle\Entity\Contact
*/
public function getContact()
{
return $this->contact;
}
/**
* Set repeating
*
* @param boolean $repeating
* @return AnniBirth
*/
public function setRepeating($repeating)
{
$this->repeating = $repeating;
return $this;
}
/**
* Get repeating
*
* @return boolean
*/
public function getRepeating()
{
return $this->repeating;
}
/**
* Set type
*
* @param string $type
* @return AnniBirth
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set ContactRelative
*
* @param \********\***Bundle\Entity\ContactRelative $contactRelative
* @return AnniBirth
*/
public function setContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelative = null)
{
$this->contactRelative = $contactRelative;
return $this;
}
/**
* Get contactRelative
*
* @return \********\***Bundle\Entity\ContactRelative
*/
public function getContactRelative()
{
return $this->contactRelative;
}
}
<?php
// src/********/***Bundle/Entity/ContactRelative.php
namespace ********\***Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="contactRelative")
*/
class ContactRelative
{
/**
* @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\Contact", inversedBy="contactRelatives", cascade={"all"})
* @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
*/
protected $contact;
/**
* @ORM\OneToMany(targetEntity="********\***Bundle\Entity\AnniBirth", mappedBy="contactRelative", cascade={"all"})
*/
protected $annibirths;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $firstname;
/**
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @ORM\Column(type="datetime")
*/
protected $modified_at;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $avatar;
/**
* @ORM\Column(type="string", length=1, nullable=true)
*/
protected $gender;
/**
* @ORM\Column(type="string", length=20, nullable=false)
*/
protected $relation;
/**
* Constructor
*/
public function __construct()
{
$this->annibirths = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
* @return ContactRelative
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return ContactRelative
*/
public function setCreatedAt($createdAt)
{
if(!$this->getCreatedAt())
{
$this->created_at = new \DateTime();
}
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set modified_at
*
* @param \DateTime $modifiedAt
* @return ContactRelative
*/
public function setModifiedAt($modifiedAt)
{
$this->modified_at = new \DateTime();
}
/**
* Get modified_at
*
* @return \DateTime
*/
public function getModifiedAt()
{
return $this->modified_at;
}
/**
* Set avatar
*
* @param string $avatar
* @return ContactRelative
*/
public function setAvatar($avatar)
{
$this->avatar = $avatar;
return $this;
}
/**
* Get avatar
*
* @return string
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* Set gender
*
* @param string $gender
* @return ContactRelative
*/
public function setGender($gender)
{
$this->gender = $gender;
return $this;
}
/**
* Get gender
*
* @return string
*/
public function getGender()
{
return $this->gender;
}
/**
* Set contact
*
* @param \********\***Bundle\Entity\Contact $contact
* @return ContactRelative
*/
public function setContact(\********\***Bundle\Entity\Contact $contact = null)
{
$this->contact = $contact;
return $this;
}
/**
* Get contact
*
* @return \********\***Bundle\Entity\Contact
*/
public function getContact()
{
return $this->contact;
}
/**
* Set relation
*
* @param string $relation
* @return ContactRelative
*/
public function setRelation($relation)
{
$this->relation = $relation;
return $this;
}
/**
* Get relation
*
* @return string
*/
public function getRelation()
{
return $this->relation;
}
/**
* Add annibirths
*
* @param \********\***Bundle\Entity\AnniBirth $annibirths
* @return ContactRelative
*/
public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
{
$this->annibirths[] = $annibirths;
return $this;
}
/**
* Remove annibirths
*
* @param \********\***Bundle\Entity\AnniBirth $annibirths
*/
public function removeAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
{
$this->annibirths->removeElement($annibirths);
}
/**
* Get annibirths
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getAnnibirths()
{
return $this->annibirths;
}
}
<?php
namespace ********\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class ContactRelativeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname')
->add('created_at', 'datetime', array(
'input' => 'datetime',
'widget' => 'single_text',
))
->add('modified_at', 'datetime', array(
'input' => 'datetime',
'widget' => 'single_text',
))
->add('avatar')
//->add('contact')
->add('relation', 'choice', array(
'choices' => array(
'Mother' => 'Mother',
'Father' => 'Father',
'Sister' => 'Sister',
'Brother' => 'Brother',
'Child' => 'Child',
),
'required' => true,
'empty_value' => 'Which is the relation?',
))
->add('gender', 'choice', array(
'choices' => array(
'M' => 'Male',
'F' => 'Female'
),
'required' => true,
'empty_value' => 'Choose the gender',
))
->add('annibirths', 'collection', array(
'label' => 'AnniBirths',
'type' => new AnniBirthType(),
'allow_add' => true,
'by_reference' => false,
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '********\***Bundle\Entity\ContactRelative'
));
}
public function getName()
{
return '********_***Bundle_contactrelativetype';
}
}
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
ContactRelative new.twig.html
{% extends '***********Bundle::layout.html.twig' %}
{% block body -%}
<h1>ContactRelative creation</h1>
<form action="{{ path('contactrelative_create') }}" method="post" {{ form_enctype(form) }}>
<!---------------- firstname ------------------>
<fieldset class="control-group">
<label class="control-label" for="inputInfo">Firstname</label>
<div class="controls">
{{ form_widget(form.firstname,{'id' : 'inputFirstname', 'attr': { 'class': 'span5' }}) }}
{% if form_errors(form.firstname) %}
<div class="alert alert-error fade in">
<button type="button" class="close" data-dismiss="alert">×</button>
<p>{{ form_errors(form.firstname) }}</p>
</div>
{% endif %}
</div>
</fieldset>
<!---------------- avatar ------------------>
<fieldset class="control-group">
<label class="control-label" for="inputInfo">Avatar</label>
<div class="controls">
{{ form_widget(form.avatar,{'id' : 'inputAvatar', 'attr': { 'class': 'span5' }}) }}
{% if form_errors(form.avatar) %}
<div class="alert alert-error fade in">
<button type="button" class="close" data-dismiss="alert">×</button>
<p>{{ form_errors(form.avatar) }}</p>
</div>
{% endif %}
</div>
</fieldset>
<!---------------- gender ------------------>
<fieldset class="control-group">
<label class="control-label" for="inputInfo">Gender</label>
<div class="controls">
{{ form_widget(form.gender,{'id' : 'inputGender', 'attr': { 'class': 'span5' }}) }}
{% if form_errors(form.gender) %}
<div class="alert alert-error fade in">
<button type="button" class="close" data-dismiss="alert">×</button>
<p>{{ form_errors(form.gender) }}</p>
</div>
{% endif %}
</div>
</fieldset>
<!---------------- relation ------------------>
<fieldset class="control-group">
<label class="control-label" for="inputInfo">Relation</label>
<div class="controls">
{{ form_widget(form.relation,{'id' : 'inputRelation', 'attr': { 'class': 'span5' }}) }}
{% if form_errors(form.relation) %}
<div class="alert alert-error fade in">
<button type="button" class="close" data-dismiss="alert">×</button>
<p>{{ form_errors(form.relation) }}</p>
</div>
{% endif %}
</div>
</fieldset>
{{ form_widget(form.annibirths.vars.prototype)}}
<hr />
{{ form_row(form._token) }}
<p>
<button type="submit">Create</button>
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('contactrelative') }}">
Back to the list
</a>
</li>
</ul>
{% endblock %}
namespace *******\***Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
class AnniBirthType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('celebrationDate','date', array(
'input' => 'datetime',
'widget' => 'choice',
'years' => range(1900,2013),
))
->add('repeating')
->add('type', 'choice', array(
'choices' => array(
'Anniversary' => 'Anniversary',
'Birthday' => 'Birthday',
'Other' => 'Other',
),
'required' => true,
'empty_value' => 'What is the occasion?',
))
->add('contact','entity', array(
'class' => '*******\***Bundle\Entity\Contact',
'property' => 'lastname',
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->orderby('u.lastname','ASC');
},
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => '*******\***Bundle\Entity\AnniBirth'
));
}
public function getName()
{
return '*******_***Bundle_annibirthtype';
}
}
问题:
- 联系人成功地将所有数据保存在MySQL数据库中。同样适用于
ContactRelative和Annibuth,但在保存最后两个的数据时
(ContactRelative和AnniBirth),联系人id和
contactrelative_id不会更新或保存在表中。我知道我在收集和持久化数据方面犯了错误。我试过了namespace *******\***Bundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityRepository; class AnniBirthType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('celebrationDate','date', array( 'input' => 'datetime', 'widget' => 'choice', 'years' => range(1900,2013), )) ->add('repeating') ->add('type', 'choice', array( 'choices' => array( 'Anniversary' => 'Anniversary', 'Birthday' => 'Birthday', 'Other' => 'Other', ), 'required' => true, 'empty_value' => 'What is the occasion?', )) ->add('contact','entity', array( 'class' => '*******\***Bundle\Entity\Contact', 'property' => 'lastname', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->orderby('u.lastname','ASC'); }, )) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => '*******\***Bundle\Entity\AnniBirth' )); } public function getName() { return '*******_***Bundle_annibirthtype'; } }
使用显示联系人id的步骤namespace *******\***Bundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityRepository; class AnniBirthType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('celebrationDate','date', array( 'input' => 'datetime', 'widget' => 'choice', 'years' => range(1900,2013), )) ->add('repeating') ->add('type', 'choice', array( 'choices' => array( 'Anniversary' => 'Anniversary', 'Birthday' => 'Birthday', 'Other' => 'Other', ), 'required' => true, 'empty_value' => 'What is the occasion?', )) ->add('contact','entity', array( 'class' => '*******\***Bundle\Entity\Contact', 'property' => 'lastname', 'query_builder' => function(EntityRepository $er) { return $er->createQueryBuilder('u') ->orderby('u.lastname','ASC'); }, )) ; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => '*******\***Bundle\Entity\AnniBirth' )); } public function getName() { return '*******_***Bundle_annibirthtype'; } }
\Doctrine\Common\Util\Debug::dump($contactRelative->getanniberties()->contact); 退出()但是我得到的都是空的
- 我希望能够显示出生表4次,我做到了
我不知道如何实现这一点。几天后我发现的唯一的东西
搜索,如果我想显示没有javascript的嵌入表单,
我必须像这样直接使用“原型”:
但只有一个实例 将显示窗体的名称{{ form_小部件(form.anniberties.vars.prototype)}
$contactRelative->annibirth=$ab1代码>这不应该是可能的,因为您在ContactRelative中将AnniBorth设置为受保护。这应该引发一个错误(如果您之前打开了错误)。你应该做的是$contactRelative->setannibiration($ab1)代码>。但这是为了使php对象附加,数据库中的附加实际上是在Annibiration表中,该表保存一个contactRelative\u id,因此您必须更新此对象:$ab1->setContactRelative($contactRelative)代码>部分问题在于:
public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
{
$this->annibirths[] = $annibirths;
return $this;
}
您会注意到,虽然这指向与annibirth的接触/相对,但annibirth并没有指向后面。这也许就是为什么另一个海报可能会建议做一个循环。但一个强大而简单的解决方案可以:
public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirth)
{
$this->annibirths[] = $annibirth;
$annibirth->setContact($this); // or setRelative for the Relative entity.
return $this;
}
这可能会解决你一直存在的问题。太多的代码无法确定。但绝对需要以上的改变。如果你仔细看大多数例子,你会看到同样的线条。容易被忽视
一旦一切设置正确,在坚持之前就不需要循环。找到了解决方案:
在控制器中添加:
foreach ( $contactRelative->getAnnibirths() as $anni )
{
$anni-> setContactRelative($contactRelative);
$anni-> setContact($contactRelative->getContact());
}
在细枝模板中:
{% for occasion in form.annibirths %}
<div>
{{ form_row(occasion.type) }}
{{ form_row(occasion.repeating) }}
{{ form_row(occasion.celebrationDate) }}
</div>
{% endfor %}
{%用于表单中的场合%}
{{form_row(sition.type)}
{{form_row(sition.repeating)}
{{世界其他地区(纪念日)}
{%endfor%}
您的问题缺少类型的代码。此外,它的代码太多,请将其分解为一个较小的测试用例。很难说这是一个条令问题还是一个symfony表单问题。添加了annibirhtype.phpI纠正了这一点,并注释掉了错误留下的这一行。我遵循了另一个的建议,在坚持之前添加了一个foreach循环。