2012-03-20 73 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