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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度