Extended FOSUserBundle Not Creating Base Entities - php

Our project has been migrated from Symfony 2.1 to 2.3 and now to 2.7. FOSUserBundle created it's own properties on 2.1 but does not on the later editions.
php app/console doctrine:schema:update actually results in commands to drop the existing username, usernameCanonical, etc., fields.
Additionally, when trying to log in using our log in form, we get:
"exception":"[object] (Symfony\\Component\\Security\\Core\\Exception\\AuthenticationServiceException(code: 0): Unrecognized field: usernameCanonical at .../vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:94
It appears the system has no knowledge of the FOSUserBundle entities that accompany our extended base user.
I have reduced our extended user class to the default as a test and still get the same issues. Here is the current code and configuration:
config.yml
orm:
#default_entity_manager: ~
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
result_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
metadata_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
query_cache_driver:
type: memcached
host: 127.0.0.1
port: 11211
instance_class: Memcached
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
mappings:
FOSUserBundle: ~
fos_user:
db_driver: orm
firewall_name: main
user_class: ODR\OpenRepository\UserBundle\Entity\User
change_password:
form:
type: odr_user_change_password
security.yml
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
# Symfony >= 2.8
# csrf_token_generator: security.csrf.token_manager
# Symfony < 2.8
csrf_provider: form.csrf_provider
remember_me:
key: asdlkjf2ji802fiuaskdjokjhafsdjsfdjhjh
lifetime: 1209600
path: /
domain: ~
User Class [ src/ODR/OpenRepository/UserBundle/Entity/User.php ]
namespace ODR\OpenRepository\UserBundle\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
}
}
Running:
php app/console doctrine:mapping:info
does not show the FOS User or Group entities as being mapped. It does show our user class but the extension does not appear to pull in the base properties.
Thank you for your assistance.

As is usually the case when having upgrade problems, the issue turned out to be an incorrect dependency.
Updating the composer.json to include all the correct dependencies as shown in the reference composer.json:
https://github.com/symfony/symfony-standard/blob/2.7/composer.json
solved the issue. Our doctrine version was not correct and was incorrectly identifying the FOSUserBundle parent class as "transient" so that it was not compiled into the metadata array.
After updating the composer.json file and running composer update all schemas generated correctly and login functionality was restored.

Did you add the fos routing to routing config ?

Related

Symfony table entity has itself recognized as repository as well

New to both Symfony and StackOverflow as an OP.
I'm on a way adding new mysql table entities and repositories on Symfony(4.3.2) and get stuck as I test it.
I really appreciate someone's help.
The error I got was
[2020-07-13 03:25:59] request.CRITICAL: Uncaught PHP Exception RuntimeException: "The "App\Entity\Foo" entity has a repositoryClass set to "App\Repository\Foo", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service"." at /var/www/html/bar_project/vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php line 66 {"exception":"[object] (RuntimeException(code: 0): The \"App\\Entity\\Foo\" entity has a repositoryClass set to \"App\\Repository\\Foo\", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with \"doctrine.repository_service\". at /var/www/html/bar_project/vendor/doctrine/doctrine-bundle/Repository/ContainerRepositoryFactory.php:66)"} []
where Foo at /App/Entity/ is an entity for the table foo.
and the problem is that it tries to refer to Foo at App/Repository/ as its repository which doesn't exist, but not FooRepository which exists.
(Updated this paragraph following the comment from Jakumi)
(FooController.php)
$repository = $this->getDoctrine()->getRepository(Foo::class);
(Foo.php)
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Table(name="foo")
* #ORM\Entity(repositoryClass="App\Repository\FooRepository") // *1
*/
class Foo
{
...
(FooRepository.php)
<?php
namespace App\Repository;
use App\Entity\Foo;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* #method Foo|null find($id, $lockMode = null, $lockVersion = null)
* ...
*/
class FooRepository extends ServiceEntityRepository
{
...
One thing I find mysterious is that the same error log still shows up even if I remove *1.
(I think I have yarn watch and cache clear appropriately working.)
Additional Info 1
(doctrine.yaml)
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '8.0'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
# dbname:
# host:
# port:
# user:
# password:
url: '%env(resolve:DATABASE_URL)%'
options:
1002: 'SET sql_mode=(SELECT REPLACE(##sql_mode, "ONLY_FULL_GROUP_BY", ""))'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver: apcu
result_cache_driver: apcu
query_cache_driver: apcu
connection: ~
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
dql:
string_functions:
group_concat: DoctrineExtensions\Query\Mysql\GroupConcat
date_format: DoctrineExtensions\Query\Mysql\DateFormat

Symfony Sonata Media Bundle

Situation
I am using Symfony (and MongoDB as DB) and want to integrate the Sonata Media Bundle in to the Sonata Admin bundle. Prior to adding the media bundle, everything was working great.
I followed the guide on the Sonata site and have appeared to setup the media admin bundle correctly; please see config below
config.yml
doctrine_mongodb:
connections:
default:
server: mongodb://localhost:27017
options: {}
default_database: test_database
document_managers:
default:
mappings:
ApplicationSonataMediaBundle: ~
SonataMediaBundle: ~
auto_mapping: true
...
sonata_media:
# if you don't use default namespace configuration
#class:
# media: MyVendor\MediaBundle\Entity\Media
# gallery: MyVendor\MediaBundle\Entity\Gallery
# gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
default_context: default
db_driver: doctrine_mongodb # or doctrine_mongodb, doctrine_phpcr
contexts:
default: # the default context is mandatory
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
Company.php Document class
namespace AppBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* #MongoDB\Document
*/
class Company
{
/**
* #MongoDB\Id
*/
protected $id;
/**
* #MongoDB\String
*/
protected $title;
/**
* #MongoDB\String
*/
protected $slug;
/**
* #MongoDB\ReferenceMany(targetDocument="Application\Sonata\MediaBundle\Document\Media", mappedBy="image")
*/
protected $logo;
companyadmin.php
->add('logo', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'default')))
Issue
Now, whenever I go in to the company admin interface, I'm greeted by the message "No document manager defined for class Doctrine\ODM\MongoDB\PersistentCollection"
I'm relatively new to Symfony and I don't really see what is going wrong. Please can you help give a poke in the right direction. If I remove the logo from admin, naturally it starts working again. Please help, really clueless here
If you use: auto_mapping: true you don't need document manager default mappings, otherwise you have to add in your config file:
orm:
# auto_mapping: true
entity_managers:
default:
connection: default
# naming_strategy: doctrine.orm.naming_strategy.underscore
mappings:
ApplicationSonataUserBundle: ~
ApplicationSonataMediaBundle: ~
YourBundle: ~
Check in AppKernel.php for the ApplicationSonataMediaBundle() instance run the command:
run app/console doctrine:schema:update --force
Take a look to this question too:
How to define/pass the entity manager for sonata-admin

Symfony, security.yml "Unknown Entity namespace alias"

I would like to implement Symfony security into my project. Problem is I have to use two databases (one for admins, second for users). I have configured config.yml :
orm:
auto_generate_proxy_classes: %kernel.debug%
default_entity_manager: default
entity_managers:
default:
connection: system
mappings:
FinanceCmsBundle: ~
admin:
connection: admin
mappings:
FinanceCzBundle: ~
and set my security.yml :
security:
encoders:
Finance\CmsBundle\Entity\AuthUser:
algorithm: md5
encode_as_base64: false
iterations: 0
Finance\CzBundle\Entity\SystemAuthMailCentrum:
algorithm: md5
encode_as_base64: false
iterations: 0
providers:
administrators:
entity:
class: FinanceCmsBundle:AuthUser
property: username
ussers:
entity:
class: FinanceCzBundle:SystemAuthMailCentrum
property: username
Before I started I tested it on a copy of a table on a database under FinanceCmsBundle:SystemAuthMailCentrum and it worked correctly.
Now aplication ends with "Doctrine\ORM\ORMException
Unknown Entity namespace alias 'FinanceCzBundle'"
I have tried to switch them (FinanceCzBundle with FinanceCmsBundle) in config.yml and it looks like the aplication knows only the default Bundle set in config.yml. Because after this switch it does not find FinanceCmsBundle.
So I have tried to use full routes like:
providers:
administrators:
entity:
class: Finance\CmsBundle\Entity\AuthUser
property: username
ussers:
entity:
class: Finance\CzBundle\Entity\SystemAuthMailCentrum
property: username
After that I get different errors: "Doctrine\Common\Persistence\Mapping\MappingException
The class 'Finance\CzBundle\Entity\SystemAuthMailCentrum' was not found in the chain configured namespaces Finance\CmsBundle\Entity"
Thanks a lot for any answer.
P.S : It would be easier to use one database, but the code is old and I would have to fix it on many places.
Try to set the entity manager to providers, I think it should help:
providers:
administrators:
entity:
class: FinanceCmsBundle:AuthUser
property: username
ussers:
entity:
class: FinanceCzBundle:SystemAuthMailCentrum
property: username
manager_name: admin

"Class XXX is not a valid entity or a mapped super class" error when using php app/console

I'm following this Symfony2/Doctrine guide and I've come to the part where I need to create getters/setters. but I am stuck with this part:
$ php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Job
I've searched the net for possible solutions (seems to mainly revolve around using 2 asterisks for the start) but couldn't find the solution.
Some info:
Bundle is properly loaded (via AppKernel.php) as I have a test "hello world" and that works.
The namespace path is correct
Job.php exists in the right folder
I'm using postgres as my database. I'm not sure if this matters.
I have tried with and without the use Doctrine\ORM\Mapping line in model class (see below for code)
I don't think I'm running a accelerator at least according to get_loaded_extensions function
Any ideas would be very helpful.
Thanks a lot :)
snippet of my settings.yml
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
Model class
<?php
// src/MyApp/MyBundle/Model/Job.php
namespace MyApp\MyBundle\Model;
use Doctrine\ORM\Mapping as ORM;
/**
* MyApp\MyBundle\Model\Job
*
* #ORM\Entity
* #ORM\Table(name="myschema.jobs")
*/
class Job {
/**
* #ORM\Column(name="job_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $jobid;
/**
* #ORM\Column(name="name", type="text")
*/
protected $name;
/**
* #ORM\Column(name="job_desc", type="text")
*/
protected $description;
/**
* #ORM\Column(name="personal_req", type="text")
*/
protected $requirements;
}
did you create your entity using doctrine? I saw on your Job.php entity you are using annotation as mapping format.
In error output it said doctrine can't find any mapped entities. I've been there and it solved with specific configuration of your config.yml.
Try to change this on your config.yml
doctrine:
dbal:
driver: "%database_driver%"
#etc
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: false
mappings:
MyAppMyBundle:
type: annotation #On your case it should be annotation
dir: Resources/Model/
Read this maybe it can help:
Doctrine Mapping in Symfony2 using YAML
Have you tried using following console command ?
$ php app/console doctrine:generate:entities MyApp/MyBundle/Model
I hope it will work. Your entity namespace is different than Acme/StoreBundle/Entity/Product. Your entities are in Model directory/namespace so you should use first argument of command a valid namespace. So that following cmd generates error as you have mentioned above.
$ php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product

