4

I'm working with the Sonata Admin bundle for the backend of my website.

My user table in my database looks like this:

id (PK)
username
email
password
salt
last_login
created_at
company_id (FK)

My User entity looks like the following:

<?php

namespace MyBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
//use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Table(name="user", indexes={@ORM\Index(name="fk_user_company1_idx", columns={"company_id"})})
 * @ORM\Entity
 */
class User extends BaseUser
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    protected $createdAt;

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

    /**
     * @var \MyBundle\Entity\Company
     *
     * @ORM\ManyToOne(targetEntity="MyBundle\Entity\Company")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     * })
     */
    protected $company;

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

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Get username
     *
     * @return string 
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set password
     *
     * @param string $password
     * @return User
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     * @return User
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;

        return $this;
    }

    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }

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

    /**
     * Set company
     *
     * @param \MyBundle\Entity\Company $company
     * @return User
     */
    public function setCompany(\MyBundle\Entity\Company $company = null)
    {
        $this->company = $company;

        return $this;
    }

    /**
     * Get company
     *
     * @return \MyBundle\Entity\Company 
     */
    public function getCompany()
    {
        return $this->company;
    }
}

Now when I go to the "Edit Account" page I get the following error:

Warning: call_user_func() expects parameter 1 to be a valid callback, class 'MyBundle\Entity\User' does not have a method 'getGenderList'

I don't have gender in my user table in my database. How can I fix this?

UPDATE:

When I change use FOS\UserBundle\Entity\User as BaseUser; to use Sonata\UserBundle\Entity\BaseUser as BaseUser;. Then I get the following error:

An exception occurred while executing 'SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS confirmation_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.created_at AS created_at17, t0.updated_at AS updated_at18, t0.date_of_birth AS date_of_birth19, t0.firstname AS firstname20, t0.lastname AS lastname21, t0.website AS website22, t0.biography AS biography23, t0.gender AS gender24, t0.locale AS locale25, t0.timezone AS timezone26, t0.phone AS phone27, t0.facebook_uid AS facebook_uid28, t0.facebook_name AS facebook_name29, t0.facebook_data AS facebook_data30, t0.twitter_uid AS twitter_uid31, t0.twitter_name AS twitter_name32, t0.twitter_data AS twitter_data33, t0.gplus_uid AS gplus_uid34, t0.gplus_name AS gplus_name35, t0.gplus_data AS gplus_data36, t0.token AS token37, t0.two_step_code AS two_step_code38, t0.id AS id39, t0.company_id AS company_id40 FROM user t0 WHERE t0.id = ? LIMIT 1' with params 1:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.updated_at' in 'field list'

How could I fix this?

nielsv
  • 6,540
  • 35
  • 111
  • 215
  • You have to implement the missing method into your class. That is all the error message says to you. – B001ᛦ Aug 28 '15 at 08:41
  • Had the same issue and after getting this sql error I had to delete tables related to this (fos_user, fos_group and fos_user_group), all migration files and create them again. Altering existing tables just didn't work. – MilanG Oct 30 '19 at 13:38

1 Answers1

3

Your User class extends the one from FOSUserBundle instead of SonataUserBundle. You have to change it:

//use FOS\UserBundle\Entity\User as BaseUser;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;

Check https://github.com/sonata-project/SonataUserBundle/issues/459

Edit: if you don't want to use this class (there are a lot of useless fields in it), then you have to create your own admin class for your User entity. This page has a full guide for this : http://m2mdas.github.io/blog/2013/11/18/integrate-fosuserbundle-and-sonatauserbundle-easily/

Update after your update : after changing the class your entity extends, you have to update your database schema to match your model :

php app/console doctrine:schema:update --force
Derek
  • 1,826
  • 18
  • 25
  • I've updated my topic, is there a way to bypass this without writing your own admin class? – nielsv Aug 28 '15 at 09:02
  • I've updated my answer. You either have to extend the proper class or write your own admin class. – Derek Aug 28 '15 at 09:21