database Symfony2,Doctrine,在没有 queryBuilder 的情况下更新数据库条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14599100/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2, Doctrine, update db entry without queryBuilder
提问by user1954544
To save entry to db we can use:
要将条目保存到 db,我们可以使用:
$em->persist($entity);
$em->flush();
But how can we update existing entry without using $this->getEntityManager()->createQuery()? 
但是我们如何在不使用的情况下更新现有条目$this->getEntityManager()->createQuery()?
Can we?
我们可以吗?
I'm searching some kind of $em->update()for existing entry in db.
我正在寻找某种$em->update()现有条目的数据库。
回答by user1954544
Simple way to do, Fusselchensaid right, just show an example
做法简单,Fusselchen说的对,举个例子
// get entity manager
$em = $this->getDoctrine()->getEntityManager();
// get from this entity manager our "entity" \ object in $item
// also we can get arrayCollection and then do all in foreach loop
$item = $em->getRepository('repoName')->findOneBy($filter);
// change "entity" / object values we want to edit
$item->setSome('someText')
//...
// call to flush that entity manager from which we create $item
$em->flush();
// after that in db column 'some' will have value 'someText'
// btw after call flush we can still use $item as 'selected object' in
// another $em calls and it will have actual (some = 'someText') values
回答by DonCallisto
No, it doesn't exist a function like $em->update().
You have to fetch object from DB and update it or, simply, write a custom query (with DQL) that update what you need
不,它不存在像$em->update(). 
您必须从数据库中获取对象并更新它,或者简单地编写一个自定义查询(使用 DQL)来更新您需要的内容
As you can see here
正如你在这里看到的
UPDATE MyProject\Model\User u SET u.password = 'new' WHERE u.id IN (1, 2, 3)
This is an example of DQL query for updating an entity named User
这是用于更新名为 User 的实体的 DQL 查询示例
Last but not least important, this query have to be placed into aspecial "class" called repository that will contain all custom sql (dql). This is a good practice.
最后但并非最不重要的一点是,必须将此查询放入称为存储库的特殊“类”中,该存储库将包含所有自定义 sql (dql)。这是一个很好的做法。
Learn more about repositories, here
在此处了解有关存储库的更多信息
回答by Fusselchen
- Get the Entity from DB
- Change the values you want to modify
- flush the entitymanager
- 从数据库获取实体
- 更改要修改的值
- 刷新实体管理器
no extra call for updating the database. The EntityManager keeps your model an Database in sync on flush()
无需额外调用更新数据库。EntityManager 使您的模型与数据库在 flush() 上保持同步
public function updateAction($id)
    {
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository('AppBundle:Product')->find($id);
    if (!$product) {
        throw $this->createNotFoundException(
            'No product found for id '.$id
        );
    }
    $product->setName('New product name!');
    $em->flush();
    return $this->redirectToRoute('homepage');
}
see http://symfony.com/doc/current/book/doctrine.html#updating-an-object
见http://symfony.com/doc/current/book/doctrine.html#updating-an-object

