不作为新记录插入而是替换先前的记录

时间:2013-08-20 07:00:09

标签: symfony doctrine-orm

我尝试在数据库表中插入新记录,而不是替换表中的记录。 (目前我在这张表中有一条记录)。有什么想法吗?这是我的疑问:

public function setExperimentData($name, $type, $description) {
    $updateQuery = $this->repository->createQueryBuilder('ud')
        ->update()
        ->set('ud.name', $this->em->createQueryBuilder()->expr()->literal($name))
        ->set('ud.type', $this->em->createQueryBuilder()->expr()->literal($type))
        ->set('ud.description', $this->em->createQueryBuilder()->expr()->literal($description))                                      
        ->getQuery();
    $updateQuery->execute();
}  

是的,我尝试了以下(实验()是实体):

$experiment = new Experiments();
$experiment->setName($experimentName);        
$experiment->setDescription($experimentDescription);
$experiment->setPrimaryinvestigator($primaryInvestigator);

然后我收到以下错误:

捕获致命错误:传递给Test \ HelloBundle \ Entity \ Experiments :: setPrimaryinvestigator()的参数1必须是Test \ HelloBundle \ Entity \ Users

的实例

以下是我在实体类中的主要调查员的设定者:

public function setPrimaryinvestigator(\Test\HelloBundle\Entity\Users $primaryinvestigator = null)
    {
        $this->primaryinvestigator = $primaryinvestigator;

        return $this;
    }

我做错了什么?

更新:

它传递的整数.E,g,1是PrimaryInvestigator的id。

1 个答案:

答案 0 :(得分:1)

我认为QueryBuilder目前不支持插入查询。假设你有一个带有setter的Experiment实体,你试过吗

public function setExperimentData($name, $type, $description) {
    $em = $this->getDoctrine()->getManager();
    $experiment = new Experiment();
    $experiment->setName($name);
    $experiment->setType($type);
    $experiment->setDescription($description);
    $em->persist($experiment);
    $em->flush();
}