doctrine2: many-to-one with non default referencedColumnName does not persist entity

Posted by timaschew on Stack Overflow See other posts from Stack Overflow or by timaschew
Published on 2012-09-21T20:29:08Z Indexed on 2012/09/23 15:37 UTC
Read the original article Hit count: 336

Filed under:
|
|

I'm using symfony 2.1.2 with FOSUserBundle.

I extend the User from FOS and define a many-to-one (bidirectional) association to a Customer entity. I don't want to use primary key for the association (referencedColumnName). I will use another integer uniqe column: customer_no

use FOS\UserBundle\Entity\User as BaseUser;


/**
 * @ORM\Entity
 * @ORM\Table(name="t_myuser")
 */
class MyUser extends BaseUser
{
    /**
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="user")
     * @ORM\JoinColumn(name="customer_no", referencedColumnName="customer_no", nullable=false)
     */
    $public $customer;

}

/**
 * @ORM\Entity
 * @ORM\Table(name="t_customer")
 */
class Customer extends BaseEntity // provides an id (pk)
{
    /**
     * @ORM\Column(type="integer", unique=true, nullable=false)
     */
    public $customer_no;

    /**
     * @ORM\OneToMany(targetEntity="MyUser", mappedBy="customer")
     */
    public $user;
}

When I try to persist (via a form) a new MyUser entity with an (already in db existing and) loaded Customer entity from db, I get this error:

Notice: Undefined index: customer_no in ...\vendor\doctrine\orm\lib\Doctrine\ORM\Persisters\BasicEntityPersister.php line 608

The schema on the db is all right.

//update: I fix the inversedBy and mappedBy stuff, but this is not the problem.

© Stack Overflow or respective owner

Related posts about symfony2

Related posts about doctrine2