简体   繁体   English

Symfony2:具有自定义链接表的多对多

[英]Symfony2 : Many-To-Many with a custom link table

I'm working on a form with 3 entities : 我正在处理具有3个实体的表单:

  • order (idorder) 订单(idorder)
  • support reference table (idsupport) 支持参考表(idsupport)
  • link table (idorder, idsupport) 链接表(idorder,idsupport)

And when i try to select one or more support i got this error: 当我尝试选择一个或多个支持时,出现此错误:

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 

Since i already created my link table i saw on the web that i can simply create 2 Many-To-One relation in my link table : 由于我已经创建了链接表,因此我在网络上看到可以在链接表中简单地创建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;

and my setters and getters : 和我的二传手:

/**
 * 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;
}

In my order form i can choose one or many supports so i created my form like this: 在我的订单表格中,我可以选择一个或多个支持,因此我创建了这样的表格:

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

And finally my supportsType form: 最后是我的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');
    },
));

I'm not using any arraycollection so i don't understand the issue. 我没有使用任何arraycollection,所以我不明白这个问题。 And the issue happened during this action: 问题是在此操作期间发生的:

$form_clips->bindRequest($request);

Thank a lot for your help ! 非常感谢您的帮助!


I tried to make it work with the many-to-many relation in a simple case (user, company and a user_company entities) but i got a problem when i try to add a company to a user: 我试图在简单的情况下(用户,公司和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

I googling a lot but i didn't find anything on this error... According to stack trace the error is when doctrine try to add the company object : 我在谷歌上搜索了很多,但是没有发现任何关于此错误的信息...根据堆栈跟踪,错误是当学说尝试添加公司对象时:

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

My user entity (societe = company): 我的用户实体(社会=公司):

/**
 * @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;
}

My company entity: 我的公司实体:

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

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

If anybody have any idea... 如果有人有什么想法...

Thanks 谢谢

You should not have an entity representing the link table. 您不应具有代表链接表的实体。 If you annotate both your entities correctly, Doctrine will handle the creation of the link table by itself. 如果您正确地注释了两个实体,Doctrine将自行处理链接表的创建。

Moreover, you do not need any link table to do a Many-to-One relationship in the first place, what you want to do is use the Many-to-Many annotations in both entities. 此外,首先不需要任何链接表即可进行多对一关系,您要做的是在两个实体中使用多对多注释。

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

Start with the basics. 从基础开始。 I was curious about something else concerning ManyToMany so I grabbed your entities as a test case. 我对与ManyToMany有关的其他事情感到好奇,因此我将您的实体作为测试用例。 Before diving into forms and such, make sure you can execute a simple test case from the command line such as: 在深入了解表格之类的内容之前,请确保您可以从命令行执行一个简单的测试用例,例如:

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();

}

For entities I used: 对于我使用的实体:

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();
}
}

Get the above working then we can move on to the forms problem. 完成上述工作后,我们可以继续进行表格问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM