您好,提前感谢任何会尝试提供帮助的人。
我对Symfony和教义很陌生,我无法弄清楚为什么我的票证实体在没有ManyToMany Realtionship的情况下存储。我非常努力,做了几天来我发现的任何事情,但最终我需要帮助。
我们到了
票
<pre>
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use AppBundleEntityComputer;
/**
* @ORMEntity
* @ORMTable(name="tickets")
*/
class Ticket {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMColumn(type="integer")
*/
protected $creator;
/**
* @ORMColumn(type="integer")
*/
protected $owner;
/**
* @ORMManyToOne(targetEntity="Category", inversedBy="tickets")
* @ORMJoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* @ORMManyToOne(targetEntity="Type", inversedBy="tickets")
* @ORMJoinColumn(name="type_id", referencedColumnName="id")
*/
protected $type;
/**
* @ORMColumn(type="string")
*/
protected $subject;
/**
* @ORMManyToMany(targetEntity="Computer", inversedBy="tickets", cascade={"persist"})
* @ORMJoinTable(name="inventory_computer_join",
* joinColumns={@ORMJoinColumn(name="ticket_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="computer_id", referencedColumnName="id")}
*)
*/
protected $computer;
[...]
/**
* Add computer
*
* @param AppBundleEntityComputer $computer
*
* @return Ticket
*/
public function addComputer(AppBundleEntityComputer $computer)
{
$this->computers[] = $computer;
return $this;
}
/**
* Remove computer
* @param AppBundleEntityComputer $computer
*/
public function removeComputer(AppBundleEntityComputer $computer)
{
$this->computers->removeElement($computer);
}
/**
* Get computers
* @return DoctrineCommonCollectionsCollection
*/
public function getComputers()
{
return $this->computers;
}
}
</pre>
计算机
<pre>
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use AppBundleEntityTicket;
/**
* @ORMEntity
* @ORMTable(name="inventory_computer")
*/
class Computer
{
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToMany(targetEntity="Ticket", mappedBy="computer")
*/
protected $tickets;
[...]
/**
* Add ticket
*
* @param AppBundleEntityTicket $ticket
*
* @return Computer
*/
public function addTicket(AppBundleEntityTicket $ticket)
{
$this->tickets[] = $ticket;
return $this;
}
</pre>
控制器
<pre>
/**
* @Route("admint/createTicket", name="AdminTicketErstellen")
*/
public function AdminCreateTicketAction(Request $request) {
$ticket = new Ticket;
$form = $this->createForm(TicketType::class, $ticket, array(
'method' => 'GET'
));
$form->handleRequest($request);
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ticket);
$em->flush();
return $this->redirectToRoute('measure_servers');
}
return $this->render( 'sysadmin/pages/AdminCreateTicket.html.twig', array(
'ticket' => $form->createView(),
)
);
}
</pre>
如果你想要存储 ManyToMany 试试;
第一:
将protected $commputer;
更改为在工单.php实体protected $computers
。因为,您在addComputer()或removeComputer()中使用$this->计算机。
第二:* @ORMManyToMany(targetEntity="Ticket", mappedBy="computer")
在计算机.php实体mappedBy="computers"
。
您可以使用计算机存储票证,请在控制器上尝试: $ticket = 新票证(); $computer = 新计算机();
$ticket->setCreator(4);
$ticket->setOwner(3);
$computer->addTicket($ticket);
$ticket->addComputer($computer);
$em = $this->getDoctrine()->getManager();
$em->persist($computer);
$em->persist($ticket);
$em->flush();