繁体   English   中英

为什么join表在symfony2中不起作用?

[英]Why join table is not working in symfony2?

我的Post.php

<?php
// src/Acme/UserBundle/Entity/User.php

namespace Acme\PostBundle\Entity;


use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 */
class Post
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     *
     * @ORM\Column(type="integer")
     */
    public $user_id;
    /**
     * @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts")
     * @ORM\JoinColumn(name="id", referencedColumnName="id")
     */
    protected $user;
    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(message="Введите текст")
     * )
     */
    protected $text;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $address;
    /**
     *
     * @ORM\Column(type="datetime")
     */
    protected $date;



}

和我的User.php:

<?php
// src/Acme/UserBundle/Entity/User.php

namespace Acme\PostBundle\Entity;


use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User
{

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;
    /**
     *
     * @ORM\Column(type="string")
     */
    protected $path;

    /**
    * @ORM\OneToMany(targetEntity="Acme\PostBundle\Entity\Post", mappedBy="users")
    */
    protected $posts;

    public function __construct() {

        $this->posts = new ArrayCollection();

    }


}

我想按表“ posts”中的“ user_id”列创建联接表用户和帖子。 接下来,我使用queryBuilder进行查询:

    $repository = $this->getDoctrine()->getManager()->getRepository('AcmePostBundle:Post');


    $queryPosts = $repository->createQueryBuilder('p')
                   ->orderBy('p.id', 'DESC')
                   ->getQuery();

    return new Response(var_dump($queryPosts->getResult()));

我得到:

object(Acme\PostBundle\Entity\Post)[476]
      protected 'id' => int 1
      public 'user_id' => int 1
      protected 'user' => 
        object(Proxies\__CG__\Acme\PostBundle\Entity\User)[475]
          public '__initializer__' => 
            object(Closure)[469]
              ...
          public '__cloner__' => 
            object(Closure)[470]
              ...
          public '__isInitialized__' => boolean false
          protected 'id' => int 1
          protected 'path' => null
          protected 'posts' => null
      protected 'text' => string 'Test' (length=4)
      protected 'address' => string 'Test' (length=4)
      protected 'date' => 
        object(DateTime)[477]
          public 'date' => string '2014-11-06 14:39:13' (length=19)
          public 'timezone_type' => int 3
          public 'timezone' => string 'Europe/Kiev' (length=11)

为什么user-> path为null? 如何使此联接在表中

当使用辅助函数(如find()findAll() ,Doctrine利用延迟加载方法来按需获取相关实体,但是当您使用查询生成器时,您正在创建显式的查询/结果,这些查询/结果不会被Doctrine的lazy处理-loader。

使用查询生成器时,必须显式加入其他相关实体:

$queryPosts = $this->getDoctrine()->getManager()->createQueryBuilder()
               ->select('p, u')
               ->from('AcmePostBundle:Post', 'p')
               ->leftJoin('p.user','u') // handles the ManyToOne association automatically
               ->orderBy('p.id', 'DESC')
               ->getQuery();

请注意,您可以轻松地使用innerJoin而不是leftJoin但是如果您有任何具有空关联的潜在Post项,则可以解决此问题。

您还必须确保您的实体映射正确:

/**
 * @ORM\ManyToOne(targetEntity="Acme\PostBundle\Entity\User", inversedBy="posts")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

不然原则将尝试匹配id后的对id的用户的。 (类似于LEFT JOIN Post ON Post.id = User.id到达原始查询时的状态)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM