I have 3 entities. And try to load data by native SQL with ResultSetMapping. My question is abount why all times profile is null. Code is under text. Please help to resolve this((( I try to join table by addFieldResult and fill columns from this table, but it's doen't has any effects. Domain entity load fine.
I thinking this problem can be because account and profile entitites have the same name of relation field and profile "inversedBy="account""
If I try to use
$rsm->addFieldResult('p', 'first_name', 'firstName', Profile::class);
for load specified field for Profile hydration doesn't work. ObjectHydrator.php in _hydrateRowData_ checks _hydrateRowData_ that must contains _isIdentifier_ fields, in my case _first_name_ it's not identifier and after this check finally set value to null.
Fetching data code
$sql = 'SELECT a.*, p.*, p.account_id as p_account FROM account AS a JOIN profile AS p ON a.account_id = p.account_id WHERE a.domain_id = ? ORDER BY a.account_id DESC LIMIT 0, 25';
$rsm = new ResultSetMapping();
$rsm->addEntityResult(\App\Entity\Account::class, 'account');
$rsm->addJoinedEntityResult(Domain::class, 'd', 'account', 'domain');
$rsm->addJoinedEntityResult(Profile::class, 'p', 'account', 'profile');
$rsm->addFieldResult('account', 'account_id', 'accountId');
$rsm->addFieldResult('account', 'created_date', 'createdDate');
$rsm->addFieldResult('account', 'zendesk_user_id', 'zendeskUserId');
$rsm->addFieldResult('d', 'domain_id', 'id', Domain::class);
$query = $this->em->createNativeQuery($sql, $rsm);
$query->setParameter(1, 1);
$users = $query->getResult();
Account:
/**
* Class Account
* @package App\Entity
* @ORM\Entity(repositoryClass="App\Entity\Repository\AccountRepository")
* @ORM\Table(name="account")
*/
class Account
{
/**
* @var Domain
* @ORM\ManyToOne(targetEntity="Domain")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="domain_id", referencedColumnName="id")
* })
*/
protected $domain;
/**
* @var Profile
*
* @ORM\OneToOne(targetEntity="Profile", inversedBy="account", fetch="LAZY", cascade={"all"})
* @ORM\JoinColumn(name="account_id", referencedColumnName="account_id")
*/
protected $profile;
}
Profile:
/**
* Class Profile
* @package App\Entity
*
* @ORM\Entity
* @ORM\Table(name="profile")
*/
class Profile
{
/**
* @var Account
*
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
* @ORM\OneToOne(targetEntity="Account", mappedBy="profile", fetch="EAGER")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="account_id", referencedColumnName="account_id")
* })
*/
private $account;
/**
* @var string
* @ORM\Column(name="first_name", type="string", length=127, nullable=false)
* @Restricted()
*/
private $firstName;
/**
* @var string
*
* @ORM\Column(name="last_name", type="string", length=127, nullable=false)
* @Restricted()
*/
private $lastName;
Domain:
/**
* Domain
*
* @ORM\Table(name="domain")
* @ORM\Entity
*/
class Domain
{
/**
* @var integer
*
* @ORM\Id
* @ORM\Column(name="id", type="integer", nullable=false)
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
protected $name;
}
@Inmmelman do you really need to use the native API on that case? I mean, do you have what you need when not using it?
Is there any news about this issue?
I run into a very similar issue. I ended up with just
$rsm->addRootEntityFromClassMetadata(\App\Entity\Account::class, 'a');
Most helpful comment
Is there any news about this issue?