【问题标题】:Symfony 3 collection many to one , one to many, FK is nullSymfony 3 集合多对一,一对多,FK 为空
【发布时间】:2017-02-18 16:24:37
【问题描述】:

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

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

}

学科形式

   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 和 labatoare 是同一个表单,只是名称改变了

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

这几乎是不可能的,只是名称不同并且不起作用。Laboratoare 和 referate 正在工作,但 cursuri 没有。有什么帮助吗?

【问题讨论】:

    标签: php doctrine-orm orm symfony


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我对你的建议是在实体类中建立你的关系,然后让机器人完成所有其余的工作,发出:

      php bin/console doctrine:generate:entities AppBundle:Curs
      

      这将确保您拥有所有需要的方法。因为,例如,您有未使用的 setCursuri() 方法。相反,您需要 addCurs(\AppBundle\Entity\Curs $curs)removeCurs(\AppBundle\Entity\Curs $curs)getCursuri()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-25
        • 2018-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        相关资源
        最近更新 更多