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

时间:2012-03-20 15:42:12

标签: php forms symfony doctrine-orm

我正在处理一个包含3个实体的表单:

  • order(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;

和我的主持人和吸气者:

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

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

$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搜索了很多,但我没有发现任何关于此错误...根据堆栈跟踪错误是当doctrine尝试添加公司对象时:

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

我的用户实体(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;
}

我的公司实体:

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

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

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

由于

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:-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();
}
}

完成上述工作,然后我们可以继续处理表单问题。