0

CodeIgniter で Doctrine を使用しています。この使い方ではカテゴリにアクセスできません。$record は null ではありませんが、$category は null です。

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

最後の行は Fatal error: Call to a member function getName() on a non-object を返します。私は何を間違っていますか?どうぞご覧ください。ありがとう。

これが私のエンティティクラスです。

エンティティ\レコードデータ:

<?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

レコードにカテゴリがない場合、取得するカテゴリ オブジェクトは null です。したがって、カテゴリをエコーする前に、カテゴリが実際のオブジェクトか null オブジェクトかを制御する必要があります。このような:

$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 に答える