wattever
wattever

Reputation: 611

FOS UserBundle -> Unable to create new user

This is the very first time I use Symfony2 by myself and I think I've made a mistake when configuring the FOS User Bundle. Looks like my User entity does not properly extend the FOS\UserBundle\Entity\User.

Here's my User class (basically the same as mentioned on the doc)

<?php
// src/Acme/UserBundle/Entity/User.php

namespace VillaPrivee\UserBundle\Entity;

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


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

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

Since I use Netbeans, I'm able to "Ctrl click" and make sure "FOS\UserBundle\Entity\User" exists. So far, I don't see anything wrong...

But when I try to create a new user using my terminal, I get this error:

Fatal error: Call to undefined method VillaPrivee\UserBundle\Entity\User::setUsername() 
in /Applications/MAMP/htdocs/VillaPrivee/vendor/friendsofsymfony/
user-bundle/FOS/UserBundle/Util/UserManipulator.php on line 50

Not sure what other details I should provide you guys with, just let me know if any other file could matter in this case.

Thanks for your help!

Edit :

<?php

namespace VillaPrivee\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class VillaPriveeUserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

config.yml:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: VillaPrivee\UserBundle\Entity\User

Upvotes: 2

Views: 4215

Answers (3)

Natan Rubinstein
Natan Rubinstein

Reputation: 665

I have also encountered this issue. This video really helped me. https://knpuniversity.com/screencast/fosuserbundle-ftw

in AppKernel.php

This was my mistake:

 $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
        new FOS\UserBundle\FOSUserBundle(),
        new \Acme\UserBundle\Entity\User()
    );

This is the problem new \Acme\UserBundle\Entity\User()

Follow the video and you will see that you need something like this

$bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
        new FOS\UserBundle\FOSUserBundle(),
        new \Acme\UserBundle\AcmeUserBundle()
    );

Here is the AcmeUserBundle class:

// src/Acme/UserBundle/AcmeUserBundle.php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
}

The UserBundle\Entity\User class is just like in the documentation

One last thing that I also saw on the view was the orm: entry in the config.yml Notice the auto_mapping: true

doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
    orm:
        default_entity_manager: default
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true

Hops this will help someone else

Upvotes: 0

wattever
wattever

Reputation: 611

Just in case someone gets the same issue as I did, i found out where the problem came from : I'm using a Mac but didn't want to get in trouble using the embedded PHP server. So I decided to use MAMP... Huge mistake! Everything I tried using my terminal was trying to access default PHP instead of MAMP one... Basically, I had to remove MAMP and upgrade my Mac PHP so I could use it together with Symfony. Sorry if I'm not crystal clear, but I don't really understand it all myself...

PS: For those using MAMP, don't be as stupid as I am : your project folder is stored INSIDE your MAMP application folder. So if you trash it, you'll trash all your projects at the same time... Yep, I'm still crying about it!

Upvotes: 0

Bartek
Bartek

Reputation: 1359

I think you extend wrong class, try with:

use FOS\UserBundle\Model\User as BaseUser;

Edit:

Yep, extending FOS\UserBundle\Entity\User is deprecated, Extend FOS\UserBundle\Model\User directly. Documentation

Upvotes: 6

Related Questions