我在2个实体之间有很多与众不同的关系:agent&习惯。我在表单验证上遇到错误:
无法解析类的类型类的类型" ID" " CNAMTS RAVC Bundle Entity Agent"
一开始,我的经纪人和Habilitation $ id是$ ageId和$ habid,但我已经有同样的错误。我试图在两个实体中仅以$ id的价格更改,但是我仍然遇到此错误。
我做doctrine:schema:validate
[映射]失败 - 实体级'cnamts ravc bundle entity agent' 映射无效:
- 引用的列名" id"必须是目标实体类的主键列'cnamts ravc bundle entity entity agent'。
引用的列名'id'必须是目标实体类的主要键列'cnamts ravc bundle entity entity habilitation'。
[Doctrine dbal Schema Schemaexception]表名 '有效_comptes.agent_j_habilitation'已经存在。
我正在使用Symfony 2.8和学说2。
他是我的代码和BDD定义:
代理:
/**
* Agent
*
* @ORMTable(name="agent")
* @ORMEntity(repositoryClass="......BundleRepositoryAgentRepository")
*/
class Agent
{
/**
* @var integer
*
* @ORMId
* @ORMColumn(name="age_id", type="integer")
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="age_numero", type="string", length=5, nullable=true)
*/
private $ageNumero;
/**
* @var string
*
* @ORMColumn(name="age_nom", type="string", length=100, nullable=true)
*/
private $ageNom;
/**
* @var string
*
* @ORMColumn(name="age_prenom", type="string", length=100, nullable=true)
*/
private $agePrenom;
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Habilitation", inversedBy="agents")
* @ORMJoinTable(
* name="agent_j_habilitation",
* joinColumns={@ORMJoinColumn(name="fk_hab_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORMJoinColumn(name="fk_age_id", referencedColumnName="id", nullable=false)}
* )
*
*/
private $habilitations;
/**
* Agent constructor.
*/
public function __construct()
{
$this->habilitations = new ArrayCollection();
}
/**
* @param Habilitation $habilitation
*/
public function addHabilitation(Habilitation $habilitation)
{
$habilitation->addAgent($this);
$this->habilitations[] = $habilitation;
}
}
Habilitation:
/**
* Habilitation
*
* @ORMTable(name="habilitation")
* @ORMEntity
* @ORMEntity(repositoryClass="......BundleRepositoryHabilitationRepository")
*/
class Habilitation
{
/**
* @var integer
*
* @ORMId
* @ORMColumn(name="hab_id", type="integer")
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="hab_libelle", type="string", length=50, nullable=true)
*/
private $habLibelle;
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Agent", mappedBy="habilitations")
*/
private $agents;
/**
* Habilitation constructor.
*/
public function __construct()
{
$this->agents = new ArrayCollection();
}
/**
* @param Agent $agent
*/
public function addAgent(Agent $agent)
{
$this->agents[] = $agent;
}
}
ajoutusertype :
class AjoutUserType extends AbstractType
{
/**
* Constructor.
*
*/
public function __construct()
{
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ageNumero', 'text', [
'label' => 'Numéro'
])
->add('ageNom', 'text', [
'label' => 'Nom'
])
->add('agePrenom', 'text', [
'label' => 'Prénom'
])
->add('habilitations', EntityType::class, [
'class' => 'RAVCBundle:Habilitation',
'label' => 'Habilitation',
'property' => 'habLibelle',
'multiple' => true,
'placeholder' => '-- Sélectionnez une habilitation --',
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Agent::class
]
);
}
}
动作控制器:
/**
* @Route("/ajoutUser", name="ravc_ajoutUser")
* @Method({"GET", "POST"})
* @internal param Request $request
*/
public function newAgentAction(Request $request)
{
$agent = new Agent();
$form = $this->createForm(new AjoutUserType(), $agent);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($agent);
$em->flush();
$this->notification('L'agent a été ajouté avec succès.');
return $this->redirectToRoute('ravc_ajoutUser');
}
return $this->render('RAVCBundle:Admin:ajoutUser.html.twig', ['form' => $form->createView()]);
}
表定义:
CREATE TABLE `agent` (
`age_id` int(11) NOT NULL AUTO_INCREMENT,
`age_numero` varchar(6) DEFAULT NULL,
`age_nom` varchar(100) DEFAULT NULL,
`age_prenom` varchar(100) DEFAULT NULL,
PRIMARY KEY (`age_id`)
) ENGINE=InnoDB AUTO_INCREMENT=91 DEFAULT CHARSET=latin1;
CREATE TABLE `agent_j_habilitation` (
`ajh_id` int(11) NOT NULL AUTO_INCREMENT,
`fk_hab_id` int(11) NOT NULL,
`fk_age_id` int(11) NOT NULL,
PRIMARY KEY (`ajh_id`),
KEY `ajh_fk_hab_id` (`fk_hab_id`),
KEY `ajh_fk_age_id` (`fk_age_id`),
CONSTRAINT `ajh_fk_age_id` FOREIGN KEY (`fk_age_id`) REFERENCES `agent` (`age_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `ajh_fk_hab_id` FOREIGN KEY (`fk_hab_id`) REFERENCES `habilitation` (`hab_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=109 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Table structure for habilitation
-- ----------------------------
DROP TABLE IF EXISTS `habilitation`;
CREATE TABLE `habilitation` (
`hab_id` int(11) NOT NULL AUTO_INCREMENT,
`hab_libelle` varchar(50) DEFAULT NULL,
PRIMARY KEY (`hab_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
学说注释应反映真实的列名称(如架构验证的建议(,对于第一个(joinColumn
(,您应该定义开始实体数据(,因此请尝试以下操作:
/**
* @var ArrayCollection
*
* @ORMManyToMany(targetEntity="Habilitation", inversedBy="agents")
* @ORMJoinTable(
* name="agent_j_habilitation",
* joinColumns={@ORMJoinColumn(name="fk_age_id", referencedColumnName="age_id", nullable=false)},
* inverseJoinColumns={@ORMJoinColumn(name="fk_hab_id", referencedColumnName="hab_id", nullable=false)}
* )
*
*/
private $habilitations;
这应该修复doctrine:schema:validate
命令
ajoutusertype 该关系应由CollectionType
而不是EntityType