Php 扩展FOSUserBundles注册以在不同的表中存储不同的字段?
我是Symfony2和FOSUserBundle的新手。我使用的是Symfony2 2.4.1版。我想从几个方面扩展FOSUserBundle。我想向注册表中添加字段,一些字段我想添加到包含用户名和密码的用户表中,但我想添加添加到其他表中的字段。(我想我必须重写registerController??) 目前,我已将FOSUserBundle继承到自己的UserBundle中。我试图通过遵循文档将字段添加到当前注册表中,但我一直返回此错误 无法加载类型“acme\u用户注册” //src/Fixie/UserBundle/Entity/User.phpPhp 扩展FOSUserBundles注册以在不同的表中存储不同的字段?,php,forms,symfony,fosuserbundle,extending,Php,Forms,Symfony,Fosuserbundle,Extending,我是Symfony2和FOSUserBundle的新手。我使用的是Symfony2 2.4.1版。我想从几个方面扩展FOSUserBundle。我想向注册表中添加字段,一些字段我想添加到包含用户名和密码的用户表中,但我想添加添加到其他表中的字段。(我想我必须重写registerController??) 目前,我已将FOSUserBundle继承到自己的UserBundle中。我试图通过遵循文档将字段添加到当前注册表中,但我一直返回此错误 无法加载类型“acme\u用户注册” //src/Fix
<?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
}
}
然后更新配置:
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');
}
}
好的
在appKerenl.php中
new Fixie\UserBundle\UserBundle(),
但是在service.yml中
class: Acme\UserBundle\Form\Type\RegistrationFormType
也许应该是:
class: Fixie\UserBundle\Form\Type\RegistrationFormType
如果在命令app/console container:debug
的结果中,你看不到acme\u user.registration.form.type
,这意味着你的服务没有注册,fos\u user
看不到你的服务,也找不到你的表单类型
我的UserBundle存储在Webmil/Joint/UserBundle
中。在UserBundle
文件夹中,我有一个文件WebmilJointUserBundle.php
我的档案:
<?php
namespace Webmil\Joint\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class WebmilJointUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
因为我在app/config中已经有一个services.yml,所以包中的那个被忽略了。最后,我将注册表添加到此文件中
# 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 }
在控制台应用程序/控制台容器中执行:调试。您能在输出中看到您的表单服务acme\u user.registration.form.type
吗?不,我看不到。再次感谢您的帮助您可以发布您的AppKernel.php吗?将其添加到那里@denys281您是否正在加载DI中的services.yml
文件?实体/模型不会成为问题,因为它们不会在请求表单时加载,并且捆绑包不需要将“FOSUserBundle”作为父项就可以重写表单,这只对控制器之类的东西才是真正必要的,模板和路由。我将其更改为Fixie\UserBundle\Form\Type\RegistrationFormType,但在运行应用程序/控制台容器:debug时,我仍然无法看到acme\u user.registration.Form.Type。我修改了上面的代码以反映这一点。因此,如果我将服务部分放在我的app/config.yml中,我可以在转到app/console container:debug时看到它,因此它只是在捆绑包中时不注册服务。@bowseph首先我认为我们需要,我想我知道你的问题出在哪里,为什么即使从app/config你的服务也不能工作。你能发布文件Fixie/UserBundle/fixieeuserbundle.php
的内容吗?我已经从我的项目中添加了等效的文件,看不出有任何区别。我试着制作一个新的包,只是为了测试我是否可以从那里注册一个包,并且我能够。我是否应该创建一个新的包并将所有文件移到其中,然后看看它是否会注册?谢谢你的帮助!尝试将文件从UserBundle.php
重命名为fixeuserbundle.php
。是的,您可以创建新的捆绑包。我遇到了同样的问题,花了将近1个小时才找到这个问题。)
class: Fixie\UserBundle\Form\Type\RegistrationFormType
<?php
namespace Webmil\Joint\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class WebmilJointUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
# 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 }