0

我正在将 Doctrine 与 CodeIgniter 一起使用。我无法使用这种用法访问该类别。$record 不为空,但 $category 为空。

$record_data = $this->getRepository('Entity\RecordData')->find($this->params['id']);
$record = $record_data->getRecord();
$category = $record->getCategory();
echo $category->getName();

最后一行给出了致命错误:调用非对象上的成员函数 getName()。我究竟做错了什么?你能看看吗。谢谢。

这是我的实体类。

实体\记录数据:

<?php

namespace Entity;

/**
 * @Entity(repositoryClass="RecordDataRepository")
 * @Table(name="record_data")
 */
class RecordData {
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $ID;

    /**
     * @ManyToOne(targetEntity="CategoryVariable")
     * @JoinColumn(name="CategoryVariableID", referencedColumnName="ID")
     */
    protected $CategoryVariable;

    /**
     * @ManyToOne(targetEntity="Record")
     * @JoinColumn(name="RecordID", referencedColumnName="ID")
     */
    protected $Record;

    /**
     * @ManyToOne(targetEntity="SkillLevel")
     * @JoinColumn(name="SkillLevelID", referencedColumnName="ID")
     */
    protected $SkillLevel = NULL;

    /**
     * @Column(type="string", length=255, nullable=true)
     */
    protected $Value;

    /**
     * @Column(type="integer", nullable=false)
     */
    protected $RecordDataOrder;

    /**
     * @Column(type="boolean", nullable=false)
     */
    protected $Deleted = FALSE;

    public function __construct(Record $record, CategoryVariable $category_variable)
    {
        $this->Record = $record;
        $this->CategoryVariable = $category_variable;
    }

    public function getID()
    {
        return $this->ID;
    }

    public function getCategoryVariable()
    {
        return $this->CategoryVariable;
    }

    public function getRecord()
    {
        return $this->Record;
    }

    public function setSkillLevel($SkillLevel)
    {
        $this->SkillLevel = $SkillLevel;
    }

    public function getSkillLevel()
    {
        return $this->SkillLevel;
    }

    public function setValue($Value)
    {
        $this->Value = $Value;
    }

    public function getValue()
    {
        return $this->Value;
    }

    public function setRecordDataOrder($RecordDataOrder)
    {
        $this->RecordDataOrder = $RecordDataOrder;
    }

    public function getRecordDataOrder()
    {
        return $this->RecordDataOrder;
    }

    public function delete()
    {
        $this->Deleted = TRUE;
    }
}

实体\记录:

<?php

namespace Entity;

/**
 * @Entity(repositoryClass="RecordRepository")
 * @Table(name="record")
 */
class Record {
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $ID;

    /**
     * @ManyToOne(targetEntity="Category")
     * @JoinColumn(name="CategoryID", referencedColumnName="ID")
     */
    protected $Category;

    /**
     * @Column(type="string", length=255, nullable=true)
     */
    protected $Name;

    /**
     * @Column(type="integer", nullable=false)
     */
    protected $RecordOrder;

    /**
     * @Column(type="string", length=255, nullable=true)
     */
    protected $Description;

    /**
     * @Column(type="boolean", nullable=false)
     */
    protected $Deleted = FALSE;

    public function __construct(Category $category)
    {
        $this->Category = $category;
    }

    public function getID()
    {
        return $this->ID;
    }

    public function setCategory($Category)
    {
        $this->Category = $Category;
    }

    public function getCategory()
    {
        return $this->Category;
    }

    public function setName($Name)
    {
        $this->Name = $Name;
    }

    public function getName()
    {
        return $this->Name;
    }

    public function setRecordOrder($RecordOrder)
    {
        $this->RecordOrder = $RecordOrder;
    }

    public function getRecordOrder()
    {
        return $this->RecordOrder;
    }

    public function setDescription($Description)
    {
        $this->Description = $Description;
    }

    public function getDescription()
    {
        return $this->Description;
    }

    public function delete()
    {
        $this->Deleted = TRUE;
    }
}

实体\类别:

<?php

namespace Entity;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @Entity(repositoryClass="CategoryRepository")
 * @Table(name="category")
 */
class Category {
    /**
     * @Id
     * @Column(type="integer", nullable=false)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $ID;

    /**
     * @Column(type="string", length=255)
     */
    protected $Name;

    /**
     * @Column(type="string", length=255)
     */
    protected $Description;

    /**
     * @Column(type="integer", nullable=false)
     */
    protected $CategoryOrder;

    /**
     * @Column(type="boolean", nullable=false)
     */
    protected $IsMultiple;

    /**
     * @Column(type="boolean", nullable=false)
     */
    protected $Deleted = FALSE;

    public function getID()
    {
        return $this->ID;
    }

    public function setName($Name)
    {
        $this->Name = $Name;
    }

    public function getName()
    {
        return $this->Name;
    }

    public function setDescription($Description)
    {
        $this->Description = $Description;
    }

    public function getDescription()
    {
        return $this->Description;
    }

    public function setCategoryOrder($CategoryOrder)
    {
        $this->CategoryOrder = $CategoryOrder;
    }

    public function getCategoryOrder()
    {
        return $this->CategoryOrder;
    }

    public function setIsMultiple($IsMultiple)
    {
        $this->IsMultiple = $IsMultiple;
    }

    public function getIsMultiple()
    {
        return $this->IsMultiple;
    }

    public function delete()
    {
        $this->Deleted = TRUE;
    }

    /**
     * @var CategoryVariable[]
     * @OneToMany(targetEntity="CategoryVariable", mappedBy="Category")
     */
    protected $CategoryVariables = NULL;

    /**
     * @var Record[]
     * @OneToMany(targetEntity="Record", mappedBy="Category")
     */
    protected $Records = NULL;

    public function setCategoryVariable(CategoryVariable $CategoryVariable)
    {
        $this->CategoryVariables[] = $CategoryVariable;
    }

    public function getCategoryVariables()
    {
        return $this->CategoryVariables;
    }

    public function setRecord(Record $Record)
    {
        $this->Records[] = $Record;
    }

    public function getRecords()
    {
        return $this->Records;
    }

    public function __construct()
    {
        $this->CategoryVariables = new ArrayCollection();
        $this->Records = new ArrayCollection();
    }
}
4

1 回答 1

0

如果您的记录没有类别,那么您检索它的类别对象为空。因此,在回显其类别之前,您必须控制类别是真实对象还是空对象。像这样:

$record_data = $this->getRepository('Entity\RecordData')->find($this->params['id']);
$record = $record_data->getRecord();}
$category = $record->getCategory();
echo ($category) ? $category->getName() : "Record doesn't have category";
于 2013-07-16T17:16:32.823 回答