多对多教义,只坚持一面

时间:2018-08-08 14:29:58

标签: php orm doctrine slim

我有一个带有两个表和一个联接表的系统。鲁尼奥有很多奴维奴,一个仆人有很多鲁尼奴。为此,我有一个reuniao表,servidor表和reuniaoservidor表。我想与许多现有的服务对象一起保留一个reuniao,但是我不想在保留一个reuniao时保留一个服务对象。问题是... 当我尝试坚持时,鲁尼奥主义对我说:

“通过关系'SistemaIfnmg \ Entity \ Reuniao#servidores'找到了一个新实体,该关系未配置为级联实体的持久操作:SistemaIfnmg \ Entity \ ReuniaoServidor @ 000000006a69473200007f33ddb737e2。要解决此问题:要么显式调用EntityManager#在未知实体上持久化()或配置级联,将该关联持久化在映射中,例如@ManyToOne(..,cascade = {“ persist”})。 #__ toString()'以获得线索。”

实体:

Reuniao.php     

namespace SistemaIfnmg\Entity;
use SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="reuniao")
 * @ORM\HasLifecycleCallbacks()
 */
class Reuniao
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $titulo;
/**
 * @ORM\Column(type="string", length=255)
 */
private $descricao;
/**
 * @ORM\Column(type="datetime")
 */
private $data;

/**
 * @ORM\OneToMany(targetEntity="SistemaIfnmg\Entity\ReuniaoServidor", mappedBy="reuniaofk")
 */
private $servidores;

public function __construct(){
    $this->servidores = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Get id
 *
 * @return integer
 */
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 getDescricao()
{
    return $this->descricao;
}

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

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

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

/**
 * Add servidores
 * @param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
 * @return Reuniao
 */
public function addServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
    $this->servidores[] = $servidor;
}

/**
 * Remove servidor
 *
 * @param \SistemaIfnmg\Entity\ReuniaoServidor $servidor
 */
public function removeServidor(\SistemaIfnmg\Entity\ReuniaoServidor $servidor){
    $this->servidores->removeElement($servidor);
}

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


}

?>

Servidor.php

<?php

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

/**
 * @ORM\Entity()
 * @ORM\Table(name="servidor")
 */
class Servidor
{

/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @var integer $id
 */
private $id;

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



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

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

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

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

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

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

/**
 * Get cargo
 *
 * @return string
 */
public function getCargo()
{
    return $this->cargo;
}

/**
 * Set cargo
 *
 * @param string $cargo
 */
public function setCargo($cargo)
{
    $this->cargo = $cargo;
}
/**
 * Get matricula
 *
 * @return string
 */
public function getMatricula()
{
    return $this->matricula;
}

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




}

?>

ReuniadoServidor.php

<?php

namespace SistemaIfnmg\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * ReuniaoServidor
 *
 * @ORM\Table(name="reuniaoservidor", indexes={@ORM\Index(name="fk_Reuniao_has_Servidor_Reuniao1_idx", columns={"reuniaofk"}), @ORM\Index(name="fk_Reuniao_has_Servidor_Servidor1_idx", columns= {"servidorfk"})})
 * @ORM\Entity
 */
class ReuniaoServidor
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Reuniao", inversedBy="servidores", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="reuniaofk", referencedColumnName="id")
 * })
 */
private $reuniaofk;


/**
 * @ORM\ManyToOne(targetEntity="SistemaIfnmg\Entity\Servidor", inversedBy="servidores")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="servidorfk", referencedColumnName="id")
 * })
 */
private $servidorfk;


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

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

/**
 * Get reuniaofk
 * @return \SistemaIfnmg\Entity\Reuniao
 */
public function getReuniaofk()
{
    return $this->reuniaofk;
}

/**
 * Set reuniaofk
 * @param \SistemaIfnmg\Entity\Reuniao $reuniaofk
 * @return ReuniaoServidor
 */
public function setReuniaofk($reuniaofk)
{
    $this->reuniaofk = $reuniaofk;
    return $this;
}

/**
 * Get servidorfk
 * @return \SistemaIfnmg\Entity\Servidor
 */
public function getServidorfk()
{
    return $this->servidorfk;
}

/**
 * Set servidorfk
 * @param \SistemaIfnmg\Entity\Servidor $servidorfk
 * @return ReuniaoServidor
 */
public function setServidorfk($servidorfk)
{
    $this->servidorfk = $servidorfk;
    return $this;
}



}

?>

我很久以来一直在尝试纠正此错误,请帮忙? 非常感谢

1 个答案:

答案 0 :(得分:0)

您不需要将Reuniao连接到Servidor的实体(ReuniaoServidor实体),因为Doctrine会为您处理连接表。您只需要在JoinTable映射中指定连接列即可。

在Reuniao.php中,将伺服服务器的映射更改为:

/**
 * @ORM\ManyToMany(targetEntity="SistemaIfnmg\Entity\Servidor")
 * @ORM\JoinTable(name="reuniao_servidor",
 *      joinColumns={@ORM\JoinColumn(name="ruuniao_id", referencedColumnName="id")},
 *     inverseJoinColumns{@ORM\JoinColumn(name="servidor_id", referencedColumnName="id")}
 *   )
 */
private $servidores;

还添加了一个构造函数,用于初始化集合的数组

public function __construct()
{
    $this->servidores = new ArrayCollection();
}

请参见https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#many-to-many-unidirectional