2012-03-20 75 views
1

我工作在窗体上有3个实体:Symfony2的:许多一对多带有自定义链接表

  • 顺序(idorder)
  • 支持参考表(idsupport)
  • 链接表(idorder,idsupport)

,当我尝试选择一个或多个支持我得到这个错误:

Catchable Fatal Error: Argument 1 passed to Myapp\MyBundle\Entity\PcastCmdsupports::setIdsupports() must be an instance of Myapp\MyBundle\Entity\PcastSupports, instance of Doctrine\Common\Collections\ArrayCollection given, 
called in C:\wamp\www\php\Symfony\vendor\symfony\src\Symfony\Component\Form\Util\PropertyPath.php on line 347 and defined in C:\wamp\www\php\Symfony\src\Myapp\MyBundle\Entity\PcastCmdsupports.php line 62 

,因为我已经创造了我看到了,我可以简单地在我的链接表中创建了2个多到一的关系在网络上我的链接表:

/** 
* @var PcastSupports 
* 
* @ORM\ManyToOne(targetEntity="PcastSupports") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="IDSUPPORTS", referencedColumnName="IDSUPPORTS") 
* }) 
*/ 
private $idsupports; 

/** 
* @var PcastOrder 
* 
* @ORM\ManyToOne(targetEntity="PcastOrder") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="IDORDER", referencedColumnName="IDORDER") 
* }) 
*/ 
private $idorder; 

和我的getter和setter方法:

/** 
* Set idsupports 
* 
*/ 
public function setIdsupports(\Myapp\MyBundle\Entity\PcastSupports $idsupports) 
{ 
    $this->idsupports = $idsupports; 
} 

/** 
* Get idsupports 
* 
*/ 
public function getIdsupports() 
{ 
    return $this->idsupports; 
} 

/** 
* Set idorder 
* 
*/ 
public function setIdcommande(\Myapp\MyBundle\Entity\PcastOrder $idorder) 
{ 
    $this->idorder = $idorder; 
} 

/** 
* Get idorder 
* 
*/ 
public function getIdorder() 
{ 
    return $this->idorder; 
} 

在我的订单我可以选择一个或多个的支持,所以我创造了我的形式是这样的:

$form_clips = $this->createFormBuilder($cmdclips) 
->add('idorder', new CmdsupportsType) 
->getForm(); 

最后我supportsType形式:

$builder 
    ->add('idsupports', 'entity', array(
    'class'   => 'MyappMyBundle:PcastSupports', 
    'property'  => 'name', 
    'expanded'  => true, 
    'multiple'  => true, 
    'query_builder' => function(EntityRepository $er) 
    { 
     return $er->createQueryBuilder('pts') 
     ->orderBy('pts.idsupports','ASC'); 
    }, 
)); 

我没有使用任何arraycollection,所以我不明白这个问题。此问题发生在此行动中:

$form_clips->bindRequest($request); 

非常感谢您的帮助!


我试图使它在一个简单的情况下,许多一对多的关系(用户,公司和user_company实体)的工作,但我有一个问题,当我尝试将公司添加到用户:

Warning: oci_bind_by_name() [<a href='function.oci-bind-by-name'>function.oci-bind-by-name</a>]: Invalid variable used for bind in C:\wamp\www\php\Promocast\Symfony\vendor\doctrine-dbal\lib\Doctrine\DBAL\Driver\OCI8\OCI8Statement.php line 113

我google搜索了很多,但我没有找到关于此错误的东西......据堆栈跟踪误差时学说尝试添加该公司目标:

array('column' => ':param10', 'variable' => object(PcastCompany), 'type' => '1')

我的用户实体(兴业=公司):

/** 
* @ORM\ManyToMany(targetEntity="PcastSociete", inversedBy="users") 
* @ORM\JoinTable(name="PcastLienusersociete", 
* joinColumns={@ORM\JoinColumn(name="ImUser_iduser", referencedColumnName="iduser")}, 
* inverseJoinColumns={@ORM\JoinColumn(name="PcastLienusersociete_idsociete", referencedColumnName="idsociete")} 
*) 
*/ 
private $societes; 

public function getSocietes() 
{ 
    return $this->societes; 
} 

public function addSociete(\Myapp\MyBundle\Entity\PcastSociete $societe) 
{ 
    $this->societes[] = $societe; 
} 

我公司实体:

