Почему не до конца работает связь?

Есть 2 сущности, User и NoticeNotRepeat. Все до боли стандартно, у User может быть много Notice, у Notice может быть только один User. При таком запросе.
$user = $em->getRepository(User::class)->findOneBy(['id' => 1]);
        $notices = $user->getNoticeNotRepeat();
        foreach ($notices as $notice){
            dump($notice);
        }
        dd();

у User есть его Notice, и так же у Notice есть выход на User.
Вот дамп.
SiteController.php on line 27:
NoticeNotRepeat {#867 ▼
  -id: 1
  -user_id: 1
  -text_notice: "Placeat minus enim dolorum accusamus repellendus. In labore sed ut qui autem eos enim. Et sapiente eius voluptas porro molestiae. Qui ut quasi doloribus veritat ▶"
  -sending_at: DateTime @1551401480 {#865 ▶}
  -send: false
  -user: User {#887 ▼
    -id: 1
    -email: "ykoelpin@daugherty.info"
    -password: "$argon2i$v=19$m=1024,t=2,p=2$aDFIMU5RN0lySVpZSHZQbw$dVaPxzh3E4zNMYx0igXpqLzQbt2UQpHGQPyW6iEie5Q"
    -roles: array:1 [▶]
    -username: "sylvester77"
    -firstName: "Joel"
    -created_at: DateTime @1552071910 {#884 ▶}
    -updated_at: DateTime @1552071910 {#885 ▶}
    -notice_not_repeat: PersistentCollection {#943 ▶}
    -status_notice: false
  }
}

при запросе когда я выбираю Notice и у него пытаюсь взять User получаю такое вот.
$notice = $em->getRepository(NoticeNotRepeat::class)->find(['id' => 1]);
        dd($notice);

SiteController.php on line 33:
NoticeNotRepeat {#781 ▼
  -id: 1
  -user_id: 1
  -text_notice: "Placeat minus enim dolorum accusamus repellendus. In labore sed ut qui autem eos enim. Et sapiente eius voluptas porro molestiae. Qui ut quasi doloribus veritat ▶"
  -sending_at: DateTime @1551401480 {#779 ▶}
  -send: false
  -user: User {#844 ▼
    +__isInitialized__: false
    -id: 1
    -email: null
    -password: null
    -roles: []
    -username: null
    -firstName: null
    -created_at: null
    -updated_at: null
    -notice_not_repeat: null
    -status_notice: false
     …2
  }
}

и в чем прикол я не понимаю, мне кажется я уже в глаза долблюсь просто.
Модель User
/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\Table("`user`")
 * @UniqueEntity("email")
 */
class User implements UserInterface
{
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\NoticeNotRepeat", mappedBy="user")
     */
    private $notice_not_repeat;

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

    /**
     * @return Collection|NoticeNotRepeat[]
     */
    public function getNoticeNotRepeat(): Collection
    {
        return $this->notice_not_repeat;
    }
}


Код NoticeNotRepeat
class NoticeNotRepeat
{
    /**
     * @ORM\Column(name="user_id", type="integer", nullable=false, options={"unsigned"=true})
     */
    private $user_id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="notice_not_repeat")
     * @ORM\JoinColumn(nullable=false)
     */
    private $user;

    /**
     * @return User|null
     */
    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;
        return $this;
    }
}


Код моделей естественно не полный, и так много кода вышло, но думаю суть вопроса понятная, или я с запросом что то не так делаю или в модели что то не то, я не понимаю.
  • Вопрос задан
  • 297 просмотров
Решения вопроса 1
Делайте дамп $notice->getUser(), там всё будет. Это ленивая загрузка доктрины
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы