Doctrine insert multiple in relationship Many to Many

0

I have a simple system with 3 entities (Ocorrencia, Aluno and OcurênciaAluno), this third entity represents a Many to Many relationship between the first two. The Instance and Student entities are working perfectly, however when entering an occurrence, records should be inserted in the OccurrenceAlumn table simultaneously. an occurrence has several students. Here are the Entity Codes, if anyone can help, thank you.

Occurrence.php

<?php

namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity\AbstractEntity;
use Doctrine\ORM\Mapping as ORM;


/**
 * Ocorrencia
 *
 * @Table(name="ocorrencia")
 * @Entity
 * @HasLifecycleCallbacks()
 */
class Ocorrencia extends AbstractEntity
{
public function __construct()
{
    $this->alunos = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * @var integer @Id
 * @Column(name="id", type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var string @Column(type="string", length=255)
 */
private $titulo;
/**
 * @var string @Column(type="string", length=255)
 */
private $tipo;
/**
 * @var string @Column(type="string", length=255)
 */
private $descricao;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ManyToMany(targetEntity="SistemaIfnmg\Entity\Aluno", inversedBy="ocorrenciafk")
 * @JoinTable(name="ocorrenciaAluno",
 *   joinColumns={
 *     @JoinColumn(name="ocorrenciafk", referencedColumnName="id")
 *   },
 *   inverseJoinColumns={
 *     @JoinColumn(name="alunofk", referencedColumnName="id")
 *   }
 * )
 */
private $alunos;


/**
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return string
 */
public function getTitulo()
{
    return $this->titulo;
}

/**
 * @param string $titulo
 */
public function setTitulo($titulo)
{
    $this->titulo = $titulo;
}

/**
 * @return string
 */
public function getTipo()
{
    return $this->tipo;
}

/**
 * @param string $tipo
 */
public function setTipo($tipo)
{
    $this->tipo = $tipo;
}

/**
 * @return string
 */
public function getDescricao()
{
    return $this->descricao;
}

/**
 * @param string $descricao
 */
public function setDescricao($descricao)
{
    $this->descricao = $descricao;
}

/**
 * Add aluno
 *
 * @param \SistemaIfnmg\Entity\Aluno $aluno
 *
 * @return Ocorrencia
 */
public function addAluno($aluno)
{
    if($aluno instanceof \SistemaIfnmg\Entity\Aluno){
        $this->alunos = $aluno;
    }elseif($aluno instanceof \Doctrine\Common\Collections\Collection){
        foreach($aluno as $afk){
            $this->alunos = $afk;
        }
    }
    return $this;
}

/**
 * Remove aluno
 *
 * @param \SistemaIfnmg\Entity\Aluno $aluno
 */
public function removeAluno($aluno)
{
    $this->alunos->removeElement($aluno);
}

/**
 * Get alunos
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getAlunos()
{
    return $this->alunos;
}


/**
 * @param string $alunos
 */
public function setAlunos($alunos)
{
    $this->alunos = $alunos;
}

function toString(){
    return "[id:".$this->id."] [tipo:".$this->tipo."] [titulo:".$this->titulo."] [descricao:".$this->descricao."][alunos:".$this->alunos."]";
}

function toArray()
{
    return [
        "id" => $this->id,
        "tipo" => $this->tipo,
        "titulo" => $this->titulo,
        "descricao" => $this->descricao,
        "alunos" => $this->alunos->toArray(),
    ];
}
}

?>

Student.php

<?php

namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity\AbstractEntity;
/**
 * @Entity
 * @Table(name="aluno")
 */
class Aluno extends AbstractEntity
{
/**
 * @var integer @Id
 * @Column(name="id", type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string @Column(type="string", length=255)
 */
private $nome;
/**
 * @var string @Column(type="string", length=255)
 */
private $sobrenome;
/**
 * @var string @Column(type="string", length=255)
 */
private $matricula;

/**
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * @return string
 */
public function getNome()
{
    return $this->nome;
}

/**
 * @param string $nome
 */
public function setNome($nome)
{
    $this->nome = $nome;
}

/**
 * @return string
 */
public function getSobrenome()
{
    return $this->sobrenome;
}

/**
 * @param string $sobrenome
 */
public function setSobrenome($sobrenome)
{
    $this->sobrenome = $sobrenome;
}

/**
 * @return string
 */
public function getMatricula()
{
    return $this->matricula;
}

/**
 * @param string $matricula
 */
public function setMatricula($matricula)
{
    $this->matricula = $matricula;
}


function toString(){
    return "[id:".$this->id."] [nome:".$this->nome."] [sobrenome:".$this->sobrenome."] [matricula:".$this->matricula."]";
}

function toArray()
{
    return [
        "id" => $this->id,
        "nome" => $this->nome,
        "sobrenome" => $this->sobrenome,
        "matricula" => $this->matricula,
    ];
}
}

?>

OccurrenceAlumnus.php

<?php

namespace SistemaIfnmg\Entity;

use SistemaIfnmg\Entity\AbstractEntity;

/**
 * OcorrenciaAluno
 *
 * @Table(name="ocorrenciaAluno", indexes= 
   {@Index(name="fk_Ocorrencia_has_Aluno_Ocorrencia1_idx", columns= 
   {"ocorrenciafk"}), 
@Index(name="fk_Ocorrencia_has_Aluno_Aluno1_idx", 
columns={"alunofk"})})
 * @Entity
 */
class OcorrenciaAluno extends AbstractEntity
{
/**
 * @var integer @Id
 * @Column(name="id", type="integer")
 * @GeneratedValue(strategy="AUTO")
 */
private $id;



/**
 * @var \SistemaIfnmg\Entity\Ocorrencia
 *
 * @ManyToOne(targetEntity="SistemaIfnmg\Entity\Ocorrencia")
 * @JoinColumns({
 *   @JoinColumn(name="ocorrenciafk", referencedColumnName="id")
 * })
 */
private $ocorrenciafk;

/**
 * @var \SistemaIfnmg\Entity\Aluno
 *
 * @ManyToOne(targetEntity="SistemaIfnmg\Entity\Aluno")
 * @JoinColumns({
 *   @JoinColumn(name="alunofk", referencedColumnName="id")
 * })
 */
private $alunofk;

/**
 * @var \DateTime
 *
 * @Column(name="data", type="datetime", nullable=true)
 */
private $data;

/**
 * @return int
 */
public function getId()
{
    return $this->id;
}

/**
 * @param int $id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * Set ocorrenciafk
 *
 * @param \SistemaIfnmg\Entity\Ocorrencia $ocorrenciafk
 *
 * @return OcorrenciaAluno
 */
public function setOcorrenciafk(\SistemaIfnmg\Entity\Ocorrencia $ocorrenciafk = null)
{
    $this->ocorrenciafk = $ocorrenciafk;

    return $this;
}

/**
 * Get ocorrenciafk
 *
 * @return \SistemaIfnmg\Entity\Ocorrencia
 */
public function getOcorrenciafk()
{
    return $this->ocorrenciafk;
}

/**
 * Set alunofk
 *
 * @param \SistemaIfnmg\Entity\Aluno $alunofk
 *
 * @return OcorrenciaAluno
 */
public function setAlunofk(\SistemaIfnmg\Entity\Aluno $alunofk = null)
{
    $this->alunofk = $alunofk;

    return $this;
}

/**
 * Get alunofk
 *
 * @return \SistemaIfnmg\Entity\Aluno
 */
public function getAlunofk()
{
    return $this->alunofk;
}

/**
 * @return \DateTime
 */
public function getData()
{
    return $this->data;
}

/**
 * @param \DateTime $data
 */
public function setData($data)
{
    $this->data = $data;
}

function toArray()
{
    return [
        "id" => $this->id,
        "alunofk" => $this->alunofk,
        "ocorrenciafk" => $this->ocorrenciafk,
        "data" => $this->data,
    ];
}
}

?>

When I insert an instance with several students, the record in the occurrence table happens, but it does not happen in the StudentDate table. Since I thank you

    
asked by anonymous 13.07.2018 / 14:31

0 answers