Symfony2 Update Error.No mapping file - php

I am using Symfony2 and when i try to append FOSUserBundle, but i got an error
(bin/console doctrine:schema:update --force)
//---------------------------------------------------------------Appkernel.php
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
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 Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new main\kipeoBundle\kipeoBundle(),
new FOS\UserBundle\FOSUserBundle(),
];
//----------------------------------------------------app/config/config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: main\kipeoBundle\Entity\User
//----------------------------------------src-main>kipeoBundle>Entity>User.php
namespace main\kipeoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* #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
}
}
//----------------------------------------------------app/config/routing.yml
fos_user:
resource: "#FOSUserBundle/Resources/config/routing/all.xml"

In doctrine config, you need to specify the mapping type. In this case, "annotation"

Related

Symfony FOSUserBundle cannot override form, no effect

with this article I'm trying to override RegisterFormType.php, and do not see the result, but Symfony Profiler shows in Forms fos_user_registration is overwritten by app_user_registration.
Firstly, I have in AppKernel.php:
new FOS\UserBundle\FOSUserBundle(),
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
services.yml
app.form.registration:
class: Application\Sonata\UserBundle\Form\RegistrationFormType
tags:
- { name: form.type, alias: app_user_registration }
config.yml:
fos_user:
db_driver: orm # can be orm or odm
firewall_name: main
user_class: Application\Sonata\UserBundle\Entity\User
use_listener: true
group:
group_class: Application\Sonata\UserBundle\Entity\Group
group_manager: sonata.user.orm.group_manager # If you're using doctrine orm (use sonata.user.mongodb.group_manager for mongodb)
service:
user_manager: sonata.user.orm.user_manager # If you're using doctrine orm (use sonata.user.mongodb.user_manager for mongodb)
registration:
form:
type: app_user_registration
#validation_groups: [AppRegistration]
profile:
form:
name: app_user_profile
src/Application/Sonata/UserBundle/Form/RegistrationForm.php:
namespace Application\Sonata\UserBundle\Form;
use ...
class RegistrationFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('fullName')
;
}
public function getParent() {
return "fos_user_registration";
}
public function getName() {
return 'app_user_registration';
}
}
src/Application/Sonata/UserBundle/Entity/User.php:
namespace Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
class User extends BaseUser
{
/**
* #var int $id
*/
protected $id;
/**
* #var string
*/
protected $fullName;
/**
* #return string
*/
public function getFullName() {
return $this->fullName;
}
/**
* #param string $fullName
*/
public function setFullName($fullName) {
$this->fullName = $fullName;
}
/**
* Get id
*
* #return int $id
*/
public function getId() { return $this->id;}
}
As other way (I think not correct) tried to override RegistrationController.php in my custom directory, changed there $form = $this->container->get('app.form.registration') (alias for my form), but I receive error Attempted to call an undefined method named "createView" of class "Application\Sonata\UserBundle\Form\RegistrationFormType. After this I changed this class' parent extended class from ContentAware to Controller, and can see form and new field, but after submitting receive unknown error, even in Profiler not written about it name.
Symfony 2.8.18, FOSUserBundle 1.3.7. Thank you for your time.
Did you update the configuration of the FOSUserBundle in config.yml ?
fos_user:
# ...
registration:
form:
name: app_user_registration

Symfony not load translation

I have a Symfony web application written in Symfony 2.8.
All translations work correctly in dev mode ,But the translations of second language not loading in production mode.
If I enable debug in app.php , translations will load completely.
$kernel = new AppKernel('prod', false);
TO
$kernel = new AppKernel('prod', true);
But it is not a good choice.
My config.yml is:
parameters:
locale: fa
framework:
#esi: ~
translator: { fallbacks: ["%locale%" , en] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
I put the messages.fa.yml and messages.en.yml files in app directory.
Try follow my instructions:
1) Move your trans.yml files into src->nameBundle->Resources->translations
2) In src->nameBunndle create or update DependencyInjection folder
3) In DependencyInjection folder create Configuration.php and NameExtension.php where name is your name of bundle.
4) Code Configuration.php :
<?php
namespace NameBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {#inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('name'); // name you bundle
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
5) Code NameExtension.php :
<?php
namespace NameBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class NameExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

Error: Attempted to load class "ProductAdmin" from namespace "Admin\AdminBundle\Admin"

