0

I have two tables user and price_types with many to many relations that create the third table user_price_types.

I want to find all price_types for a specific user so I created this Doctrine query.

/**
 * Gets User's PriceTypes Query
 *
 * @param User $user
 * @return array
 */
public function getUserPriceTypes(User $user)
{
    $qb = $this->createQueryBuilder('pt')
        ->innerJoin('pt.users', 'u')
        ->where('u.id = :user_id')
        ->setParameter('user_id', $user->getId());
    $this->useResultCacheOnQuery($qb);

    return $qb->getQuery()->getResult();
}

But I'm getting this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'u2_.price_type_id' in 'on clause'

I agree with the error because there is no price_type_id column because the column name in the generated table is pricetype_id.

So I have two questions.

First, why is did Doctrine name the column pricetype_id instead of price_type_id?

Secondly, how do I update my query/entities to search column pricetype_id instead of price_type_id?

These are my entity statements for user and price_type, respectively:

   /**
     * @ORM\ManyToMany(targetEntity="ThreeWebOneEntityBundle\Entity\PriceType", mappedBy="users")
     * @ORM\JoinTable(name="user_price_types")
     */
    protected $priceTypes;



/**
     * @ORM\ManyToMany(targetEntity="ThreeWebOneEntityBundle\Entity\User", inversedBy="priceTypes")
     * @ORM\JoinTable(name="user_price_types")
     */
    protected $users;

1 Answers1

0

Adding the following statement to the annotation lets doctrine know what column name to look for.

joinColumns={@ORM\JoinColumn(name="pricetype_id", referencedColumnName="id

Final entity method is:

    /**
     * @ORM\ManyToMany(targetEntity="ThreeWebOneEntityBundle\Entity\User", inversedBy="priceTypes")
     * @ORM\JoinTable(name="user_price_types",
     * joinColumns={@ORM\JoinColumn(name="pricetype_id", referencedColumnName="id")})
     */
    protected $users;