0

I'm trying to bind two attributes to the same column in Doctrine.

My goal is to be able to populate and find an integer column from an integer value $entity->setSomeId(3), but also be able to do it using the referenced object $entity->setSome($some) (where $some is an object)

I have tried to define my entity as such :

/** * @var Some * * @ORM\OneToOne(targetEntity="Some") * @ORM\joinColumn(name="id", referencedColumnName="other_id") */ private $someId; /** * @var integer * * @ORM\Column(name="id", type="integer", length=11, precision=0, scale=0, nullable=false, unique=true) */ private $something; 

It works to retrieve data, but not when I try to create an entity.

I create an entity that way :

$entity = new Entity(); $entity->setSomeId(3) $entity->getSomeId(); // = 3 $em->persist($entity); $em->flush(); 

Gives me the following error :

`Integrity constraint: Column id cannot be null` 

I guess it's because $something overrides the value of $someId somewhere.

Is it possible to do what I want to do with Doctrine ? Or should I use the referenced object only, and forget about assigning integers directly to this column ?

2
  • Instead of explaining how you would like to implement the relationship by assigning an "integer to $someId, describe the overall OneToOne relationship between the entities that you would like to create. I can better assist you in that way because I think you are trying to circumvent Doctrine when it already handles relationships quite well. Also, it is important to recognize that Doctrine will be handling your keys in almost every entity. I have yet to discover a need to circumvent this. Commented Jun 30, 2017 at 17:07
  • You should use the "referenced" entity see answer. Commented Jun 30, 2017 at 17:20

1 Answer 1

1

You are probably wanting a One-To-One, Bidirectional relationship:

Code snippet below is from Doctrine documentation located at Association Mapping:

<?php /** @Entity */ class Customer { // ... /** * One Customer has One Cart. * @OneToOne(targetEntity="Cart", mappedBy="customer") */ private $cart; public function getCart() { return $this->cart; } // ... } /** @Entity */ class Cart { // ... /** * One Cart has One Customer. * @OneToOne(targetEntity="Customer", inversedBy="cart") * @JoinColumn(name="customer_id", referencedColumnName="id") */ private $customer; public function getCustomer() { return $this->customer; } // ... } 

Doctrine will manage your "ids" for you since it manages the relationship, all you need to do is interact with $cart or $customer in the example above. You would create an accessor for each property.

Sign up to request clarification or add additional context in comments.

3 Comments

Will give it a try. I see that you don't mention * @var Cart $cart while I do, could it be the reason Doctrine refused to let me set my Id as in integer and forced me to give an object ?
Doctrine expects you to work with the related entity and not set the "Id". Let Doctrine handle the keys for you because it manages the object relational mapping (so you don't have to). For example, in some Many-To-Many relationships Doctrine will actually create a "join" table that contains keys for both related tables. That is just one example how Doctrine helps you work smarter. You are just becoming familiar with Doctrine and it is more abstracted layer of software. Update the object Doctrine "gives you" and then persist() and flush(). The "Id" is handled for you.
Found out what my problem was. I had the relationships principles in Doctrine right if fact, but my problem was that I was doing a ->find to retrieve the referenced entity, instead of a ->getReference and that sounded terrible in terms of number of queries to me. I'll keep using entity relationships with references and that's will be fine. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.