Symfony3:关系一对多 return 空对象
Symfony3 : Relation One to Many return empty object
我试图在我的 Strip
实体中获取一对多 author
属性的内容。 Account
和 Strip
实体之间的关系是双向的,并且由 Strip
实体拥有。
这是我对它们的代码:
Strip.php
/**
* Strip
*
* @ORM\Table(name="strip")
* @ORM\Entity(repositoryClass="AppBundle\Repository\StripRepository")
*/
class Strip
{
//...
/**
*
* @ORM\ManyToOne(targetEntity="Account", inversedBy="strips")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
//...
}
Account.php
/**
* Account
*
* @ORM\Table(name="account")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AccountRepository")
*/
class Account
{
//...
/**
* @var array
*
* @ORM\OneToMany(targetEntity="Strip", mappedBy="author")
*/
private $strips;
public function __construct() {
$this->strips = new ArrayCollection();
}
//...
}
当我尝试从 strip
获取 author
属性时,我得到一个空的 account
和右边的 id
,但在其他字段上是空的( username
, slug
, ...).
例如,这里是 dump($strip->getAuthor())
returns:
MyController.php on line 326:
Account {#773 ▼
+__isInitialized__: false
-id: 1
-username: null
-email: null
-emailIsPublic: 0
-password: null
-avatar: null
-biography: null
-level: 1
-registerDate: null
-strips: null
#slug: null
…2
}
以下是我的数据库的屏幕截图,显示此关系已正确注册:
数据库截图条table:
数据库截图账号table:
Doctrine2 使用延迟加载。
Whenever you have a managed entity instance at hand, you can traverse and use any associations of that entity that are configured LAZY as if they were in-memory already. Doctrine will automatically load the associated objects on demand through the concept of lazy-loading
嗨,这是 Doctrine 懒惰的获取...尝试放入您的 :
/**
* @var array
*
* @ORM\OneToMany(targetEntity="Strip", mappedBy="author", fetch="EAGER")
*/
private $strips;
或者更好的解决方案是在 StripRepository 和 select Author
中进行自定义查询
$this->createQueryBuilder('s')
->addSelect('a')
->join('s.author', 'a')
我是怎么解决这个问题的
我在 StripRepository.php
中进行了自定义连接查询以获取 Account
和 Strip
实体内容:
StripRepository.php
class StripRepository extends \Doctrine\ORM\EntityRepository
{
public function findAuthors($strip)
{
$query = $this->getEntityManager()
->createQuery(
'SELECT s, a
FROM AppBundle:Strip s
JOIN s.author a
WHERE s.id = :id'
)
->setParameter('id', $strip);
try
{
return $query->getSingleResult();
}
catch (\Doctrine\ORM\NoResultException $e)
{
return null;
}
}
}
问题是什么?
Doctrine2 默认使用延迟加载,所以这样做允许从两个实体加载所有内容,而不是只从 Strip
实体加载内容。
我再次阅读了关于 Doctrine associations 的 Symfony 文档,在那里我找到了我使用的示例代码:
Of course, if you know up front that you'll need to access both objects, you can [...] issue a join in the original query.
Joining Related Records - Symfony 3.4
感谢 Mocrates 让我领先:
Or Better solution make a custom query in a StripRepository and select Author
我试图在我的 Strip
实体中获取一对多 author
属性的内容。 Account
和 Strip
实体之间的关系是双向的,并且由 Strip
实体拥有。
这是我对它们的代码:
Strip.php
/**
* Strip
*
* @ORM\Table(name="strip")
* @ORM\Entity(repositoryClass="AppBundle\Repository\StripRepository")
*/
class Strip
{
//...
/**
*
* @ORM\ManyToOne(targetEntity="Account", inversedBy="strips")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
//...
}
Account.php
/**
* Account
*
* @ORM\Table(name="account")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AccountRepository")
*/
class Account
{
//...
/**
* @var array
*
* @ORM\OneToMany(targetEntity="Strip", mappedBy="author")
*/
private $strips;
public function __construct() {
$this->strips = new ArrayCollection();
}
//...
}
当我尝试从 strip
获取 author
属性时,我得到一个空的 account
和右边的 id
,但在其他字段上是空的( username
, slug
, ...).
例如,这里是 dump($strip->getAuthor())
returns:
MyController.php on line 326:
Account {#773 ▼
+__isInitialized__: false
-id: 1
-username: null
-email: null
-emailIsPublic: 0
-password: null
-avatar: null
-biography: null
-level: 1
-registerDate: null
-strips: null
#slug: null
…2
}
以下是我的数据库的屏幕截图,显示此关系已正确注册:
数据库截图条table:
数据库截图账号table:
Doctrine2 使用延迟加载。
Whenever you have a managed entity instance at hand, you can traverse and use any associations of that entity that are configured LAZY as if they were in-memory already. Doctrine will automatically load the associated objects on demand through the concept of lazy-loading
嗨,这是 Doctrine 懒惰的获取...尝试放入您的 :
/**
* @var array
*
* @ORM\OneToMany(targetEntity="Strip", mappedBy="author", fetch="EAGER")
*/
private $strips;
或者更好的解决方案是在 StripRepository 和 select Author
中进行自定义查询$this->createQueryBuilder('s')
->addSelect('a')
->join('s.author', 'a')
我是怎么解决这个问题的
我在 StripRepository.php
中进行了自定义连接查询以获取 Account
和 Strip
实体内容:
StripRepository.php
class StripRepository extends \Doctrine\ORM\EntityRepository
{
public function findAuthors($strip)
{
$query = $this->getEntityManager()
->createQuery(
'SELECT s, a
FROM AppBundle:Strip s
JOIN s.author a
WHERE s.id = :id'
)
->setParameter('id', $strip);
try
{
return $query->getSingleResult();
}
catch (\Doctrine\ORM\NoResultException $e)
{
return null;
}
}
}
问题是什么?
Doctrine2 默认使用延迟加载,所以这样做允许从两个实体加载所有内容,而不是只从 Strip
实体加载内容。
我再次阅读了关于 Doctrine associations 的 Symfony 文档,在那里我找到了我使用的示例代码:
Of course, if you know up front that you'll need to access both objects, you can [...] issue a join in the original query.
Joining Related Records - Symfony 3.4
感谢 Mocrates 让我领先:
Or Better solution make a custom query in a StripRepository and select Author