DDC-1180: Indexed Associations: foreign key (association) cannot be used as indexBy field #1784

Closed
doctrinebot opened this Issue May 29, 2011 · 5 comments

2 participants

@doctrinebot

Jira issue originally created by user sobotka:

I am trying to index a collection by its entity's column which is also a foreign key (association). It seems to me that it is not possible at the moment.

For example:

/****
 * @Entity
 */
class Hotel
{

    // $id column and other stuff

    /****
     * @oneToMany(targetEntity="Booking", mappedBy="hotel", indexBy="room")
     * @var Booking[]
     */
    private $bookings;
}

/****
 * @Entity
 */
class Booking
{
    /****
     * @var Hotel
     *
     * @Id 
     * @ManyToOne(targetEntity="Hotel", inversedBy="bookings")
     * @JoinColumns({
     *   @JoinColumn(name="hotel_id", referencedColumnName="id")
     * })
     */
    private $hotel;

    /****
     * @var Room
     *
     * @Id
     * @ManyToOne(targetEntity="Room")
     * @JoinColumns({
     *   @JoinColumn(name="room_id", referencedColumnName="id")
     * })
     */
    private $room;
}

Only possible workaround I found is to define another (plain) entity's property mapped to the same table column and index by it:

/****
 * @Entity
 */
class Hotel
{

    // $id column and other stuff

    /****
     * @oneToMany(targetEntity="Booking", mappedBy="hotel", indexBy="roomId")
     * @var Booking[]
     */
    private $bookings;
}

/****
 * @Entity
 */
class Booking
{
    // ...

    /****
     * @var Room
     *
     * @Id
     * @ManyToOne(targetEntity="Room")
     * @JoinColumns({
     *   @JoinColumn(name="room_id", referencedColumnName="id")
     * })
     */
    private $room;

    /****
     * @var integer $roomId
     *
     * @Column(name="room_id", type="integer", nullable=false)
     */
    private $roomId;
}

Wouldn't it be easy to support it?

@doctrinebot

Comment created by @beberlei:

It is not so easy to implement from the first gimplse and it is not a bug but an improvement/feature request.

@doctrinebot

Comment created by benjamin:

Related PR: #204

@doctrinebot

Comment created by benjamin:

This seems to have been integrated now:
#639

Should this be marked as resolved?

@doctrinebot

Comment created by sobotka:

Resolved. Thanks!

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.4 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment