I'm using FOSUser bundle and everything went ok until I tried to create the database entities, I get this error:
It has nothing to do with the bug in Symfony 2.5, I'm using version 2.7
Case mismatch between loaded and declared class names: App\UserBundle\Entity\user vs App\UserBundle\Entity\User
This is my config file for fos:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: App\UserBundle\Entity\User
My classes and namespaces they seem to have the right caps, I don't know why this is happening. I tried to clear the cache, no errors but didn't work.
The user class is App/UserBundle/Entity/User.php, here's the content:
<?php
namespace App\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
/**
* User
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
}
What happens if you amend your User entity to appear as below (replacing user_table with intended table name)?
namespace App\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* User
* #ORM\Table(name="user_table")
* #ORM\Entity
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
}
}
I found the problem, I was using yaml for the entity definition and the filename was user.orm.yml instead of User.orm.yml.
Related
I cannot get the FOS User Bundle to work in Symfony 2.5. It's probably so simple I'm overlooking something basic. I've used this bundle before without issues and cannot find a solution online. I have everything set up and can't clear the cache -- I get this error. It also displays the error on the browser. Please be advised I am only a few weeks into learning Symfony! I followed these instructions to the letter.
Here is the error I receive:
InvalidConfigurationException: Unrecognized options "0, 1, 2" under
"fos_user"
What are the options "0, 1, 2" it is referring to? I thought maybe it was a routing issue, failing to bring in a query variable but I'm not sure.
Here is the relative part of config.yml:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
fos_user:
- db_driver: orm
- firewall_name: main
- user_class: Main\Bundle\ToolsBundle\Entity\User
Yes, it's registered in the AppKernel.php:
new FOS\UserBundle\FOSUserBundle(),
User.orm.yml:
# Main/Bundle/ToolsBundle/Resources/config/doctrine/User.orm.yml
Main\Bundle\ToolsBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
generator:
strategy: AUTO
The User.php entity is straight from the demo:
// Main/Bundle/ToolsBundle/Entity/User.php
namespace Main\Bundle\ToolsBundle\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;
public function __construct()
{
parent::__construct();
// your own logic
}
}
Remove - from fos_user config(config.yml). It should look like this:
fos_user:
db_driver: orm
firewall_name: main
user_class: Main\Bundle\ToolsBundle\Entity\User
I am using FOSUserBundle in Symfony 2.3, I created an additional table for store the user profile, at some point, I need to access to the entity manager to persist and flush the data coming from the profile form to the profile table, but I am having problems trying to get an instance of the entity manager.
Any idea?
Thanks!
To edit profiles of users using the FOSUserBundle, you have to create a new ProfileController which extends the ProfileController of FOSUserBundle.
Here you can now override methods of the FOSUserBundle.
An exemple of start :
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Acme\UserBundle\Controller;
use FOS\UserBundle\Model\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Controller\ProfileController as BaseController;
/**
* Controller managing the user profile
*
* #author Christophe Coevoet <stof#notk.org>
*/
class ProfileController extends BaseController
{
public function editAction()
{
$em = $this->container->get('doctrine')->getManager();
// your code here
}
}
You can access to the Doctrine service this way :
$em = $this->container->get('doctrine')->getManager();
The controllers of the FOSUserBundle extend from Symfony\Component\DependencyInjection\ContainerAware. So you have access to the Symfony\Component\DependencyInjection\ContainerInterface through $this->container. With this container you can access doctrine and subequently the EntityManager:
$doctrine = $this->container->get('doctrine');
$em = $doctrine->getManager();
I want to add an address and telephone number using FOSUserBundle. How can I add a Custom fields, with FOSuserBundle, to have a profile that contains address and telephone number....
Create an own user bundle and in the MyCompanyUserBundle.php you set
public function getParent(){
return 'FOSUserBundle';
}
Then in your new UserBundle you create a User entity and let it extend from the base user of the FOS user bundle:
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
public function __toString(){
return $this->firstname . ' ' . $this->lastname . ' (' . $this->email . ')';
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
EDIT
Set the user in the config as well:
fos_user:
db_driver: orm
firewall_name: main
user_class: MyCompany\UserBundle\Entity\User
the problem is in :
when i follow the symfony documentation:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md in step a) Doctrine ORM User class
i have created a folder doctrine contains User.orm.yml:
src/Acme/UserBundle/Resources/config/doctrine/User.orm.yml
Acme\UserBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
generator:
strategy: AUTO
this file crush any update in your entities and update of your database shemea .The solution is to delete this folder.
You don't have to add custom fields but you have to create your own user model.
Read the doc, all is explained:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-3-create-your-user-class
I want to extend default user entity attributes. In database after extending FOSUserBundle with SonataUserBunle there are 2 tables for storing users: fos_user and fos_user_user. I want to extend fos_user
Here is app/AppKernel.php:
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
in app/config.yml file I set:
fos_user:
db_driver: orm<br>
firewall_name: main<br>
user_class: Acme\DemoBundle\Entity\User<br>
group:<br>
group_class: Application\Sonata\UserBundle\Entity\Group<br>
sonata_user:
security_acl: true
class:
user: Acme\DemoBundle\Entity\User
admin:
user:
class: Acme\DemoBundle\Admin\UserAdmin
parameters:
sonata.user.admin.user.class: Blogger\BlogBundle\Admin\UserAdmin
sonata.user.admin.user.entity: Blogger\BlogBundle\Entity\User
doctrine:
orm:
auto_mapping: auto
SonataUserBundle is created in src/Application/Sonata/UserBundle
To override SonataUserBundle, i extended UserAdmin by creating another UserAdmin class in src/Acme/DemoBundle/Admin/UserAdmin and all works fine
Now I want to extend User entity (fos_user table) to add new attributes.
Here is my Acme/Demo/Entity/User.php that I want to extend the default User entity
namespace Acme\DemoBundle\Entity;
use FOS\UserBundle\Entity\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;
/*
* #ORM\Column(type="string", name="newAttribute")
*/
protected $newAttribute;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
What should I do to extend default fos_user table with php app/console doctrine:generate:entities Acme/DemoBundle
I've read many posts about this issue but none of the solutions helped me. I tried to extend Model class instead of Entity class but also nothing changed
You don't have to.
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle')
Here your are telling the SonataUserBundle to use the FOSUserBundle as well.
Then in your User entity you set:
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User extends BaseUser
Because you have said to the SonataUserBundle "use the fosuserbundle" and you extend from the sonata user bundle user entity he's gonna "merge" those 2 models into 1 and add your custom fields from in your user entity as well.
I've a brand new Symfony2 application and I'm 100% sure I've followed http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes to the letter. However I'm getting the following error;
Entity 'Core\MainBundle\Entity\LandingPromotions' has no field 'promotionType'. You can therefore not call 'findByPromotionType' on the entities' repository
I've no clue as to what I've missed. Code as follows;
LandingPromotions
<?php
namespace Core\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Core\MainBundle\Entity\LandingPromotions
*
* #ORM\Entity(repositoryClass="Core\MainBundle\Entity\PromotionRepository")
* #ORM\Table(name="landing_promotions")
*/
class LandingPromotions
{
//Normal entity stuff
}
PromotionRepository.php
namespace Core\MainBundle\Entity;
use Doctrine\ORM\EntityRepository;
class PromotionRepository extends EntityRepository
{
public function findByPromotionType($typeId)
{
}
}
Controller
namespace Core\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Core\MainBundle\Entity\Game;
use Core\MainBundle\Entity\LandingPromotions as LandingPromotions;
class GameController extends Controller
{
/**
* #Route("{_locale}/games/")
* #Template()
*/
public function indexAction($_locale)
{
$lp_repo = $this->getDoctrine()->getRepository('CoreMainBundle:LandingPromotions');
$lp_repo->findByPromotionType(2);
}
}
After much hair pulling and cursing I discovered that the entities are not using PHP annotations, but rather the XML versions. How this got mixed up I've no idea, but that'll be tomorrow's issue.