我在域和网站之间有多对一的关系。但是当我尝试添加新域时,我遇到了这个错误:
找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体 关联Eliophot \ BackBundle \ Entity \ Domain #site,但期待 Eliophot \ BackBundle \实体\网站
这是我的DomainController:
public function createDomainAction(Request $request)
{
$domain = new Domain;
if (!$domain) {
throw $this->createNotFoundException('Unable to find Domain entity.');
}
$newForm = $this->createForm(new DomainType(), $domain);
$newForm->handleRequest($request);
if ($newForm->isValid()) {
$entityManager = $this->get('doctrine')->getManager();
$entityManager->persist($domain);
$entityManager->flush();
$this->get('session')->getFlashBag()->add('success', 'Le domaine a été crée');
return $this->redirect($this->generateUrl('domain_list'));
}
return $this->render('EliophotBackBundle:Domain:new_domain.html.twig', array(
'domain' => $domain,
'form' => $newForm->createView(),
));
}
这是我的DomainType:
class DomainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('domainName','text', array(
'label' => 'Nom du domaine'
))
->add('site','entity', array(
'class' => 'EliophotBackBundle:Site',
'property' => 'name',
'label' => 'Sélectionnez un ou plusieurs site(s)',
'multiple' => true
));
}
public function getName()
{
return 'domain';
}
...
}
和我的SiteType:
class SiteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'Nom du site',
'required' => true
))
->add('nameBundle', 'text', array(
'label' => 'Nom du bundle du site',
'required' => true
))
->add('numClient', 'integer', array(
'label' => 'Numéro client du site',
'required' => true
));
}
public function getName()
{
return 'site';
}
...
}
网站实体:
/**
* Site
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Eliophot\BackBundle\Entity\SiteRepository")
*/
class Site
{
/**
* @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=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="name_bundle", type="string", length=255)
*/
private $nameBundle;
/**
* @var integer
*
* @ORM\Column(name="num_client", type="integer")
*/
private $numClient;
/**
* @ORM\OneToMany(targetEntity="Domain", mappedBy="site")
*/
protected $domains;
/**
* Initialisation
*/
public function __construct()
{
$this->domains = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Site
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set nameBundle
*
* @param $nameBundle
* @return $this
*/
public function setNameBundle($nameBundle)
{
$this->nameBundle = $nameBundle;
return $this;
}
/**
* Get nameBundle
*
* @return string
*/
public function getNameBundle()
{
return $this->nameBundle;
}
/**
* Set numClient
*
* @param integer $numClient
* @return Site
*/
public function setNumClient($numClient)
{
$this->numClient = $numClient;
return $this;
}
/**
* Get numClient
*
* @return integer
*/
public function getNumClient()
{
return $this->numClient;
}
/**
* Get domains
*
* @return ArrayCollection
*/
public function getDomains()
{
return $this->domains;
}
/**
* Set domains
*
* @param ArrayCollection $domains
*/
public function setDomains(ArrayCollection $domains)
{
$this->domains = $domains;
}
}
域实体:
/**
* Domain
*
* @ORM\Table()
* @ORM\Entity
*/
class Domain
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="domain_name", type="string", length=255)
*/
private $domainName;
/**
* @ORM\ManyToOne(targetEntity="Site", inversedBy="domains")
* @ORM\JoinColumn(name="site_id", referencedColumnName="id")
*/
protected $site;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set domainName
*
* @param string $domainName
* @return Domain
*/
public function setDomainName($domainName)
{
$this->domainName = $domainName;
return $this;
}
/**
* Get domainName
*
* @return string
*/
public function getDomainName()
{
return $this->domainName;
}
public function getSite()
{
return $this->site;
}
public function setSite($site)
{
$this->site = $site;
return $this;
}
}
就像错误说的那样,我认为Symfony需要一个Site对象而不是一个arrayCollection ...当我var_dump
添加后的请求时,我得到了这个站点字段(在这个例子中我选择了& #34;网站测试1"和#34;网站测试2"):
array(2) { [0]=> string(1) "1" [1]=> string(1) "2" }
=>这应该添加到域表2有关域的新行
那我怎么能这样做呢?
答案 0 :(得分:1)
您似乎试图以相反的方式关联对象。您的前端显示一个表单,用户可以在其中输入一个域并选择应与此域关联的站点。
从我的角度来看,您应该创建一个表单,用户在该表单中键入站点名称并选择要关联的域。反过来说。