简体   繁体   English

Symfony 2.2 Doctrine2多对多关系插入查询

[英]Symfony 2.2 Doctrine2 many-to-many relation insert query

I'm using Doctrine2 in Symfony 2.2. 我在Symfony 2.2中使用Doctrine2。 I have the following entities: 我有以下实体:

  1. "User" “用户”
  2. "OrganizationSupplier" “组织供应商”

and m:n relation between those entities. 以及这些实体之间的m:n关系。 Here is how it is in schema: 这是架构中的样子:

class User {
.....
    /**
     * @ORM\ManyToMany(targetEntity="OrganizationSupplier", inversedBy="users")
     * @ORM\JoinTable(name="User_Supplier",
     *      inverseJoinColumns={@ORM\JoinColumn(name="organization_supplier_id", referencedColumnName="id")},
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
     *      )
     */
    private $allowed_suppliers;
.....
}

and

class OrganizationSupplier
{
...
    /**
     * @ORM\ManyToMany(targetEntity="User", mappedBy="allowed_suppliers")
     */
    private $users;
...
}

Here is m:n table created by Doctrine: 这是由Doctrine创建的m:n表:

CREATE TABLE `User_Supplier` (
  `user_id` int(11) NOT NULL,
  `organization_supplier_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`organization_supplier_id`),
  KEY `IDX_31AFA1DDA76ED395` (`user_id`),
  KEY `IDX_31AFA1DD1A81C9F7` (`organization_supplier_id`),
  CONSTRAINT `FK_31AFA1DD1A81C9F7` FOREIGN KEY (`organization_supplier_id`) REFERENCES `organizationsupplier` (`id`),
  CONSTRAINT `FK_31AFA1DDA76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Here is the data for user id=638 from this m:n table: 以下是此m:n表中用户id = 638的数据:

user_id organization_supplier_id
638 618
638 620
638 622
638 624
638 626
638 628
638 630
638 632
638 634
638 636

Everything is fine up to this point. 到目前为止,一切都很好。

Now the problem: I have a form in admin that modifies this m:n table by adding/removing records from it. 现在的问题是:我在admin中有一个表单,可以通过在其中添加/删除记录来修改此m:n表。 From business logic perspective I need to assign/unassign suppliers to certain users from the list of suppliers assigned to organization (OrgaizationSupplier entity - see above) 从业务逻辑的角度来看,我需要从分配给组织的供应商列表中为某些用户分配/取消分配供应商(OrgaizationSupplier实体-参见上文)

Here is the code for this field of the form: 这是表单中此字段的代码:

->add('allowed_suppliers', 'entity', array('class'=>'STMainBundle:OrganizationSupplier', 'multiple'=>true, 'expanded'=>true,
        'query_builder' => function(EntityRepository $er) use ($user) {
            return $er->createQueryBuilder('os')
                ->where('os.organization = :organization')
                ->setParameter(':organization', $user->getOrganization());

If I submit it with a checkbox checked, I'm getting an SQL exception, it tries to launch a query with swapped columns: 如果我在选中复选框的情况下提交它,那么我将收到一个SQL异常,它将尝试启动具有交换列的查询:

INSERT INTO User_Supplier (organization_supplier_id, user_id) VALUES (?, ?)' with params [638, 618]: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`sollod_new`.`user_supplier`, CONSTRAINT `FK_31AFA1DDA76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))".

so it should be vice versa, [618, 638] and not as it tries [638,618]. 因此应该反之亦然[618,638],而不是尝试[638,618]。

I'm a newbie in symfony, so I'm trying to find the place where it builds this query on postack, to control this parameters order. 我是symfony的新手,因此我试图找到在postack上构建此查询的位置,以控制此参数顺序。

Does anyone know where I can find it? 有谁知道我在哪里可以找到它?

UPDATE: Here is ladybug dump of Request, User entity and Form. 更新: 是请求,用户实体和表单的瓢虫转储。 This is done right after $form->bind($request); 这是在$ form-> bind($ request);之后完成的。

Thanks a lot 非常感谢

UPDATE: My bad, I forgot that schema is cached as well, the schema that I gave above was already the correct and fixed one: 更新:我不好,我也忘记了架构也被缓存,我上面给出的架构已经是正确和固定的:

 *      inverseJoinColumns={@ORM\JoinColumn(name="organization_supplier_id", referencedColumnName="id")},
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}

Initially these columns were swapped. 最初,这些列已交换。 I only had to clear dev/prod cache to make it work. 我只需要清除dev / prod缓存即可使其工作。 Sorry about that 对于那个很抱歉

It was a matter of cache. 这是一个缓存问题。 Initially the data model was wrong, I fixed it to be: 最初,数据模型是错误的,我将其修复为:

     *      inverseJoinColumns={@ORM\JoinColumn(name="organization_supplier_id", referencedColumnName="id")},
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}

and forgot to clear cache 忘了清除缓存

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

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