vimuth vimuth - 6 months ago 42
PHP Question

Error: Cannot access protected property AdminBundle\Entity\User::$id

I'm trying to use findUserBy() method of FOS-User Bundle inside sonata admin class. But I'm having this error. (I added get public function getId() but still the same result.) And can't remove protected of id since it gives a compile error.

This is my User class,

<?php

// src/AppBundle/Entity/User.php

namespace AdminBundle\Entity;

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

/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser {

/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @var string
*
* @ORM\Column(name="telephone", type="text", length=30, nullable=false)
*/
private $telephone;

/**
* Set nonotification
*
* @param text $telephone
* @return User
*/
public function settelephone($telephone) {
$this->telephone = $telephone;

return $this;
}

/**
* Get telephone
*
* @return text
*/
public function gettelephone() {
return $this->telephone;
}

/**
* @var string
*
* @ORM\Column(name="name", type="text", length=60, nullable=false)
*/
private $name;

/**
* Set nonotification
*
* @param text $name
* @return User
*/
public function setname($name) {
$this->name = $name;

return $this;
}

/**
* Get name
*
* @return text
*/
public function getname() {
return $this->name;
}

/**
* @var string
*
* @ORM\Column(name="surname", type="text", length=60, nullable=false)
*/
private $surname;

/**
* Set nonotification
*
* @param text $name
* @return User
*/
public function setsurname($surname) {
$this->surname = $surname;

return $this;
}

/**
* Get surname
*
* @return text
*/
public function getsurname() {
return $this->surname;
}

public function __construct() {
parent::__construct();
// your own logic
}

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

}


This is my admin class,

<?php

namespace AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class UserAdmin extends Admin {

public function postPersist($object) {
$container = $this->getConfigurationPool()->getContainer();
$userManager = $container->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('id' => $object->id));
$user->setPlainPassword('test');
$userManager->updateUser($user);
}


This is the error I got,


Error: Cannot access protected property AdminBundle\Entity\User::$id


And this is the line of error,


$user = $userManager->findUserBy(array('id' => $object->id));

Answer

change your line of error to following and see whether it works

$user = $userManager->findUserBy(array('id' => $object->getId()));

hope it helps. let us know if it does not...

Comments