FosUserBundle:无法找到与 class "AppBundle\Entity\User" 的实体关联的对象管理器
FosUserBundle: Unable to find the object manager associated with an entity of class "AppBundle\Entity\User"
在我的 symfony 3.4 项目中,我有以下用户实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name): User
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname): User
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum): User
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization): User
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation): User
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}
我也相应地扩展了表格:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('organization',TextType::class,["label"=>"register.organization","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('occupation',TextType::class,["label"=>"register.position","required"=>false,'translation_domain' => 'FOSUserBundle']);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
另外,我抵制的扩展形式如下:
{% extends "@FOSUserBundle/layout.html.twig" %}
{% block title %} Register {% endblock title %}
{% block fos_user_content %}
{% trans_default_domain 'FOSUserBundle' %}
<div class="row clearfix"><div class="col"><h1 style="text-align:center">{{ 'register.title'|trans }}</h1></div></div>
<div class="row clearfix">
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register col'}}) }}
{% include "@FOSUser/Registration/register_content.html.twig" %}
<div class="form-row">
<div class="form-group col">
<div class="form-check">
<input class="form-check-input" name="acceptTerms" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">{{ 'register.acceptTerms'|trans }}</label>
</div>
</div>
<div class="form-row">
<input type="submit" class="btn btn-primary" value="{{ 'registration.submit'|trans }}" />
</div>
{{ form_end(form) }}
</row>
{% endblock fos_user_content %}
我也看过这个 and this piece of documentation 但是即使我的 orm 有以下配置,我仍然没有得到修复:
doctrine:
dbal:
driver: pdo_mysql
server_version: 5.7
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
types:
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
dir: 'Entity'
is_bundle: true
prefix: AppBundle\Entity
alias: AppBundle
但我收到以下错误:
Unable to find the object manager associated with an entity of class "AppBundle\Entity\User".
我也看过这个,但对解决方案没有任何了解。
你知道为什么会这样吗?我该如何解决?
编辑 1
我还尝试了以下学说配置,但没有得到任何预期的结果:
doctrine:
dbal:
driver: pdo_mysql
server_version: 5.7
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
types:
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
dir: 'Entity'
is_bundle: true
prefix: AppBundle\Entity
alias: AppBundle
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
编辑 2
根据要求,我对 fos_user
的设置是:
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
service:
mailer: fos_user.mailer.twig_swift
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
registration:
form:
type: AppBundle\Form\RegistrationType
你把@ORM\Entity放在了AppBundle\Entity\User
吗?
正如@dbrumann 提到的那样,您在 AppBundle\Entity\User
中遗漏了它
所以 class 应该这样定义:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name)
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname)
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum)
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization)
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation)
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}
在我的 symfony 3.4 项目中,我有以下用户实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name): User
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname): User
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum): User
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization): User
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation): User
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}
我也相应地扩展了表格:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('phone',PhoneNumberType::class,["label"=>"register.phonenum","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('organization',TextType::class,["label"=>"register.organization","required"=>false,'translation_domain' => 'FOSUserBundle'])
->add('occupation',TextType::class,["label"=>"register.position","required"=>false,'translation_domain' => 'FOSUserBundle']);
}
public function getParent()
{
return 'FOS\UserBundle\Form\Type\RegistrationFormType';
}
public function getBlockPrefix()
{
return 'app_user_registration';
}
// For Symfony 2.x
public function getName()
{
return $this->getBlockPrefix();
}
}
另外,我抵制的扩展形式如下:
{% extends "@FOSUserBundle/layout.html.twig" %}
{% block title %} Register {% endblock title %}
{% block fos_user_content %}
{% trans_default_domain 'FOSUserBundle' %}
<div class="row clearfix"><div class="col"><h1 style="text-align:center">{{ 'register.title'|trans }}</h1></div></div>
<div class="row clearfix">
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register col'}}) }}
{% include "@FOSUser/Registration/register_content.html.twig" %}
<div class="form-row">
<div class="form-group col">
<div class="form-check">
<input class="form-check-input" name="acceptTerms" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">{{ 'register.acceptTerms'|trans }}</label>
</div>
</div>
<div class="form-row">
<input type="submit" class="btn btn-primary" value="{{ 'registration.submit'|trans }}" />
</div>
{{ form_end(form) }}
</row>
{% endblock fos_user_content %}
我也看过这个
doctrine:
dbal:
driver: pdo_mysql
server_version: 5.7
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
types:
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
dir: 'Entity'
is_bundle: true
prefix: AppBundle\Entity
alias: AppBundle
但我收到以下错误:
Unable to find the object manager associated with an entity of class "AppBundle\Entity\User".
我也看过这个
你知道为什么会这样吗?我该如何解决?
编辑 1
我还尝试了以下学说配置,但没有得到任何预期的结果:
doctrine:
dbal:
driver: pdo_mysql
server_version: 5.7
host: '%database_host%'
port: '%database_port%'
dbname: '%database_name%'
user: '%database_user%'
password: '%database_password%'
charset: UTF8
types:
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
AppBundle:
type: annotation
dir: 'Entity'
is_bundle: true
prefix: AppBundle\Entity
alias: AppBundle
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
编辑 2
根据要求,我对 fos_user
的设置是:
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: main
user_class: AppBundle\Entity\User
service:
mailer: fos_user.mailer.twig_swift
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
registration:
form:
type: AppBundle\Form\RegistrationType
你把@ORM\Entity放在了AppBundle\Entity\User
吗?
正如@dbrumann 提到的那样,您在 AppBundle\Entity\User
所以 class 应该这样定义:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* @ORM\Column(type="string", length=25)
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max=25,
* minMessage="The surname is too short.",
* maxMessage="The surname is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $surname;
/**
* @ORM\Column(type="phone_number")
* @AssertPhoneNumber
*/
protected $phone;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $organization;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(
* min=0,
* max=255,
* groups={"Registration", "Profile"}
* )
*/
protected $occupation;
public function __construct()
{
parent::__construct();
}
public function setName(string $name)
{
$this->name=$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setSurname(string $surname)
{
$this->surname=$surname;
return $this;
}
public function getSurname()
{
return $this->name;
}
public function setPhone($phoneNum)
{
$this->phone=$phoneNum;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setOrganization($organization)
{
$this->organization=$organization;
return $this;
}
public function getOrganization()
{
return $this->organization;
}
public function setOccupation($occupation)
{
$this->occupation=$occupation;
return $this;
}
public function getOccupation()
{
return $this->occupation;
}
}