开发者

Select one entry from @OneToMany Association from within Model

开发者 https://www.devze.com 2023-03-24 03:55 出处:网络
I have Doctrine 2 model defined as: class Movie { /** @Id @Column(type=\"integer\") @GeneratedValue @var int */

I have Doctrine 2 model defined as:

class Movie {
    /** @Id @Column(type="integer") @GeneratedValue @var int */
    private $id;

    /**
     * @ManyToOne(targetEntity="Language")
     * @JoinColumn(nullable=false)
     */
    private $default_title_language;

    /**
     * @OneToMany(targetEntity="MovieTitle", mappedBy="movie")
     * @var MovieTitle[]
     */
    protected $titles = null;

    public function __construct() {
        $this->titles = new ArrayCollection();
    }

    public function get_titles() { return $this->titles; }
    public function get_title(Language $language = NULL) {
开发者_运维百科        if (is_null($language)) {
            $language = $this->default_title_language;
        }

        // ??????????
    }
    public function add_title(MovieTitle $title) { $this->titles[] = $title; }

    public function get_default_title_language() {
        return $this->default_title_language;
    }
    public function set_default_title_language(Language $language) {
        $this->default_title_language = $language;
    }
}

so... there are Movie, MovieTitle and Language models. One Movie may have many titles (Language dependent). I want to provide Movie model with a method - which will return me only one parameterized title. How can I do this? (marked with ??????????)


Doctrine 2.1 has indexed collections. It should do the job

http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/working-with-indexed-associations.html

0

精彩评论

暂无评论...
验证码 换一张
取 消