我有3个表:EdiTradingPartner,EdiDocType和EdiTradingPartnerTransactions
SRC \矩阵\ MatrixEdiBundle \实体\ EdiTradingPartner
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiTradingPartner
*
* @ORM\Table(name="edi_trading_partner")
* @ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiTradingPartnerRepository")
*/
class EdiTradingPartner
{
/**
* @var string
*
* @ORM\Column(name="edi_interchange_id", type="string", length=30, nullable=false)
*/
private $ediInterchangeId;
/**
* @var string
*
* @ORM\Column(name="tp_name", type="string", length=30, nullable=false)
*/
private $tpName;
/**
* @var string
*
* @ORM\Column(name="tp_location", type="string", length=50, nullable=false)
*/
private $tpLocation;
/**
* @var integer
*
* @ORM\Column(name="edi_trading_partner_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediTradingPartnerId;
/**
* Set ediInterchangeId
*
* @param string $ediInterchangeId
* @return EdiTradingPartner
*/
public function setEdiInterchangeId($ediInterchangeId)
{
$this->ediInterchangeId = $ediInterchangeId;
return $this;
}
/**
* Get ediInterchangeId
*
* @return string
*/
public function getEdiInterchangeId()
{
return $this->ediInterchangeId;
}
/**
* Set tpName
*
* @param string $tpName
* @return EdiTradingPartner
*/
public function setTpName($tpName)
{
$this->tpName = $tpName;
return $this;
}
/**
* Get tpName
*
* @return string
*/
public function getTpName()
{
return $this->tpName;
}
/**
* Set tpLocation
*
* @param string $tpLocation
* @return EdiTradingPartner
*/
public function setTpLocation($tpLocation)
{
$this->tpLocation = $tpLocation;
return $this;
}
/**
* Get tpLocation
*
* @return string
*/
public function getTpLocation()
{
return $this->tpLocation;
}
/**
* Get ediTradingPartnerId
*
* @return integer
*/
public function getEdiTradingPartnerId()
{
return $this->ediTradingPartnerId;
}
}
SRC \矩阵\ MatrixEdiBundle \实体\ EdiDocType
<?php
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiDocType
*
* @ORM\Table(name="edi_doc_type")
* @ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiDocTypeRepository")
*/
class EdiDocType
{
/**
* @var integer
*
* @ORM\Column(name="doc_type", type="integer", nullable=false)
*/
private $docType;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50, nullable=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="description", type="string", length=255, nullable=false)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="direction", type="string", length=10, nullable=false)
*/
private $direction;
/**
* @var integer
*
* @ORM\Column(name="edi_doc_type_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediDocTypeId;
/**
* Set docType
*
* @param integer $docType
* @return EdiDocType
*/
public function setDocType($docType)
{
$this->docType = $docType;
return $this;
}
/**
* Get docType
*
* @return integer
*/
public function getDocType()
{
return $this->docType;
}
/**
* Set name
*
* @param string $name
* @return EdiDocType
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set description
*
* @param string $description
* @return EdiDocType
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set direction
*
* @param string $direction
* @return EdiDocType
*/
public function setDirection($direction)
{
$this->direction = $direction;
return $this;
}
/**
* Get direction
*
* @return string
*/
public function getDirection()
{
return $this->direction;
}
/**
* Get ediDocTypeId
*
* @return integer
*/
public function getEdiDocTypeId()
{
return $this->ediDocTypeId;
}
}
SRC \矩阵\ MatrixEdiBundle \实体\ EdiTradingPartnerTransactions
namespace Matrix\MatrixEdiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* EdiTradingPartnerTransactions
*
* @ORM\Table(name="edi_trading_partner_transactions", indexes={@ORM\Index(name="edi_tp_id", columns={"edi_tp_id", "edi_doc_type_id"}), @ORM\Index(name="edi_transactions_id", columns={"edi_doc_type_id"}), @ORM\Index(name="IDX_F2BE50F7B9C737A1", columns={"edi_tp_id"})})
* @ORM\Entity(repositoryClass="Matrix\MatrixEdiBundle\Repository\EdiTradingPartnerTransactionsRepository")
*/
class EdiTradingPartnerTransactions
{
/**
* @var integer
*
* @ORM\Column(name="edi_tp_transactions_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $ediTpTransactionsId;
/**
* @var \Matrix\MatrixEdiBundle\Entity\EdiDocType
*
* @ORM\ManyToOne(targetEntity="Matrix\MatrixEdiBundle\Entity\EdiDocType")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="edi_doc_type_id", referencedColumnName="edi_doc_type_id")
* })
*/
private $ediDocType;
/**
* @var \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner
*
* @ORM\ManyToOne(targetEntity="Matrix\MatrixEdiBundle\Entity\EdiTradingPartner")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="edi_tp_id", referencedColumnName="edi_trading_partner_id")
* })
*/
private $ediTp;
/**
* Get ediTpTransactionsId
*
* @return integer
*/
public function getEdiTpTransactionsId()
{
return $this->ediTpTransactionsId;
}
/**
* Set ediDocType
*
* @param \Matrix\MatrixEdiBundle\Entity\EdiDocType $ediDocType
* @return EdiTradingPartnerTransactions
*/
public function setEdiDocType(\Matrix\MatrixEdiBundle\Entity\EdiDocType $ediDocType = null)
{
$this->ediDocType = $ediDocType;
return $this;
}
/**
* Get ediDocType
*
* @return \Matrix\MatrixEdiBundle\Entity\EdiDocType
*/
public function getEdiDocType()
{
return $this->ediDocType;
}
/**
* Set ediTp
*
* @param \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner $ediTp
* @return EdiTradingPartnerTransactions
*/
public function setEdiTp(\Matrix\MatrixEdiBundle\Entity\EdiTradingPartner $ediTp = null)
{
$this->ediTp = $ediTp;
return $this;
}
/**
* Get ediTp
*
* @return \Matrix\MatrixEdiBundle\Entity\EdiTradingPartner
*/
public function getEdiTp()
{
return $this->ediTp;
}
}
我想插入EdiTradingPartnerTransactions表但我真的不知道该怎么办,因为我收到了这个错误:
可捕获的致命错误:参数1传递给 矩阵\ MatrixEdiBundle \实体\ EdiTradingPartnerTransactions :: setEdiTp() 必须是一个实例 Matrix \ MatrixEdiBundle \ Entity \ EdiTradingPartner,给定的字符串,调用 在 C:\ XAMPP \ htdocs中\ Editracker \ SRC \矩阵\ MatrixEdiBundle \控制器\ MatrixController.php 在第474行并在中定义 C:\ XAMPP \ htdocs中\ Editracker的\ src \矩阵\ MatrixEdiBundle \实体\ EdiTradingPartnerTransactions.php 第85行
这是我在控制器方面的功能:
public function deleteTpTransAction($tpId, $docType, $direction, $tpName) {
$em = $this->getDoctrine()->getManager();
$docTypeId = $em->getRepository('MatrixEdiBundle:EdiDocType')->getId($docType, $direction);
if ($docTypeId != null) {
$result = $em->getRepository('MatrixEdiBundle:EdiTradingPartnerTransactions')->getTpTrans($tpId, $docType, $direction);
if ($result == null) {
$transaction = new EdiTradingPartnerTransactions();
$transaction->setEdiTp($tpId);
$transaction->setEdiDocType($docTypeId);
$em->persist($transaction);
} else {
foreach ($result as $key) {
$id = $key->getEdiTpTransactionsId();
$transaction = $em->getRepository('MatrixEdiBundle:EdiTradingPartnerTransactions')->find($id);
$em->remove($transaction);
}
}
$em->flush();
}
return $this->redirect($this->generateUrl('matrix_edi_tpTrans'));
}
真的很感激任何帮助。先谢谢!
答案 0 :(得分:2)
这里的错误非常明确:至少对于关系,Doctrine句柄只处理&#34;相关的对象&#34; (让我们这样称呼)实体。
当你这样做时
$transaction->setEdiTp($tpId);
$transaction->setEdiDocType($docTypeId);
您正在尝试插入相关对象的ID。这是一个错误:虽然你的db需要一个整数(外键),但是Doctrine是一个抽象的&#34;枕头&#34;你的代码和数据库之间;它选择使用对象而不是id(你可以很清楚地看到实体类文件)
此处的解决方案非常简单:
setEdiTp
和setEdiDocType
在PHP(Symfony)中:
$ediTp = $em
->getRepository('MatrixEdiBundle:EdiTp')
->findOneById($tpId);
$ediDocType = $em
->getRepository('MatrixEdiBundle:EdiDocType')
->findOneById($docTypeId);
然后
$transaction->setEdiTp($ediTp);
$transaction->setEdiDocType($ediDocType);
答案 1 :(得分:1)
您的错误是因为setEdiTp
需要EdiTradingPartner
的实例。
您的错误说string given
。
因为$tpId
是字符串。
更改
$transaction->setEdiTp($tpId);
要强>
// $transaction->setEdiTp($tpId);
var_dump($tpId);
通常,$ tpId返回一个字符串,而不是一个对象。
因此,当您致电deleteTpTransAction
时,$tpId
必须是setEdiTp
的实例
$editp = $em->getRepository('MatrixEdiBundle:EdiTradingPartner')->find($tpId);
并更改
$transaction->setEdiTp($tpId);
按强>
$transaction->setEdiTp($editTp);