Question

I am using the class table inheritance pattern with Doctrine 2.2. My DB schema is as follows:

Parent
access_id (primary key)
access_type (discriminator column)
access_role
access_acl
access_primary
user_id (foreign key)

Child 1
access_id (foreign key)
account_id (foreign key)

Child 2
access_id (foreign key)
distributor_id (foreign key)

When I try to insert a new entity into the database, the parent query looks okay, but the child query has an extra parameter. When I dumped the query being executed, this is what I saw:

INSERT INTO user_access_account (access_id, account_id) VALUES (?, ?)   
array('1'=> 39, '2'=> NULL, '3'=> 3 )

The '2' index is extraneous. '1' => 39, '2' => 3 are the correct parameters.

The code used to execute this query is as follows:

$entity = new Entity\UserAccessAccount();
$entity->setAccount($account)
       ->setUser($user)
       ->setAccessRole($accessRole)
       ->setAccessAcl($accessAcl)
       ->setAccessPrimary($accessPrimary);

$em->persist($entity);
$em->flush($entity);
Was it helpful?

Solution

As it turns out, the auto-generated entity mapping had some data that needed to be deleted. The oneToOne definition from UserAccessAccount back to UserAccess caused Doctrine to believe there was an extra parameter. It makes sense now!

Correct:

Entity\UserAccessAccount:
  type: entity
  table: user_access_account
  repositoryClass: Repository\UserAccessAccount
  oneToOne:
    account:
      targetEntity: Entity\Account
      cascade: {  }
      mappedBy: null
      inversedBy: null
      joinColumns:
        account_id:
          referencedColumnName: account_id
      orphanRemoval: false
  lifecycleCallbacks: {  }

Incorrect:

Entity\UserAccessAccount:
  type: entity
  table: user_access_account
  repositoryClass: Repository\UserAccessAccount
  oneToOne:
    account:
      targetEntity: Entity\Account
      cascade: {  }
      mappedBy: null
      inversedBy: null
      joinColumns:
        account_id:
          referencedColumnName: account_id
      orphanRemoval: false
    userAccess:
      targetEntity: Entity\UserAccess
      cascade: {  }
      mappedBy: null
      inversedBy: null
      joinColumns:
        access_id:
          referencedColumnName: user_access
  lifecycleCallbacks: {  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top