Symfony2 - Fatal error with Bundles

I've got the error [Solution at the end of the question]
Fatal error: Class 'symblog\Blogger\BlogBundle\SymblogBundle' not found in
/var/www/Symfony/app/AppKernel.php on line 20
I founded the question How to install or integrate bundles in Symfony2, but the solutions given didn't help me, because I already did what is suggested there. I'm following the tutorial symblog.co.uk except that I created at
app/config/routing.yml
a
*.php resource
Thanks in advance!
I have to add that while registering the bundle by console I've got the error
The command was not able to configure everything automatically.
You must do the following changes manually.
And the instructions:
- Edit the app/autoload.php file and register the bundle
namespace at the top of the registerNamespaces() call:
'symblog\Blogger\BlogBundle' => '/var/www/Symfony/blog',
which I followed.
AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
//..
new symblog\Blogger\BlogBundle\SymblogBundle(),
);
/app/config/routing.yml
SymblogBundle:
resource: "#SymblogBundle/Resources/config/routing.php"
prefix: /
As requested: /app/config/config.yml
imports:
- { resource: parameters.ini }
- { resource: security.yml }
framework:
#esi: ~
#translator: { fallback: %locale% }
secret: %secret%
charset: UTF-8
router: { resource: "%kernel.root_dir%/config/routing.yml" }
form: true
csrf_protection: true
validation: { enable_annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: %locale%
auto_start: true
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
# java: /usr/bin/java
filters:
cssrewrite: ~
# closure:
# jar: %kernel.root_dir%/java/compiler.jar
# yui_css:
# jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
jms_security_extra:
secure_controllers: true
secure_all_services: false
[Edit] The solution was very easy after the answer from #Clamidity that the bundles usually are located at src/Blogger/SymBlogBundle/BloggerSymBlogBundle.php
While the configuration using the console it saked about the location of the bundle and the default was /../src but I changed to /../blog. And of course it won't work, Symfony was looking into the wrong location. What I did was to move the folders inside /blog to /src and everything went fine.
There are a few things that it could be. I'll just cover anything I can think of.
Generally bundles are placed in the src folder. So the path to your bundle should look like this.
src/Blogger/SymBlogBundle/BloggerSymBlogBundle.php
(Notice that the bundle name follows the file name convention)
Inside of the BloggerSymBlogBundle.php make sure you have something similar to the following:
<?php
namespace Blogger\SymBlogBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class BloggerSymBlogBundle extends Bundle
{
}
(Notice that the same name convention is also followed here)
In your autoload.php the namespace that should be registered is the "Blogger" part of the name/path to your bundle. This is because the bundle itself resides in the Blogger folder:
'Blogger' => __DIR__.'/../src',
(Notice that the folder listed is the parent of the Blogger folder)
Now in the AppKernel.php register the bundle according the namespace your set up and registered:
new Blogger\SymBlogBundle\BloggerSymBlogBundle(),
*Note - Your resources and references to this bundle with the above configuration would be
BloggerSymBlogBundle
so your php routing would be called by using:
#BloggerSymBlogBundle/Resources/config/routing.php
There are different solution.
Clear the app/cache/(prod|dev) folder. When you have edited the Appkernel and autoload.
Fatal error: Class 'symblog\Blogger\BlogBundle\SymblogBundle' not found in
/var/www/Symfony/app/AppKernel.php on line 20
Here are now different Problems:
Look to the SymblogBundle.php in the Bundle Folder and look which namespace they use. Perhaps its really an uppercase ("S") like #kuba said.
The Class file is not in the folder or you have no rights to the folder that the interpreter can load the file.
I think your autoload.php is not correct.
'Avalanche' => __DIR__.'/../vendor/bundles',
This is the common way to register an Namespace. In your case it have to be
'Symlog' => '/var/www/Symfony/blog',
Here are the instructions from the "BloggerBundle" I hope its the correct one
There you can see the your autoload its not correct the Namespace is "Blogger".

Categories