Uc.IT_samuel
Uc.IT_samuel

Reputation: 233

FatalErrorException: Error: Call to a member function getId() on a non-object in

FatalErrorException: Error: Call to a member function getId() on a non-object in /home/project/src/Sample/MainBundle/Controller/SampleLogController.php line 19

I am using Symfony v.2.3 and I kept getting this error today.

Here is my controller:

public function NewAction(Request $request){
    $user = $this->getUser()->getId();
    //var_dump($user);
    $repository = $this->getDoctrine()
    ->getRepository('SampleMainBundle:SampleLog');
    $sample_repository = $repository->findByUser($user);
    //...
}

MyEntities:

class SampleLog
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
    * @var integer
    *
    * @ORM\Column(name="user", type="integer")
    */
    protected $user;

    //...

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


     public function setUser($user)
    {
        $this->user = $user;

        return $this;
    }

   /**
     * Get userid
     *
     * @return integer 
     */
    public function getUser()
    {
        return $this->user;
    }

    //...
}

Upvotes: 1

Views: 607

Answers (1)

Suf_Malek
Suf_Malek

Reputation: 666

In guest user OR not authenticated user in action "NewAction" You will get this error. You have to check first if user object exists or not, than redirect to other action OR do you logic.

Upvotes: 1

Related Questions