Doctrine 2.1 - 调用未定义的方法+ MyMethod(代理)?

时间:2011-12-24 19:36:42

标签: php doctrine-orm

我有两个班级:

新闻

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

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

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

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

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

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

    # CATEGORY methods
    public function setCategory($val) { if($val instanceof NewsCategory) $this->category = $val; }
    public function getCategory() { return $this->category; }
}

新闻分类

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

    /**
     * @Column(type="string", length=50, unique=TRUE)
     * @var string
     */
    protected $name;

    /**
     * @OneToMany(targetEntity="News", mappedBy="category")
     */
    protected $news;    

    public function __construct() {
        $this->news = new \Doctrine\Common\Collections\ArrayCollection;
    }

    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }

    public function getNews() { return $this->news; }
}

我想使用此查询下载一条新闻:

$q = $this->db->createQuery("SELECT n FROM News n WHERE n.id = :id");
$q->setParameter('id', $_GET['id']);
$news = $q->getResult();

接下来,我希望通过

获取与此新闻相关的类别的ID
$news->getCategory()->getId()

上面的代码,我收到了这个错误:

Fatal error: Call to undefined method DoctrineProxies\NewsCategoryProxy::getId() in C:\[...]\newsController.php on line 61

怎么了?为什么我的NewsCategory课程无法看到getId()方法?

1 个答案:

答案 0 :(得分:3)

这是一个很好的做法,总是声明你的班级成员是私人的,并在你的班级成员身上产生吸气剂和制定者。

在您的情况下,您不会生成getter和setter(getId()类没有NewCategory方法)。

这就是你的NewCategory课程的样子:

/** @Entity @Table(name="news_category") */
class NewsCategory {
    /**
     * @Id @GeneratedValue @Column(type="integer")
     * @var integer
     */
    private $id;

    /**
     * @Column(type="string", length=50, unique=TRUE)
     * @var string
     */
    private $name;

    /**
     * @OneToMany(targetEntity="News", mappedBy="category")
     */
    private $news;    

    public function __construct() {
        $this->news = new \Doctrine\Common\Collections\ArrayCollection;
    }

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

    public function setName($name) { $this->name = $name; }
    public function getName() { return $this->name; }

    public function setNews($news) {$this->news = $news;}
    public function getNews() { return $this->news; }

}

生成的代理不会在每个属性上生成神奇的 getter和setter(它会破坏OOP的封装原则)。 您可以在此处找到有关代理的更多文档:http://www.doctrine-project.org/docs/orm/2.0/en/reference/configuration.html#proxy-objects