我是Symfony的初学者,在那里我被要求使用FOSUser创建一个包含一些信息的用户实体S一切都很顺利,直到我尝试执行以下行命令
php/bin console doctrine:schema:update --force
此时此刻,我出现了以下错误
[Doctrine\ORM\Mapping\MappingException]
在"UserBundle\entity\User#userRoles"中找不到目标实体UserBundle\ entity\userRoles。
我试图更改一些注释,但由于我真的不知道自己在做什么,这并不能真正起作用
以下是实体的片段
<?php
// src/UserBundle/Entity/User.php
namespace UserBundleEntity;
use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentValidatorConstraints as Assert;
/**
* @ORMEntity
* @UniqueEntity(fields="email", message="Email already taken")
* @UniqueEntity(fields="username", message="Username already taken")
*/
class User extends BaseUser{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
// /**
// * @ORMColumn(type="string", length=255, unique=true)
// * @AssertNotBlank()
// * @AssertEmail()
// */
// protected $email;
/**
* @var string
*
* @ORMColumn(name="prenom", type="string", length=255)
*/
private $prenom;
/**
* @var string
*
* @ORMColumn(name="nom", type="string", length=255)
*/
private $nom;
/**
* @var string
*
* @ORMColumn(name="pseudo", type="string", length=255)
*/
private $pseudo;
/**
* @var string
*
* @ORMColumn(name="telephone", type="string", length=10)
*/
private $telephone;
// /**
// *
// * @ORMColumn(type="string", length=64)
// */
// protected $password;
/**
* @ORMManyToMany(targetEntity="userRoles", inversedBy="user")
* @ORMJoinTable(name="userRoles")
*
*/
private $userRoles;
/**
* Set prenom
*
* @param string $prenom
*
* @return User
*/
public function setPrenom($prenom)
{
$this->prenom = $prenom;
return $this;
}
/**
* Get prenom
*
* @return string
*/
public function getPrenom()
{
return $this->prenom;
}
/**
* Set nom
*
* @param string $nom
*
* @return User
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set pseudo
*
* @param string $pseudo
*
* @return User
*/
public function setPseudo($pseudo)
{
$this->pseudo = $pseudo;
return $this;
}
/**
* Get pseudo
*
* @return string
*/
public function getPseudo()
{
return $this->pseudo;
}
/**
* Set telephone
*
* @param string $telephone
*
* @return User
*/
public function setTelephone($telephone)
{
$this->telephone = $telephone;
return $this;
}
/**
* Get telephone
*
* @return string
*/
public function getTelephone()
{
return $this->telephone;
}
/**
* Add userRole
*
* @param UserBundleEntityRole $userRole
*
* @return User
*/
public function addUserRole(UserBundleEntityRole $userRole)
{
$this->userRoles[] = $userRole;
return $this;
}
/**
* Remove userRole
*
* @param UserBundleEntityRole $userRole
*/
public function removeUserRole(UserBundleEntityRole $userRole)
{
$this->userRoles->removeElement($userRole);
}
/**
* Get userRoles
*
* @return DoctrineCommonCollectionsCollection
*/
public function getUserRoles()
{
return $this->userRoles;
}
}
这个代码出了什么问题?提前感谢
注释中似乎有一个拼写错误,签名为setsetter/getter方法targetEntity不是userRoles
类,而是UserBundleEntityRole
类。
因此,更改定义如下:
/**
* @ORMManyToMany(targetEntity="UserBundleEntityRole", inversedBy="user")
* @ORMJoinTable(name="userRoles")
*
*/
private $userRoles;
希望这能帮助