Doctrine Query - Get sub objects
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 :
Each device group has one smi_enterprise_code (via smi_enterprise_code_id)
Table smi_enterprise_code:
Table device :
These tables were created using artisan with theses classes :
SmiEnterpriseCode.php
<?php
namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesSmiEnterpriseCode")
* @ORMTable(name="smi_enterprise_code")
*/
class SmiEnterpriseCode extends SCAPBaseModel
{
/**
* @ORMColumn(type="integer", name="code")
*/
protected $code;
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* One SmiEnterpriseCode has Many DeviceGroup
* @ORMOneToMany(targetEntity="AppEntitiesDeviceGroup", 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 AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesDeviceGroup")
* @ORMTable(name="device_group")
*/
class DeviceGroup extends SCAPBaseModel
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* @ORMColumn(type="string", name="oid", length=127)
*/
protected $oid;
/**
* @ORMColumn(type="string", name="hash", length=127)
*/
protected $hash;
/**
* Many DeviceGroup have one SmiEnterpriseCode
* @ORMManyToOne(targetEntity="AppEntitiesSmiEnterpriseCode", inversedBy="groups")
* @ORMJoinColumn(name="smi_enterprise_code_id", nullable=false)
*/
protected $smi_enterprise_code;
/**
* One DeviceGroup has Many Device
* @ORMOneToMany(targetEntity="AppEntitiesDevice", 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 :
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,
php laravel doctrine
add a comment |
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 :
Each device group has one smi_enterprise_code (via smi_enterprise_code_id)
Table smi_enterprise_code:
Table device :
These tables were created using artisan with theses classes :
SmiEnterpriseCode.php
<?php
namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesSmiEnterpriseCode")
* @ORMTable(name="smi_enterprise_code")
*/
class SmiEnterpriseCode extends SCAPBaseModel
{
/**
* @ORMColumn(type="integer", name="code")
*/
protected $code;
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* One SmiEnterpriseCode has Many DeviceGroup
* @ORMOneToMany(targetEntity="AppEntitiesDeviceGroup", 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 AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesDeviceGroup")
* @ORMTable(name="device_group")
*/
class DeviceGroup extends SCAPBaseModel
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* @ORMColumn(type="string", name="oid", length=127)
*/
protected $oid;
/**
* @ORMColumn(type="string", name="hash", length=127)
*/
protected $hash;
/**
* Many DeviceGroup have one SmiEnterpriseCode
* @ORMManyToOne(targetEntity="AppEntitiesSmiEnterpriseCode", inversedBy="groups")
* @ORMJoinColumn(name="smi_enterprise_code_id", nullable=false)
*/
protected $smi_enterprise_code;
/**
* One DeviceGroup has Many Device
* @ORMOneToMany(targetEntity="AppEntitiesDevice", 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 :
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,
php laravel doctrine
add a comment |
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 :
Each device group has one smi_enterprise_code (via smi_enterprise_code_id)
Table smi_enterprise_code:
Table device :
These tables were created using artisan with theses classes :
SmiEnterpriseCode.php
<?php
namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesSmiEnterpriseCode")
* @ORMTable(name="smi_enterprise_code")
*/
class SmiEnterpriseCode extends SCAPBaseModel
{
/**
* @ORMColumn(type="integer", name="code")
*/
protected $code;
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* One SmiEnterpriseCode has Many DeviceGroup
* @ORMOneToMany(targetEntity="AppEntitiesDeviceGroup", 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 AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesDeviceGroup")
* @ORMTable(name="device_group")
*/
class DeviceGroup extends SCAPBaseModel
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* @ORMColumn(type="string", name="oid", length=127)
*/
protected $oid;
/**
* @ORMColumn(type="string", name="hash", length=127)
*/
protected $hash;
/**
* Many DeviceGroup have one SmiEnterpriseCode
* @ORMManyToOne(targetEntity="AppEntitiesSmiEnterpriseCode", inversedBy="groups")
* @ORMJoinColumn(name="smi_enterprise_code_id", nullable=false)
*/
protected $smi_enterprise_code;
/**
* One DeviceGroup has Many Device
* @ORMOneToMany(targetEntity="AppEntitiesDevice", 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 :
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,
php laravel doctrine
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 :
Each device group has one smi_enterprise_code (via smi_enterprise_code_id)
Table smi_enterprise_code:
Table device :
These tables were created using artisan with theses classes :
SmiEnterpriseCode.php
<?php
namespace AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesSmiEnterpriseCode")
* @ORMTable(name="smi_enterprise_code")
*/
class SmiEnterpriseCode extends SCAPBaseModel
{
/**
* @ORMColumn(type="integer", name="code")
*/
protected $code;
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* One SmiEnterpriseCode has Many DeviceGroup
* @ORMOneToMany(targetEntity="AppEntitiesDeviceGroup", 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 AppEntities;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity(repositoryClass="AppRepositoriesDeviceGroup")
* @ORMTable(name="device_group")
*/
class DeviceGroup extends SCAPBaseModel
/**
* @ORMColumn(type="string", name="name", length=255)
*/
protected $name;
/**
* @ORMColumn(type="string", name="oid", length=127)
*/
protected $oid;
/**
* @ORMColumn(type="string", name="hash", length=127)
*/
protected $hash;
/**
* Many DeviceGroup have one SmiEnterpriseCode
* @ORMManyToOne(targetEntity="AppEntitiesSmiEnterpriseCode", inversedBy="groups")
* @ORMJoinColumn(name="smi_enterprise_code_id", nullable=false)
*/
protected $smi_enterprise_code;
/**
* One DeviceGroup has Many Device
* @ORMOneToMany(targetEntity="AppEntitiesDevice", 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 :
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,
php laravel doctrine
php laravel doctrine
edited Nov 16 '18 at 10:03
Cybermate
asked Nov 15 '18 at 16:57
CybermateCybermate
52210
52210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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,
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324421%2fdoctrine-query-get-sub-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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,
add a comment |
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,
add a comment |
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,
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,
answered Nov 17 '18 at 21:41
CybermateCybermate
52210
52210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324421%2fdoctrine-query-get-sub-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown