Doctrine2缺少相关表的值

时间:2017-07-26 11:11:59

标签: php mysql doctrine-orm

我遇到一个问题,即在查询结果中缺少相关表的值。

我的查询是:

$em = $this->getDoctrine()
        ->getManager();  

$repo = $em->getRepository("DbBundle:Auto"); 
$auto = $repo->findAll();

var_dump($auto);

在vardump的输出中,缺少字段hName和hCountry的值... 我期望doctrine会自动加载已经实现的表的值,还是我还要进行连接?

C:\Apache24\htdocs\src\DbBundle\Controller\DefaultController.php:90:
array (size=3)
  0 => 
    object(DbBundle\Entity\Auto)[427]
      private 'aId' => int 7
      private 'aName' => string 'A5' (length=2)
      private 'aPs' => int 190
      private 'aHersteller' => 
        object(Proxies\__CG__\DbBundle\Entity\Hersteller)[442]
          public '__initializer__' => 
            object(Closure)[435]
              ...
          public '__cloner__' => 
            object(Closure)[437]
              ...
          public '__isInitialized__' => boolean false
          private 'hId' (DbBundle\Entity\Hersteller) => int 1
          private 'hName' (DbBundle\Entity\Hersteller) => null
          private 'hCountry' (DbBundle\Entity\Hersteller) => null
  1 => 
    object(DbBundle\Entity\Auto)[441]
      private 'aId' => int 8
      private 'aName' => string 'Ibiza' (length=5)
      private 'aPs' => int 130
      private 'aHersteller' => 
      .
      .
      .ASO

实体:自动

  /**
 * Auto
 *
 * @ORM\Table(name="AUTO", uniqueConstraints={@ORM\UniqueConstraint(name="const_auto_name", columns={"A_NAME"})}, indexes={@ORM\Index(name="A_HERSTELLER", columns={"A_HERSTELLER"})})
 * @ORM\Entity
 */
class Auto
{
    /**
     * @var integer
     *
     * @ORM\Column(name="A_ID", type="smallint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $aId;

    /**
     * @var string
     *
     * @ORM\Column(name="A_NAME", type="string", length=100, nullable=false)
     */
    private $aName;

    /**
     * @var integer
     *
     * @ORM\Column(name="A_PS", type="smallint", nullable=true)
     */
    private $aPs = '0';

    /**
     * @var \DbBundle\Entity\Hersteller
     *
     * @ORM\ManyToOne(targetEntity="DbBundle\Entity\Hersteller")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="A_HERSTELLER", referencedColumnName="H_ID", fetch="EAGER")
     * })
     */
    private $aHersteller;

实体:Hersteller

    /**
 * Hersteller
 *
 * @ORM\Table(name="HERSTELLER", uniqueConstraints={@ORM\UniqueConstraint(name="const_hersteller_name", columns={"H_NAME"})})
 * @ORM\Entity
 */
class Hersteller
{
    /**
     * @var integer
     *
     * @ORM\Column(name="H_ID", type="smallint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $hId;

    /**
     * @var string
     *
     * @ORM\Column(name="H_NAME", type="string", length=100, nullable=false)
     */
    private $hName;

    /**
     * @var string
     *
     * @ORM\Column(name="H_COUNTRY", type="string", length=100, nullable=false)
     */
    private $hCountry;

Table structure and content:

1 个答案:

答案 0 :(得分:0)

您需要在EntityRepository中创建自定义方法,并选择加入关联实体的实体:

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;

class AutoRepository extends EntityRepository
{
    public function findAll()
    {
        return $this->createQueryBuilder('a')
            ->select('a','h');
            ->join('a.aHersteller', 'h');
            ->getQuery()
            ->getResult()
        ;
    }
}