Specifying Index with Symfony2 for Doctrine PersistentCollection

Can anyone tell me how I can configure Symfony2 / Doctrine to index objects into a PersistentCollection, from a specified column in an entity?

For example, suppose we have two tables; categories and products, with many products falling into the same category. When I use a repository to load a category and then call getProducts()

to load products, I want them to be returned indexed by the product id or whatever column I specify. They are currently only indexed from scratch.

Is there anyway to do this, or do I have to scroll through and manually set the keys myself?

+3


source to share


1 answer


Mapping associations OneToMany

allows you to define a property indexBy

. With annotation mapping , it would look like this

class Category
{
    // ...

    /**
     * @OneToMany(targetEntity="Products", indexBy="productId", mappedBy="product")
     */
    protected $products;

    // ...
}

      



XML , YAML and PHP mappings allow this too, and it works with ManyToMany

.

+1


source







All Articles