Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony3:一对多关系返回空对象_Php_Symfony_Doctrine Orm_Symfony 3.4 - Fatal编程技术网

Php Symfony3:一对多关系返回空对象

Php Symfony3:一对多关系返回空对象,php,symfony,doctrine-orm,symfony-3.4,Php,Symfony,Doctrine Orm,Symfony 3.4,我正在尝试获取我的条带实体中一对多作者属性的内容。该关系在账户和条带实体之间,双向,且由条带实体拥有 以下是我为这两个项目编写的代码: Strip.php /** * Strip * * @ORM\Table(name="strip") * @ORM\Entity(repositoryClass="AppBundle\Repository\StripRepository") */ class 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
 *
 * @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();
    }
    //...
}
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;
        }
    }

}
Account.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
 *
 * @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();
    }
    //...
}
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;
        }
    }

}
当我试图从
条带
中获取
作者
属性时,我会得到一个空的
帐户
,该帐户具有正确的
id
,但在其他字段上为空(
用户名
slug
,…)

例如,以下是
转储($strip->getAuthor())
返回的内容:

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
}
以下是我的数据库的屏幕截图,显示此关系已正确注册:

数据库屏幕截图条带表:

数据库屏幕截图帐户表:
Doctrine2使用延迟加载

只要手头有一个托管实体实例,就可以遍历和使用该实体的任何关联,这些关联被配置为惰性的,就好像它们已经在内存中一样。条令将通过延迟加载的概念根据需要自动加载相关对象

嗨,这是一个很好的理论。。。尝试输入您的:

/**
 * @var array
 *
 * @ORM\OneToMany(targetEntity="Strip", mappedBy="author", fetch="EAGER")
 */
private $strips;
或者更好的解决方案是在StripRepository中进行自定义查询并选择Author

$this->createQueryBuilder('s')
     ->addSelect('a')
     ->join('s.author', 'a')

我是如何解决这个问题的

我在
StripRepository.php
中执行了一个自定义联接查询,以获取
帐户
Strip
实体内容:

StripRepository.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
 *
 * @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();
    }
    //...
}
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
实体加载内容

我再次阅读了Symfony文档中关于条令关联的内容,在这里我找到了我使用的示例代码:

当然,如果预先知道需要访问这两个对象,可以[…]在原始查询中发出联接

感谢Mocrates让我领先:

或者更好的解决方案是在StripRepository中进行自定义查询并选择Author

$this->createQueryBuilder('s')
     ->addSelect('a')
     ->join('s.author', 'a')

很确定这是延迟加载。你是怎么得到数据的?我也是@你能查一下我的答案吗?