Search code examples
phpsymfonydoctrinefosuserbundlesymfony4

Symfony 4 - FOSUserBundle - Render user information on custom route


I have a constructor and route in my custom ProfileController

private $userManager;

public function __construct(UserManagerInterface $userManager)
{
    $this->userManager = $userManager;
}

/**
 * @Route("/profile/bookings", name="profile_bookings")
 */
public function bookings()
{

    $user = $this->getUser();

    return $this->render('profile/bookings/bookings.html.twig', array('user'=>$user));
}

And in my template I reference

{{ user.first_name }}

But I get the error:

HTTP 500 Internal Server Error Neither the property "first_name" nor one of the methods "first_name()", "getfirst_name()"/"isfirst_name()"/"hasfirst_name()" or "__call()" exist and have public access in class "App\Entity\User".

How do I get the user info from db and display in sub pages of profile?

Edit: User Entity ...

<?php

namespace App\Entity;

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

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

    /**
     * @ORM\Column(type="string", length=190)
     */
    private $first_name;

    /**
     * @ORM\Column(type="string", length=190)
     */
    private $last_name;

    /**
     * @ORM\Column(type="string", length=190, nullable=true)
     */
    private $phone_number;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $profile_height;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $profile_weight;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $profile_dob;

    /**
     * @ORM\Column(type="string", length=190, nullable=true)
     */
    private $profile_gender;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Booking", mappedBy="user")
     */
    private $bookings;

    public function __construct()
    {
        parent::__construct();
        $this->bookings = new ArrayCollection();
    }

    /**
     * Overridde setEmail method so that username is now optional
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->setUsername($email);

        return parent::setEmail($email);
    }

    public function getFirstName()
    {
        return $this->first_name;
    }

    public function setFirstName($first_name)
    {
        $this->first_name = $first_name;
    }

    public function getLastName()
    {
        return $this->last_name;
    }

    public function setLastName($last_name)
    {
        $this->last_name = $last_name;
    }

    public function getPhoneNumber(): ?string
    {
        return $this->phone_number;
    }

    public function setPhoneNumber(string $phone_number): self
    {
        $this->phone_number = $phone_number;

        return $this;
    }

    public function getProfileHeight(): ?int
    {
        return $this->profile_height;
    }

    public function setProfileHeight(?int $profile_height): self
    {
        $this->profile_height = $profile_height;

        return $this;
    }

    public function getProfileDob(): ?\DateTimeInterface
    {
        return $this->profile_dob;
    }

    public function setProfileDob(?\DateTimeInterface $profile_dob): self
    {
        $this->profile_dob = $profile_dob;

        return $this;
    }

    public function getProfileWeight(): ?int
    {
        return $this->profile_weight;
    }

    public function setProfileWeight(?int $profile_weight): self
    {
        $this->profile_weight = $profile_weight;

        return $this;
    }

    public function getProfileGender(): ?string
    {
        return $this->profile_gender;
    }

    public function setProfileGender(?string $profile_gender): self
    {
        $this->profile_gender = $profile_gender;

        return $this;
    }

    /**
     * @return Collection|Booking[]
     */
    public function getBookings(): Collection
    {
        return $this->bookings;
    }

    public function addBooking(Booking $booking): self
    {
        if (!$this->bookings->contains($booking)) {
            $this->bookings[] = $booking;
            $booking->setUser($this);
        }

        return $this;
    }

    public function removeBooking(Booking $booking): self
    {
        if ($this->bookings->contains($booking)) {
            $this->bookings->removeElement($booking);
            // set the owning side to null (unless already changed)
            if ($booking->getUser() === $this) {
                $booking->setUser(null);
            }
        }

        return $this;
    }

}

Thanks.


Solution

  • @Franck Gamess is right but you can also get rid of the get. If you write {{ user.firstName }}, twig will associate that to your method getFirstName() automatically.

    I don't know why you write your properties with snake_case but you could change it to camelCase and access your properties via their "real" name.