/** 
* @ORM\ManyToMany(targetEntity="ImUser", mappedBy="societes") 
*/ 
private $users; 

public function __construct() { 
    $this->users = new \Doctrine\Common\Collections\ArrayCollection(); 
} 

如果任何人有任何想法...

感谢

+0

这可能有助于:http://www.prowebdev.us/2012/07/symfnoy2-many-to-many-relation-with.html – PMoubed 2013-03-11 00:33:54

回答

2

你不应该有一个代表链接表的实体。如果您正确注释了两个实体,则Doctrine将自行处理链接表的创建。

此外,您不需要任何链接表来首先执行多对一关系,您想要做的就是在两个实体中使用多对多注释。

http://readthedocs.org/docs/doctrine-orm/en/latest/reference/association-mapping.html?highlight=many%20to%20one#many-to-many-bidirectional

+0

好吧,我会尝试,只是一个问题,教条如何创建这个链接表? (在数据库中或像一个实体?) 感谢您的回答! – Snroki 2012-03-20 15:58:13

+0

在多对多注释中,您必须提供要用作链接表的表的名称。如果它已经存在,它就会使用它,如果它不存在,它只会在数据库中创建表并从此使用它。 – pcdl 2012-03-20 16:03:28

-1

从基础开始。我很好奇关于ManyToMany的其他内容,所以我抓住你的实体作为测试用例。在深入的形式和之前这样,请确保您可以在命令行中,如执行一个简单的测试案例:

use Zayso\ArbiterBundle\Entity\PcastSociete as Company; 
use Zayso\ArbiterBundle\Entity\ImUser  as User; 

protected function test1() 
{ 
    $em = $this->getContainer()->get('doctrine.orm.entity_manager'); 
    $company = new Company(); 
    $em->persist($company); 

    $user = new User(); 
    $user->addSociete($company); 
    $em->persist($user); 

    $em->flush(); 

} 

为我所用的实体:

namespace Zayso\ArbiterBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
*/ 
class ImUser 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer",name="iduser") 
* @ORM\GeneratedValue 
*/ 
protected $id; 

public function getId() { return $this->id; } 

/** 
* @ORM\ManyToMany(targetEntity="PcastSociete", inversedBy="users") 
* @ORM\JoinTable(name="PcastLienusersociete", 
* joinColumns={@ORM\JoinColumn(name="ImUser_iduser", referencedColumnName="iduser")}, 
* inverseJoinColumns={@ORM\JoinColumn(name="PcastLienusersociete_idsociete", referencedColumnName="idsociete")} 
*) 
*/ 
private $societes; 

public function getSocietes() 
{ 
    return $this->societes; 
} 

public function addSociete(PcastSociete $societe) 
{ 
    $this->societes[] = $societe; 
} 
public function __construct() 
{ 
    $this->societes = new ArrayCollection(); 
} 

} 

namespace Zayso\ArbiterBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 

/** 
* @ORM\Entity 
*/ 
class PcastSociete 
{ 
/** 
* @ORM\Id 
* @ORM\Column(type="integer", name="idsociete") 
* @ORM\GeneratedValue 
*/ 
protected $id; 

public function getId() { return $this->id; } 

/** 
* @ORM\ManyToMany(targetEntity="ImUser", mappedBy="societes") 
*/ 
private $users; 

public function __construct() 
{ 
    $this->users = new ArrayCollection(); 
} 
} 

得到上面的工作,然后我们可以移动解决表格问题。

+0

感谢您的回答,我没有说出来,但我的测试实际上和您的测试一样。我只是使用表单来填充我的用户实体,然后手动添加一个公司:'$ test_societe = $ em-> getRepository('MyappMyBundle:PcastSociete') - > find(49); $ newUser-> addSociete($ test_societe);'然后只是坚持用户,所以这就是为什么我不明白symfony错误 – Snroki 2012-03-21 15:27:58

+0

不知道除了可能使用MySQL数据库进行测试什么建议。 PcastCompany(来自您的错误)有点可疑。本来期望像PcastSociete这样的东西。并设置一个小测试环境,以便您可以从命令行运行,将为您节省大量的工作。 – Cerad 2012-03-21 16:00:33

+0

好吧我试着用另一个例子,事情是我得到这个错误('警告:oci_bind_by_name()')每次我尝试坚持一个实体与对象在一个setter中。就像教条不明白,他必须在坚持时只拿这个东西的身份证。 – Snroki 2012-03-22 09:15:22