如何在没有自定义查询的情况下从多对多表获取数据?

时间:2019-01-07 10:22:10

标签: php symfony symfony-3.3

好吧,我创建了ManyToMany关系,该关系很好,但是当我尝试获取数据时,它返回了一个空对象:

我如何获取数据(控制器):

    $userRepo = $this->getDoctrine()->getRepository(User::class);
    $user = $this->getUser();
    $user = $userRepo->find($user->getId());
    $test = $user->getFavouriteArticles();
    dump($test); exit;

用户实体:

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    private $firstName;

    /**
     * @var favouriteArticles[]
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Article")
     * @ORM\JoinTable(name="user_favourite_articles",
     *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")})
     */
    private $favouriteArticles;

    public function __construct() {
        $this->favouriteArticles = new ArrayCollection();
    }

    /**
     * @return favouriteArticles[]
     */
    public function getFavouriteArticles(): object
    {
        return $this->favouriteArticles;
    }

    /**
     * @param favouriteArticles[] $favouriteArticles
     */
    public function setFavouriteArticles(array $favouriteArticles): void
    {
        $this->favouriteArticles = $favouriteArticles;
    }

    /**
     * Add user
     *
     * @param \AppBundle\Entity\Article $article
     *
     * @return User
     */
    public function addFavouriteArticle(Article $article)
    {
        $this->favouriteArticles[] = $article;

        return $this;
    }

我如何添加用户喜欢的文章:

public function addToFavourites($category, $id)
{
    $em = $this->getDoctrine()->getManager();
    $article = $em->getRepository("AppBundle:Article")->find($id);
    $user = $this->getUser();
    $user = $em->getRepository("AppBundle:User")->find($user->getId());
    $user->addFavouriteArticle($article);
    $em->persist($user);
    $em->flush();

DB(user_favourite_articles表):

|user_id| article_id |
|____________________|
|    3  |      8     |
|    3  |      9     |

当前user_id是3

转储的结果为,我无法显示favouriteArticles

PersistentCollection {#7796 ▼
  -snapshot: []
  -owner: User {#7563 ▼
    #id: 3
    -firstName: "test"
    -favouriteArticles: PersistentCollection {#7796}
    #username: "test@test.com"
    #usernameCanonical: "test@test.com"
    #email: "test@test.com"
    #emailCanonical: "test@test.com"
    #enabled: true
    #salt: null
    #password: "$2y$13$.8Ky5Jj71PUsGD9E04nre./xClPVZ/Uiia40PTQjGmMAqOFpW2mwi"
    #plainPassword: null
    #lastLogin: DateTime {#7559 ▶}
    #confirmationToken: null
    #passwordRequestedAt: null
    #groups: null
    #roles: []
  }

0 个答案:

没有答案