次のDB構造があります。
Pge\IncidenciasBundle\Entity\Cliente:
type: entity
table: cliente
repositoryClass: Pge\IncidenciasBundle\Entity\ClienteRepository
id:
id:
type: integer
generator:
strategy: IDENTITY
fields:
nombre:
type: string
length: 250
fixed: false
nullable: true
manyToMany:
servicios:
targetEntity: Servicio
cascade: [persist]
lifecycleCallbacks: { }
Pge\IncidenciasBundle\Entity\Servicio:
type: entity
table: servicio
id:
id:
type: integer
generator:
strategy: IDENTITY
fields:
nombre:
type: string
length: 250
fixed: false
nullable: true
lifecycleCallbacks: { }
現在、cliente_servicio
この 2 つのエンティティ間の関係を保持するという名前の DB テーブルがあります。たとえば、次のデータがあります。
cliente_id servicio_id
1 1
1 4
1 8
2 1
3 3
3 7
フォームは現在、この関係を管理するために完全に機能しています。しかし今、私は別の機能が必要です。
行ごとに2つの追加フィールドを設定するために、新しいフォーム/構造/あなたが私に言ったことは何でも作成する必要があります:status
したがってcomment
、date
最終的なDB出力は次のようになります:
cliente_id servicio_id date status comment
1 1 2013-09-19 ok null
1 4 2013-09-19 ko Needs to clear
1 8 2013-09-19 ok null
2 1 2013-09-19 ko Packets lost (fix firewall)
3 3 2013-09-19 ko Out of service (see ticket T9388)
3 7 2013-09-19 ok null
ポイントは、ルートを呼び出すlocalhost/whatever/{id}/{date}
こと{id}
ですcliente_id
(必要に応じてエンティティのフィールドnombre
にクライアント名を指定できますcliente
)。{date} は日付です。このページのインデックスでは、記録されたデータが表示されます(作成したいが作成しないデータ)方法を知っている) 選択された の特定の日付cliente_id
。
new
アクションでは、フォームには 、および割り当てられたそれぞれ ( ) のフィールドestado
がcomment
表示さdate
れます。servicio_id
{id}
cliente_id
Symfony2 を知っていれば簡単に作れると思いますが、私は完全に迷っています...単純な PHP (フレームワークなし) でこの状況をなんとか処理して動作していますが、Symfony2 ではどうすればよいかわかりません。
追加情報:
cliente_servicio
リレーションシップを処理するテーブルmanyToMany
は Symfony2 ではマップされません。自動生成doctrine:schema:update --force
されたため、id プライマリ キーがないためマップできません ( と しかありませんcliente_id
) servicio_id
。
アップデート
さて、Doctrine2 を実行します。参照テーブルに余分な列がある多対多を処理するための最良の方法です。これで、中間エンティティとの m:1 および 1:m 関係が作成されcliente
、servicio
呼び出されますclienteservicio
fromフォームに割り当てる
servicio
ことができないことがわかりました。cliente
cliente
形からでもない
servicio
。ClienteServicio
新しく作成されたエンティティ、「中間」エンティティからできます。しかし、以前のように1 つに対して複数を選択することはできません。servicio
cliente
また、ポイントは、単一のフォームに X 行とestado
、comentario
およびX =指定された割り当てdate
の数であるフィールドを含めることでしたservicio
cliente
<?php
namespace Pge\IncidenciasBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Cliente
*
* @ORM\Table(name="cliente")
* @ORM\Entity(repositoryClass="Pge\IncidenciasBundle\Entity\ClienteRepository")
*/
class Cliente
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=250, nullable=true)
*/
private $nombre;
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\OneToMany(targetEntity="ClienteServicio", mappedBy="cliente")
*/
private $servicio;
public function __toString()
{
return $this->nombre;
}
/**
* Constructor
*/
public function __construct()
{
$this->servicio = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* @param string $nombre
* @return Cliente
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* @return string
*/
public function getNombre()
{
return $this->nombre;
}
/**
* Add servicio
*
* @param \Pge\IncidenciasBundle\Entity\ClienteServicio $servicio
* @return Cliente
*/
public function addServicio(\Pge\IncidenciasBundle\Entity\ClienteServicio $servicio)
{
$this->servicio[] = $servicio;
return $this;
}
/**
* Remove servicio
*
* @param \Pge\IncidenciasBundle\Entity\ClienteServicio $servicio
*/
public function removeServicio(\Pge\IncidenciasBundle\Entity\ClienteServicio $servicio)
{
$this->servicio->removeElement($servicio);
}
/**
* Get servicio
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getServicio()
{
return $this->servicio;
}
}
<?php
namespace Pge\IncidenciasBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Servicio
*
* @ORM\Table(name="servicio")
* @ORM\Entity
*/
class Servicio
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombre", type="string", length=250, nullable=true)
*/
private $nombre;
/*
*
* @ORM\OneToMany(targetEntity="ClienteServicio", mappedBy="servicio")
*/
private $cliente;
public function __toString()
{
return $this->nombre;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nombre
*
* @param string $nombre
* @return Servicio
*/
public function setNombre($nombre)
{
$this->nombre = $nombre;
return $this;
}
/**
* Get nombre
*
* @return string
*/
public function getNombre()
{
return $this->nombre;
}
}
<?php
namespace Pge\IncidenciasBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* ClienteServicio
*
* @ORM\Table(name="cliente_servicio")
* @ORM\Entity
*/
class ClienteServicio
{
/**
* @var integer
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="estado", type="string", length=255, nullable=false)
*/
private $estado;
/**
* @var string
*
* @ORM\Column(name="comentario", type="string", length=255, nullable=false)
*/
private $comentario;
/**
* @var \DateTime
*
* @ORM\Column(name="fecha", type="date", nullable=false)
*/
private $fecha;
/**
* @var \Pge\IncidenciasBundle\Entity\Servicio
*
* @ORM\ManyToOne(targetEntity="Servicio", inversedBy="cliente")
*/
private $servicio;
/**
* @var \Pge\IncidenciasBundle\Entity\Id
*
* @ORM\ManyToOne(targetEntity="Cliente", inversedBy="servicio")
*/
private $cliente;
/**
* Set id
*
* @param integer $id
* @return ClienteServicio
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set estado
*
* @param string $estado
* @return ClienteServicio
*/
public function setEstado($estado)
{
$this->estado = $estado;
return $this;
}
/**
* Get estado
*
* @return string
*/
public function getEstado()
{
return $this->estado;
}
/**
* Set comentario
*
* @param string $comentario
* @return ClienteServicio
*/
public function setComentario($comentario)
{
$this->comentario = $comentario;
return $this;
}
/**
* Get comentario
*
* @return string
*/
public function getComentario()
{
return $this->comentario;
}
/**
* Set fecha
*
* @param \DateTime $fecha
* @return ClienteServicio
*/
public function setFecha($fecha)
{
$this->fecha = $fecha;
return $this;
}
/**
* Get fecha
*
* @return \DateTime
*/
public function getFecha()
{
return $this->fecha;
}
/**
* Set servicio
*
* @param \Pge\IncidenciasBundle\Entity\Servicio $servicio
* @return ClienteServicio
*/
public function setServicio(\Pge\IncidenciasBundle\Entity\Servicio $servicio = null)
{
$this->servicio = $servicio;
return $this;
}
/**
* Get servicio
*
* @return \Pge\IncidenciasBundle\Entity\Servicio
*/
public function getServicio()
{
return $this->servicio;
}
/**
* Set cliente
*
* @param \Pge\IncidenciasBundle\Entity\Cliente $cliente
* @return ClienteServicio
*/
public function setCliente(\Pge\IncidenciasBundle\Entity\Cliente $cliente = null)
{
$this->cliente = $cliente;
return $this;
}
/**
* Get cliente
*
* @return \Pge\IncidenciasBundle\Entity\Cliente
*/
public function getCliente()
{
return $this->cliente;
}
}
私が言ったように、ポイントはfromフォームに割り当てservicio
られます。cliente
cliente
次に、別のフォームから、追加のフィールドをservicio
割り当て先に設定します (各行を含む 1 つのフォームですべてをcliente
処理できれば素晴らしいと思います)servicio
cliente
servicio
私が今しているやり方は、私が望んでいるやり方ではありません...