douqujin2767 2018-11-15 16:57
浏览 45
已采纳

Doctrine Query - 获取子对象

I am using Doctrine ORM in my Laravel Project. I have three tables :

  • Smi
  • Device Group
  • Device

Each device has a device group, and each device group has a smi. What I want to do with Doctrine is to query all groups and get their devices and their smi.

Here is my query in PHP (from the repository) :

   $results = $this->_em->createQueryBuilder()
            ->select('dg')
            ->from($this->_entityName, 'dg')
            ->innerJoin('dg.smi_enterprise_code', 'sec')
            ->getQuery()
            ->getResult();

Here are the tables structure :

Table device_group :

Table devicegroup

Each device group has one smi_enterprise_code (via smi_enterprise_code_id)

Table smi_enterprise_code:

Table smienterprisecode

Table device :

Table device

These tables were created using artisan with theses classes :

SmiEnterpriseCode.php

    <?php

    namespace App\Entities;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;

    /**
 * @ORM\Entity(repositoryClass="App\Repositories\SmiEnterpriseCode")
 * @ORM\Table(name="smi_enterprise_code")
 */
class SmiEnterpriseCode extends SCAPBaseModel
{
    /**
     * @ORM\Column(type="integer", name="code")
     */
    protected $code;

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

    /**
     * One SmiEnterpriseCode has Many DeviceGroup
     * @ORM\OneToMany(targetEntity="App\Entities\DeviceGroup", cascade={"persist", "remove"}, mappedBy="smi_enterprise_code")
     */
    protected $groups;

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

    public function getIana()
    {
        return $this->code;
    }

    public function getVendor()
    {
        return $this->name;
    }

    public function getGroups()
    {
        return $this->groups;
    }

    public function addGroup(DeviceGroup $group)
    {
        $this->groups->add($group);
        $group->setSmi($this);
    }

DeviceGroup.php

    <?php

    namespace App\Entities;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;

    /**
 * @ORM\Entity(repositoryClass="App\Repositories\DeviceGroup")
 * @ORM\Table(name="device_group")
 */
class DeviceGroup extends SCAPBaseModel
{
    /**
     * @ORM\Column(type="string", name="name", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", name="oid", length=127)
     */
    protected $oid;

    /**
     * @ORM\Column(type="string", name="hash", length=127)
     */
    protected $hash;

    /**
     * Many DeviceGroup have one SmiEnterpriseCode
     * @ORM\ManyToOne(targetEntity="App\Entities\SmiEnterpriseCode", inversedBy="groups")
     * @ORM\JoinColumn(name="smi_enterprise_code_id", nullable=false)
     */
    protected $smi_enterprise_code;

    /**
     * One DeviceGroup has Many Device
     * @ORM\OneToMany(targetEntity="App\Entities\Device", cascade={"persist", "remove"}, mappedBy="device_group")
     */
    protected $devices;

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

    public function setSmi(SmiEnterpriseCode $smi)
    {
        $this->smi_enterprise_code = $smi;
    }

    public function toArray()
    {
        return [
            "id" => $this->id,
            "active" => $this->active,
            "oid" => $this->oid,
            "name" => $this->name,
            "iana" => $this->smi_enterprise_code->getIana(),
            "vendor" => $this->smi_enterprise_code->getVendor(),
            "created_at" => parent::formatTimestamp($this->date_created),
            "updated_at" => parent::formatTimestamp($this->timestamp)
        ];
    }


}

For now I just want to access to the SMI of the groups, I will deal with the devices later.

NB : the classes extend from a superclass which includes common fields such as id, timestamp...

This is what I get at the moment :

What I get

As you can see the values are null whereas I created entries in the database (and anyway I cant have a device group without a SMI).

Do you see what the problem is ?

Thanking you in advance,

  • 写回答

1条回答 默认 最新

  • duansai1314 2018-11-17 21:41
    关注

    Finally I found out the causes of my problem.

    I just had to run php artisan doctrine:generate:entities in order to get rid of it.

    Regards,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