Php 插入Doctrine2/Symfony2上的实体时id错误
几天以来,我和我的一个实体发生了问题。 此实体的ID需要手动定义(无自动增量)。但当我试图定义它时,在insert into查询中返回一个错误: DBALException:使用参数[null]、“Test”、“Hebert”执行“插入InfosPerso(num、login、nom、prenom、email、societe、adr1、adr2、cp、ville、tel、fax、nichandle、parrain、actif、state、emailvalide、date_creation、id_entite_legale_pays、pays)”值(?、、、、、、、、、、、、、、、、、、、、、、、?)时发生异常“多诺万”test@mail.com“,”SNCF“,”因临时性和非临时性的铜酸盐含量引起的临时性法律责任除外“,”0,空,1,0,1,“2015-06-04 11:09:46.000”,空,171]: SQLSTATE[HY000]:常规错误:515常规SQL Server错误:检查来自SQL Server的消息[515](严重性16)[(空)] 以下是我当前的代码:Php 插入Doctrine2/Symfony2上的实体时id错误,php,symfony,doctrine,Php,Symfony,Doctrine,几天以来,我和我的一个实体发生了问题。 此实体的ID需要手动定义(无自动增量)。但当我试图定义它时,在insert into查询中返回一个错误: DBALException:使用参数[null]、“Test”、“Hebert”执行“插入InfosPerso(num、login、nom、prenom、email、societe、adr1、adr2、cp、ville、tel、fax、nichandle、parrain、actif、state、emailvalide、date_creation、id_
$infosPerso = new InfosPerso();
$infosPerso->setLogin($client->getLogin())
->setParrain($leaderBmcId)
->setNicHandle(0)
->setNom($client->getName())
->setPrenom($client->getFirstname())
->setEmail($client->getEmail())
->setAdr1($client->getAddress())
->setAdr2($client->getComplementaryAddress())
->setCp($client->getZipCode())
->setDateCreation($client->getCreationDate())
->setTel($client->getPhone())
->setFax($client->getFax())
->setVille($client->getCity())
->setPays($client->getCountry())// Entity Pays
->setSociete($client->getCompany()->getLibCompanies())
->setEmailValide(true)
->setState(0)
->setActif(1);
以及ID的实体定义:
/**
* @var integer $num
*
* @ORM\Column(name="num", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="BizDatasBundle\Database\BMC\InfosPersoIdGenerator")
*/
private $num;
InfosPersoIdGenerator类的定义如下:
<?php
namespace BizDatasBundle\Database\BMC;
use Doctrine\ORM\Id\AbstractIdGenerator;
class InfosPersoIdGenerator extends AbstractIdGenerator
{
public function generate(\Doctrine\ORM\EntityManager $em, $entity)
{
$maxId = $em->createQueryBuilder('ip')
->select('MAX(ip.num)')
->from('BizDatasBundle:BMC\\InfosPerso', 'ip')
->getQuery()
->getSingleScalarResult();
$maxId++;
return $maxId;
}
}
好的,让我们看看:
您说转储$maxId
返回正确的值,但如果仔细查看您提供的代码,实际上并没有将该值设置为InfosPerso
。更可能的是缺少->setNum($maxId)
顺便说一下,您可以在SQL查询中清楚地看到:
INSERT INTO InfosPerso (num...) // and the rest of your columns here
紧接着:
with params [null,...]
如您所见,您没有为列提供值
最后一点,如果你想自己生成ID,你可以删除GeneratedValue
annotation
NONE:告诉Dority标识符是由您的代码分配(并由此生成的)。分配必须在新实体传递给EntityManager#persist之前进行。NONE与完全取消@GeneratedValue是一样的
好的,让我们看看:
您说转储$maxId
返回正确的值,但如果仔细查看您提供的代码,实际上并没有将该值设置为InfosPerso
。更可能的是缺少->setNum($maxId)
顺便说一下,您可以在SQL查询中清楚地看到:
INSERT INTO InfosPerso (num...) // and the rest of your columns here
紧接着:
with params [null,...]
如您所见,您没有为列提供值
最后一点,如果你想自己生成ID,你可以删除GeneratedValue
annotation
NONE:告诉Dority标识符是由您的代码分配(并由此生成的)。分配必须在新实体传递给EntityManager#persist之前进行。NONE与完全取消@GeneratedValue是一样的
我使用CUSTOM
生成的值,该值指示使用外部类生成ID(InfosPersoIdGenerator
),在不使用GeneratedValue
时,使用setNum()
方法,结果是相同的…我使用自定义
生成的值,它指示Doctrine使用外部类生成ID(InfosPersoIdGenerator
),在不使用GeneratedValue
时,使用setNum()
方法,结果是相同的…我使用自定义
生成的值,它指示条令使用外部类生成ID(InfosPersoIdGenerator
),在这种情况下,当我不使用GeneratedValue
并使用setNum()
方法时,结果是相同的。。。