我有一个问题,查询结果中缺少相关表的值。

我的查询是:

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

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

var_dump($auto);


在vardump的输出中,缺少字段hName和hCountry的值...
我希望该原则会自动加载已分配表的值,还是我还要进行联接?

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
 *
 * @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:

最佳答案

您需要在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()
        ;
    }
}

10-07 17:59