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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)