1

I am trying to store a number of address entities within a single customer entity.

That part is easy, as its just a simple ManyToOne/OneToMany bidirectional relationship.

Take a look at our simple code, but notice the question I am posing with the additional OneToOne association I am trying to make on Customer for primary_address

class Address
{
    /**
     * @ORM\Column 
     */
    protected $address_text;

    /**
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="addresses") 
     */
    protected $customer;
}



class Customer 
{
    /**
    * @ORM\OneToMany(targetEntity="Address", mappedBy="customer")
    */
    protected $addresses;

    /**
    * @ORM\OneToOne(targetEntity="Address")
    */
    protected $primary_address;
}

So, each Customer entity should be able to have a number of Address entities associated with it, but the Customer entity should also have just one of them be a Primary Address.

How is this possible? Are there any elegant solutions?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
Chris Tickner
  • 2,008
  • 2
  • 20
  • 24
  • 1
    Doesn't it work the way you did? What you have written seems like a perfectly fine solution to me... I don't understand what you're looking for... controller code maybe? – greg0ire Dec 17 '11 at 15:25
  • 1
    True, we've done exactly the same thing in a project we're working on, and that works fine! – BenMorel Dec 17 '11 at 18:41
  • 1
    Yup I've got a similar setup here that has revisions on an entity and also a current revision. The only thing you have to do is make sure that the address is in `addresses` before making it primary address (if that makes sense to your design) which you can enforce on the entity through code but I'm sure there'd be a performance overhead to scan the collection of `addresses`; or just do it by convention i.e. make sure any calling code knows to put the address into addresses before making it primary. – Kasheen Dec 17 '11 at 19:33
  • This data model precludes two customers sharing a primary address. But I have to echo the previous comments. Your approach will work. Is there some trouble you are having? – Lighthart Feb 29 '16 at 22:40

0 Answers0