I'm trying to configure Sonata admin bundle, but I'm having this error:
Attempted to load class "ProductAdmin" from namespace
"Admin\AdminBundle\Admin". Did you forget a "use" statement for
another namespace?
protected function getSonata_Admin_ProductService() {
$instance = new \Admin\AdminBundle\Admin\ProductAdmin('sonata.admin.product', 'Admin\AdminBundle\Entity\Product', 'SonataAdminBundle:CRUD');
$instance->setTranslationDomain('AdminAdminBundle');
$instance->setFormTheme(array(0 => 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig'));
}
Someone have an idea how to sloved this error ?
Thank you
Config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: #AdminAdminBundle/Resources/config/admin.yml }
# app/config/config.yml
sonata_block:
default_contexts: [cms]
blocks:
# Enable the SonataAdminBundle block
sonata.admin.block.admin_list:
contexts: [admin]
# Your other blocks
Admin.yml
services:
sonata.admin.Product:
class: Admin\AdminBundle\Admin\ProductAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Content", label: "Product" }
arguments:
- ~
- Admin\AdminBundle\Entity\Product
- ~
calls:
- [ setTranslationDomain, [AdminAdminBundle]]
ProductAdmin.php
<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php
namespace Admin\AdminBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class ProductAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Post Title'))
->add('author', 'entity', array('class' => 'Admin\AdminBundle\Entity\Product'))
->add('body') //if no type is specified, SonataAdminBundle tries to guess it
;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('author')
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title')
->add('slug')
->add('author')
;
}
}
AdminAdminExtension.php
<?php
namespace Admin\AdminBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class AdminAdminExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('admin.yml');
}
}
autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;
/**
* #var ClassLoader $loader
*/
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The error indicates that your class file could not be autoloaded properly. Check if the namespace matches your path, and when in doubt, add a file to the root of your project loading the class to check whether the autoload config works:
<?php
require_once 'vendor/autoload.php';
var_dump(class_exists('a\b\c'));
The fact that your source file states another path than your namespace structure indicates that your class is in the wrong place.

Error when creating config parameter in bundle

I obtained this error when I want to create a config paramter to a bundle.
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for
"mrc_morales_tyre"
This is the code:
app/config/config.yml
mrc_morales_tyre:
db_driver: orm
Configuration.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {#inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mrc_morales_tyre');
$supportedDrivers = array('orm');
$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->end()
->end();
return $treeBuilder;
}
}
TyreExtension.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Definition\Processor;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class TyreExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
}
}
TyreBundle.php
namespace MrcMorales\TyreBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use MrcMorales\TyreBundle\DependencyInjection\Compiler\ValidationPass;
class TyreBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());
}
}
ValidationPass is to load validation.yml in not usual folder and it works.
Thanks
Resolved:
Symfony by default expects than config var name is the name of extension, in this case tyre.
Then I need to change the Extension name to MrcMoralesTyreExtension but then we need override the method getContainerExtension() in:
TyreBundle.php
public function getContainerExtension()
{
return new MrcMoralesTyreExtension();
}

Extending FOSUserBundles Registration to have different fields stored in different tables?

I am new with Symfony2 and FOSUserBundle. I am using Symfony2 version 2.4.1. I would like to extend FOSUserBundle in a few ways. I want to add fields to the registration form, some fields i would like to add to the user table which contains username and password but i would like to add fields that get added to a different table. (I think i have to rewrite the registerController??)
Currently i have inherited FOSUserBundle into my own UserBundle. I have tried to simply add fields to the current registration form by following the documentation but i keep returning this error.
Could not load type "acme_user_registration"
// src/Fixie/UserBundle/Entity/User.php
<?php
namespace Fixie\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #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", length=255)
*
* #Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* #Assert\Length(
* min=3,
* max="255",
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
public function __construct()
{
parent::__construct();
// your own logic
}
}
I then add the name field to the form builder
// src/Fixie/UserBundle/Form/Type/RegistrationFormType.php
<?php
namespace Fixie\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
// add your custom field
$builder->add('name');
}
public function getName()
{
return 'acme_user_registration';
}
}
I then go on to declare the custom form type:
// src/Fixie/UserBundle/Resources/config/services.yml
services:
acme_user.registration.form.type:
class: Fixie\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: acme_user_registration }
Then updated config:
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Fixie\UserBundle\Entity\User
registration:
form:
type: acme_user_registration
//AppKernel.php
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$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 Fixie\WelcomeBundle\WelcomeBundle(),
new Fixie\UserBundle\UserBundle(),
new FOS\UserBundle\FOSUserBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
// Fixie/UserBundle/UserBundle.php
<?php
namespace Fixie\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class UserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
My main questions are:
Can anyone see an error with my current code?
In relation to extending the form to have multiple fields being stored in multiple tables. What are the step i have to take? Do i rewrite the logic of the controller and the form???
Any help would be very much appreciated.
Ok.
In appKerenl.php you have
new Fixie\UserBundle\UserBundle(),
But in service.yml
class: Acme\UserBundle\Form\Type\RegistrationFormType
Maybe it should be:
class: Fixie\UserBundle\Form\Type\RegistrationFormType
If in result of command app/console container:debug you can't see acme_user.registration.form.type it means that your service don't register and fos_user can't see you service, and can't find your form type.
My UserBundle store in Webmil/Joint/UserBundle. In UserBundle folder I have file WebmilJointUserBundle.php
My file:
<?php
namespace Webmil\Joint\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class WebmilJointUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
So I think that your problem in this file. Can you compare file and your file, and maybe you will find error.
With me it was that i had already a services.yml in app/config, so the one in my bundle was ignored. In the end I added the registry to this file
# app/config/services.yml
services:
# [other already existing services]
acme_user.registration.form.type:
class: Acme\UserBundle\Form\Type\RegistrationFormType
tags:
- { name: form.type, alias: acme_user_registration }

Categories