M
M
Maxim Lagoysky2019-03-08 22:23:07
symfony
Maxim Lagoysky, 2019-03-08 22:23:07

Why isn't the connection fully working?

There are 2 entities, User and NoticeNotRepeat. Everything is painfully standard, a User can have many Notices, a Notice can only have one User. With such a request.

$user = $em->getRepository(User::class)->findOneBy(['id' => 1]);
        $notices = $user->getNoticeNotRepeat();
        foreach ($notices as $notice){
            dump($notice);
        }
        dd();

User has its Notice, and Notice also has an output to User.
Here is the dump.
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: "[email protected]"
    -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
  }
}

when requested, when I select Notice and try to take User from it, I get this.
$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
  }
}

and I don’t understand what’s the joke, it seems to me that I’m just pecking my eyes.
Model 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 Code
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;
    }
}

The code of the models is naturally not complete, and so much code came out, but I think the essence of the issue is clear, or I’m doing something wrong with the request, or something is wrong in the model, I don’t understand.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Skorokhodov, 2019-03-08
@lagoy

Dump $notice->getUser(), everything will be there. It's lazy loading doctrine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question