0

私はZend Doctrineに取り組んでいます。groups_contactsフィールドを持ちgroup_idcontact_id関連するテーブルgroupにリンクされ、エンティティcontactで作成された多対多のエンティティがありgroupます。

多対多の関係であるエンティティを作成groups_contactsしています。group

以下は、削除アクションのコードです。

public function deleteGroupMemberAction() {
    $auth_service = $this->getServiceLocator()->get('doctrine.authenticationservice.orm_default');
    $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    $user = $auth_service->getIdentity();
    //die($_POST['g_id'] . ' removed');

    $query_deleteMember = $em->createQuery('delete from groups_contacts gc where gc.contact_id = 7 and gc.group_id = 1');

    $numDeleted = $query_deleteMember->execute();
    die($query_deleteMember. ' removed');

    $objectManager->flush();
    die($title . ' removed');
}

この関数は、完全に機能している ajax 呼び出しで呼び出されました。

削除クエリが機能しない理由がわかりません。他の方法を試しましたが、同じ結果が得られました。誰にもアイデアはありますか?

4

2 に答える 2

0

You have to specify the mapping to your entity

delete from MyMapping:groups_contacts gc where gc.contact_id = 7 and gc.group_id = 1

Or put your namespace:

delete from Your\Name\Space\groups_contacts gc where gc.contact_id = 7 and gc.group_id = 1

But that's not enough because you can not access these contact_id and group_id columns directly. So you would have to write:

delete from MyMapping:groups_contacts gc JOIN gc.contact c JOIN gc.group g where c.id = 7 and g.id = 1

Wow two joins .. this should be simpler in native SQL. Does it even execute those JOINS? Perhaps Doctrine will optimize it and not use the JOIN in the final query.

The use case is a bit weird though. Suppose you have a website which lists groups and their contacts. The relation is represented by groups_contacts (on a site note you should only make this a seperate entity when the relation needs to hold data itself). Then when the user wants to remove a relationship (disconnect a contact from a group) the relationship (represented by groups_contacts) can just be identified with it's own id. Then your query would become:

DELETE FROM MyMapping:groups_contacts gc WHERE gc.id = <user clicked relation>
于 2013-08-14T16:45:56.807 に答える