Symfony 3集合多对一,一对多,FK为空

时间:2017-02-18 16:24:38

标签: php doctrine-orm orm symfony

我有一个到多个集合,其中2个集合正在工作但是cursuri(变量名称)没有得到门徒的id是null.Laboratoare和referate工作为什么?我尝试了一切唯一的区别我看到的是这个名字。 Disciplina实体  

  namespace AppBundle\Entity;


 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\HttpFoundation\File\File;
  use Vich\UploaderBundle\Mapping\Annotation as Vich;

 /**
 * @ORM\Entity
   * @ORM\Table(name="disciplina")
* @Vich\Uploadable
 */
 class Disciplina
{

/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * NOTE: This is not a mapped field of entity metadata, just a simple property.
 *
 * @Vich\UploadableField(mapping="banner_image", fileNameProperty="imageName")
 *
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $imageName;

/**
 * @ORM\Column(type="string")
 */
private $locatieLaborator;

/**
 * @ORM\Column(type="string")
 */
private $descriere;

/**
 * @ORM\Column(type="string")
 */
private $personal;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Referat",mappedBy="disciplina",cascade={"persist"})
 *
 */
private $referate;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Laborator",mappedBy="disciplina",cascade={"persist"})
 *
 */
private $laboratoare;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Curs",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 AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
 use Symfony\Component\HttpFoundation\File\File;

 /**
* @ORM\Entity
* @ORM\Table(name="curs")
 * @Vich\Uploadable
*/
 class Curs {
/**
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @Vich\UploadableField(mapping="discipline_files", fileNameProperty="fileName")
 *
 * @var File
 */
private $file;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $fileName;

/**
 * @ORM\Column(type="string",nullable=true)
 */
private $ordine;

/**
 * @ORM\Column(type="string",nullable=true)
 */
private $nume;

/**
 * @var
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Disciplina",inversedBy="cursuri")
 * @ORM\JoinColumn(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;
}

}

DisciplinaForm       

   namespace AppBundle\Form;

   use AppBundle\Entity\Disciplina;
  use AppBundle\Entity\Fisier;
  use AppBundle\Entity\Laborator;
 use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
 use Vich\UploaderBundle\Form\Type\VichFileType;

 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
    ]);
}
}

CursForm,referate和laboratoare是相同的形式,只有名称被更改

   <?php

   namespace AppBundle\Form;

  use AppBundle\Entity\Curs;
  use Symfony\Component\Form\AbstractType;
 use Symfony\Component\Form\FormBuilderInterface;
 use Symfony\Component\OptionsResolver\OptionsResolver;
 use Vich\UploaderBundle\Form\Type\VichFileType;

  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 \Symfony\Component\HttpFoundation\Response
    * @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()
    ]);
  }

这几乎是不可能的只有名字不同而且不起作用。实验和遗嘱正在运作但是cursuri不是。任何帮助?

2 个答案:

答案 0 :(得分:0)

我必须运行那些控制台命令

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 ???????要验证这一点,你可以删除setCourses方法,它会告诉你想要的方法。

答案 1 :(得分:0)

我对你的建议是在实体课程中建立你的关系,然后让机器人完成剩下的工作,发出:

Foo

这将确保您拥有所有必需的方法。例如,因为您拥有未使用的php bin/console doctrine:generate:entities AppBundle:Curs 方法。相反,您需要setCursuri()addCurs(\AppBundle\Entity\Curs $curs)removeCurs(\AppBundle\Entity\Curs $curs)