Doctrine 2 Native Query不会返回连接对象

时间:2017-12-06 15:27:52

标签: php mysql symfony doctrine-orm dql

我有3个实体。并尝试使用ResultSetMapping通过本机SQL加载数据。我的问题是为什么 profile 的所有时间都为空。代码在文本下。请帮忙解决这个问题(((我尝试通过addFieldResult连接表并填充此表中的列,但它没有任何效果。域实体加载正常

获取数据代码

$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();

帐户:

/**
 * 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;
}

配置文件:

/**
 * 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
 *
 * @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;
}

0 个答案:

没有答案