我有一对一的colletion,其中2个集合在起作用,但是cursuri(可变的名称)没有获得纪律的ID,null.laboratoare和引用正在工作为什么?我尝试了所有我看到的唯一区别是名字。纪律实体
namespace AppBundleEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
/**
* @ORMEntity
* @ORMTable(name="disciplina")
* @VichUploadable
*/
class Disciplina
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*/
private $id;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @VichUploadableField(mapping="banner_image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORMColumn(type="string", length=255)
*
* @var string
*/
private $imageName;
/**
* @ORMColumn(type="string")
*/
private $locatieLaborator;
/**
* @ORMColumn(type="string")
*/
private $descriere;
/**
* @ORMColumn(type="string")
*/
private $personal;
/**
* @ORMOneToMany(targetEntity="AppBundleEntityReferat",mappedBy="disciplina",cascade={"persist"})
*
*/
private $referate;
/**
* @ORMOneToMany(targetEntity="AppBundleEntityLaborator",mappedBy="disciplina",cascade={"persist"})
*
*/
private $laboratoare;
/**
* @ORMOneToMany(targetEntity="AppBundleEntityCurs",mappedBy="disciplina",cascade={"persist"})
*
*/
private $cursuri;
public function addLaboratoare(Laborator $laborator){
if($this->laboratoare->contains($laborator)){
return;
}
$this->laboratoare->add($laborator);
$laborator->setDisciplina($this);
return $this;
}
public function removeLaboratoare(Laborator $laborator){
if(!$this->laboratoare->contains($laborator)){
return;
}
$this->laboratoare->remove($laborator);
$laborator->setDisciplina(null);
}
public function addCursuri(Curs $curs){
if($this->cursuri->contains($curs)){
return;
}
$this->cursuri->add($curs);
$curs->setDisciplina($this);
return $this;
}
public function removeCursuri(Curs $curs){
if(!$this->cursuri->contains($curs)){
return;
}
$this->cursuri->remove($curs);
$curs->setDisciplina(null);
}
public function addReferate(Referat $referat){
if($this->referate->contains($referat)){
return;
}
$this->referate->add($referat);
$referat->setDisciplina($this);
return $this;
}
public function removeReferate(Referat $Laboratoare){
if(!$this->laboratoare->contains($Laboratoare)){
return;
}
$this->laboratoare->remove($Laboratoare);
$Laboratoare->setDisciplina(null);
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File $imageFile
*/
public function setImageFile(File $imageFile=null)
{
$this->imageFile = $imageFile;
}
/**
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
* @param string $imageName
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return mixed
*/
public function getLocatieLaborator()
{
return $this->locatieLaborator;
}
/**
* @param mixed $locatieLaborator
*/
public function setLocatieLaborator($locatieLaborator)
{
$this->locatieLaborator = $locatieLaborator;
}
/**
* @return mixed
*/
public function getDescriere()
{
return $this->descriere;
}
/**
* @param mixed $descriere
*/
public function setDescriere($descriere)
{
$this->descriere = $descriere;
}
/**
* @return mixed
*/
public function getPersonal()
{
return $this->personal;
}
/**
* @param mixed $personal
*/
public function setPersonal($personal)
{
$this->personal = $personal;
}
/**
* @return mixed
*/
public function getReferate()
{
return $this->referate;
}
/**
* @param mixed $referate
*/
public function setReferate($referate)
{
$this->referate = $referate;
}
/**
* @return mixed
*/
public function getLaboratoare()
{
return $this->laboratoare;
}
/**
* @param mixed $laboratoare
*/
public function setLaboratoare($laboratoare)
{
$this->laboratoare = $laboratoare;
}
/**
* @return mixed
*/
public function getCursuri()
{
return $this->cursuri;
}
/**
* @param mixed $cursuri
*/
public function setCursuri($cursuri)
{
$this->cursuri = $cursuri;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
public function __construct()
{
$this->cursuri = new ArrayCollection();
$this->laboratoare = new ArrayCollection();
$this->referate = new ArrayCollection();
}
}
cursuri实体
<?php
/**
* Created by PhpStorm.
* User: Alex
* Date: 2/6/2017
* Time: 2:11 PM
*/
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use VichUploaderBundleMappingAnnotation as Vich;
use SymfonyComponentHttpFoundationFileFile;
/**
* @ORMEntity
* @ORMTable(name="curs")
* @VichUploadable
*/
class Curs {
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*/
private $id;
/**
* @VichUploadableField(mapping="discipline_files", fileNameProperty="fileName")
*
* @var File
*/
private $file;
/**
* @ORMColumn(type="string", length=255)
*
* @var string
*/
private $fileName;
/**
* @ORMColumn(type="string",nullable=true)
*/
private $ordine;
/**
* @ORMColumn(type="string",nullable=true)
*/
private $nume;
/**
* @var
* @ORMManyToOne(targetEntity="AppBundleEntityDisciplina",inversedBy="cursuri")
* @ORMJoinColumn(name="disciplina_id", referencedColumnName="id",nullable=false)
*/
private $disciplina;
/**
* @return mixed
*/
public function getDisciplina()
{
return $this->disciplina;
}
/**
* @param mixed $disciplina
*/
public function setDisciplina($disciplina)
{
$this->disciplina = $disciplina;
}
/**
* @return mixed
*/
public function getNume()
{
return $this->nume;
}
/**
* @param mixed $nume
*/
public function setNume($nume)
{
$this->nume = $nume;
}
/**
* @return string
*/
public function getFileName()
{
return $this->fileName;
}
/**
* @param string $fileName
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
/**
* @return mixed
*/
public function getFile()
{
return $this->file;
}
/**
* @param mixed $file
*/
public function setFile(File $file=null)
{
$this->file = $file;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getOrdine()
{
return $this->ordine;
}
/**
* @param mixed $ordine
*/
public function setOrdine($ordine)
{
$this->ordine = $ordine;
}
}
纪律
namespace AppBundleForm;
use AppBundleEntityDisciplina;
use AppBundleEntityFisier;
use AppBundleEntityLaborator;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use VichUploaderBundleFormTypeVichFileType;
class DisciplinaForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('imageFile', VichFileType::class, [
'required' => false,
'label' =>'Banner',
'allow_delete' => false, // not mandatory, default is true
'download_link' => false, // not mandatory, default is true
])
->add("locatieLaborator")
->add("descriere")
->add("personal")
->add('laboratoare',CollectionType::class,[
'entry_type'=>LaboratorForm::class,
'allow_add' => true,
'by_reference' => false,
'attr' => array('class' => 'row'),
'allow_delete' => true,
'entry_options' => array(
'attr' => array('class' => 'col-xs-4')
),
])
->add('cursuri',CollectionType::class,[
'entry_type'=>CursForm::class,
'allow_add' => true,
'by_reference' => false,
'attr' => array('class' => 'row'),
'allow_delete' => true,
'entry_options' => array(
'attr' => array('class' => 'col-xs-4')
),
])
->add('referate',CollectionType::class,[
'entry_type'=>ReferatForm::class,
'allow_add' => true,
'by_reference' => false,
'attr' => array('class' => 'row'),
'allow_delete' => true,
'entry_options' => array(
'attr' => array('class' => 'col-xs-4')
),
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
"data_class"=>Disciplina::class
]);
}
}
遏制,引用和Laboratoare是相同的形式,只有名称被更改
<?php
namespace AppBundleForm;
use AppBundleEntityCurs;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use VichUploaderBundleFormTypeVichFileType;
class CursForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add("nume")
->add("ordine")
->add('file', VichFileType::class, [
'required' => false,
'allow_delete' => false, // not mandatory, default is true
'download_link' => false, // not mandatory, default is true
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Curs::class
]);
}
}
控制器
/**
* @param $name
* @return SymfonyComponentHttpFoundationResponse
* @Route("/adauga/disciplina",name="adauga_disciplina")
*/
public function newAction(Request $request)
{
$form = $this->createForm(DisciplinaForm::class);
// only handles data on POST
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$disciplina = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($disciplina);
$em->flush();
$this->addFlash(
'success',
sprintf('Disciplina adaugata.')
);
return $this->redirectToRoute('adauga_disciplina');
}
return $this->render('disciplina/adauga_disciplina.html.twig',[
'form'=>$form->createView()
]);
}
这几乎是不可能的,只有名称是不同的,并且不起作用。laboratoare和引用在起作用,但是cursuri却没有。任何帮助?
我必须运行那些控制台命令
php vendor/bin/doctrine.php orm:clear-cache:result
php vendor/bin/doctrine.php orm:clear-cache:query
php vendor/bin/doctrine.php orm:clear-cache:metadata
加上愚蠢的表单调用方法addcourse而不是addCourses,它给了我id null,用于reall ????????
我对您的建议就是在实体类中建立您的关系,然后让机器人通过发行:
来完成所有工作的工作。php bin/console doctrine:generate:entities AppBundle:Curs
这将确保您拥有所有必要的方法。因为,例如,您拥有未使用的setCursuri()
方法。相反,您需要addCurs(AppBundleEntityCurs $curs)
,removeCurs(AppBundleEntityCurs $curs)
和getCursuri()
。