8

I have 3 entities:

Style

Colour

Article

An article has a ManyToOne relationship with both entities (an article is a unique combination of style and colour). All entities have auto incrementing surrogate integer indexes.

I have a Style Entity, and a Colour Entity, and I want to create a new article that links these two entities if one does not already exist. Given that there is no way to do the equivalent of ('INSERT on DUPLICATE KEY UPDATE') using doctrine, I need to find any Articles that have a relationship with BOTH my Style and Colour entities. If there are no matches then create a new entity.

How can I locate any Article Entity within my system that is related to both Style and Colour Entities?

/** 
 * @Entity 
 */
class Style{

    /** @Id @GeneratedValue @Column (type="integer") */
    private $id;
    /** @Column (name="name", type="string") */
    private $name;
    /**
     * @OneToMany(targetEntity="Article", mappedBy="style", cascade={"persist"})
     */
    private $articles;
}


class Colour{
    /** @Id @GeneratedValue @Column (type="integer") */
    private $id;
    /** @Column (name="name", type="string") */
    private $name;
}

class Article{

    /** @Id @GeneratedValue @Column (type="integer") */
    private $id;

    /**
     * @ManyToOne(targetEntity="Style", inversedBy="articles", cascade={"persist"})
     */
    private $style;

    /**
     * @ManyToOne(targetEntity="Colour", cascade={"persist"})
     */
    private $colour;
}

The 'findBy' methods as described here http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-objects.html only seem to allow finding entities by using string values of entity properties, and only one of those at a time.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
calumbrodie
  • 4,722
  • 5
  • 35
  • 63
  • 4
    Aannnnddd I just found this question which has the answer... http://stackoverflow.com/questions/4619071/doctrine2-findby-relationship-object-triggers-string-conversion-error – calumbrodie May 10 '11 at 14:18

0 Answers0