加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Doctrine 2.1 – 获得实体限制

发布时间:2020-12-13 17:17:50 所属栏目:PHP教程 来源:网络整理
导读:我有两节课: 游戏 /** @Entity @Table(name="games") */class Game { /** @Id @GeneratedValue @Column(type="integer") */ protected $id; /** @Column(type="string",length=100) */ protected $title; /** @ManyToMany(targetEntity="News",mappedBy="ga
我有两节课:
游戏

/** @Entity @Table(name="games") */
class Game {
    /** @Id @GeneratedValue @Column(type="integer") */
    protected $id;

    /** @Column(type="string",length=100) */
    protected $title;

    /** @ManyToMany(targetEntity="News",mappedBy="games") */
    protected $news;

    public function __construct() {
        $this->news = new DoctrineCommonCollectionsArrayCollection();
    }

    public function getId() { return $this->id; }

    public function setTitle($val) { $this->title = trim($val); }
    public function getTitle() { return $this->title; }

    public function getNews() { return $this->news; }
    public function setNews($value) {
        $except_txt = 'Jedna z przes?anych warto?ci nie jest instancj? klasy News!';

        if(is_array($value)) {
            foreach($value as $v) {
                if($v instanceof News) $this->news->add($v);
                else throw new Exception($except_txt);
            }
        } else {
            if($value instanceof News) $this->news->add($value);
            else throw new Exception($except_txt);
        }
    }
}

新闻:

/** @Entity @Table(name="news") */
class News {
    /** @Id @GeneratedValue @Column(type="integer") */
    protected $id;

    /** @Column(type="string",length=100) */
    protected $title;

    /** @Column(type="text") */
    protected $content;

    /**
     * @ManyToOne(targetEntity="User",inversedBy="news")
     * @JoinColumn(referencedColumnName="id")
     */ 
    protected $author;

    /** @Column(type="datetime") */
    protected $add_date;

    /**
     * @ManyToMany(targetEntity="Game",inversedBy="news")
     * @JoinTable(name="news_game",*      joinColumns={@JoinColumn(name="news_id",referencedColumnName="id")},*      inverseJoinColumns={@JoinColumn(name="game_id",referencedColumnName="id")}
     *      )
     */
    protected $games;

    public function __construct() {
        $this->add_date = new DateTime();
        $this->games = new DoctrineCommonCollectionsArrayCollection();
    }


    # ID methods
    public function getId() { return $this->id; }

    # TITLE methods
    public function setTitle($val) { $this->title = $val; }
    public function getTitle() { return $this->title; }

    # CONTENT methods
    public function setContent($val) { $this->content = $val; }
    public function getContent() { return $this->content; }

    # AUTHOR methods
    public function setAuthor($val) { if($val instanceof User) $this->author = $val; }
    public function getAuthor() { return $this->author; }

    # ADD DATE methods
    public function getAddDate() { return $this->add_date; }

    # GAMES methods
    public function setGames($value) {
        $except_txt = 'Jedna z przes?anych warto?ci nie jest instancj? klasy Game!';

        if(is_array($value)) {
            foreach($value as $v) {
                if($v instanceof Game) $this->games->add($v);
                else throw new Exception($except_txt);
            }
        } else {
            if($value instanceof Game) $this->games->add($value);
            else throw new Exception($except_txt);
        }
    }
    public function getGames() { return $this->games; }
}

这段代码:

$i = 1;
if($game->getNews()->count() > 0) {
    foreach($game->getNews()->getValues() as $v) {
        $news_list.= '<p>News '.$i.'</p>';
        $i++;
        if($i == 6) break;
    }
}

第一个问题:
Doctrine是否会从数据库中下载与特定游戏相关的所有新闻,或仅下载我需要的那些5?

第二个问题:
如何在没有NewsGames课程的情况下使用Doctrine的单独查询下载5个新闻?

解决方法

预先回答:
在我开始回答之前,你并不需要从News ArrayCollection中获取getValues().
你所能做的就是:

foreach ($game->getNews() as $news) {
    // Do whatever you want with associated News object.
}

答案#1:
Doctrine 2只会检索与此给定游戏相关的新闻,而不是其他任何内容.

答案#2:
您不需要NewsGames对象.请注意,您映射的是连接表,因此在OO透视图中,对象永远不存在.

干杯,

吉列尔梅布兰科

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读