php 删除 Doctrine 中的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19540870/
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
Delete records in Doctrine
提问by Hassan Sardar
I'm trying to delete a record in Doctrine, but I don't know why it's not deleting.
我正在尝试删除 Doctrine 中的记录,但我不知道为什么它没有删除。
Here is my Code:
这是我的代码:
function del_user($id)
{
$single_user = $entityManager->find('Users', $id);
$entityManager->remove($single_user);
$entityManager->flush();
}
Plus: How can I echo query to see what going on here?
另外:我如何回显查询以查看这里发生了什么?
回答by Ramy Nasr
This is an old question and doesn't seem to have an answer yet. For reference I am leaving that here for more reference. Also you can check the doctrine documentation
这是一个老问题,似乎还没有答案。作为参考,我将其留在这里以供更多参考。您也可以查看学说文档
To delete a record, you need to ( assuming you are in your controller ):
要删除记录,您需要(假设您在控制器中):
// get EntityManager
$em = $this->getDoctrine()->getManager();
// Get a reference to the entity ( will not generate a query )
$user = $em->getReference('ProjectBundle:User', $id);
// OR you can get the entity itself ( will generate a query )
// $user = $em->getRepository('ProjectBundle:User')->find($id);
// Remove it and flush
$em->remove($user);
$em->flush();
Using the first method of getting a referenceis usually better if you just want to delete the entity without checking first whether it exists or not, because it will not query the DB and will only create a proxy object that you can use to delete your entity.
如果您只想删除实体而不先检查它是否存在,则使用第一种获取引用的方法通常会更好,因为它不会查询数据库并且只会创建一个可用于删除实体的代理对象.
If you want to make sure that this ID corresponds to a valid entity first, then the second method is better because it will query the DB for your entity before trying to delete it.
如果您想首先确保此 ID 对应于有效实体,则第二种方法更好,因为它会在尝试删除实体之前查询数据库中的实体。
回答by Vladtn
In a Silex route I do like this, in case it helps someone:
在 Silex 路线中,我喜欢这样做,以防它对某人有所帮助:
$app->get('/db/order/delete', function (Request $request) use ($app) {
...
$id = $request->query->get('id');
$em = $app['orm.em']; //or wherever your EntityManager is
$order = $em->find("\App\Entity\Orders",$id); //your Entity
if($order){
try{
$em->remove($order);
$em->flush();
}
catch( Exception $e )
{
return new Response( $e->getMessage(), 500 );
}
return new Response( "Success deleting order " . $order->getId(), 200 );
}else{
return new Response("Order Not Found", 500);
}
}
回答by NLZ
try a var_dump()
of your $single_user
. If it is "null
", it doens't exist ?
试试var_dump()
你的$single_user
. 如果是“ null
”,它不存在?
Also check if "Users" is a valid Entity name (no namespace?), and does the $id reference the PK of the user?
还要检查“Users”是否是有效的实体名称(没有命名空间?),并且 $id 是否引用了用户的 PK?
If you want to see the queries that are executed check your mysql/sql/... log or look into Doctrine\DBAL\Logging\EchoSQLLogger
如果您想查看执行的查询,请检查您的 mysql/sql/... 日志或查看 Doctrine\DBAL\Logging\EchoSQLLogger
回答by Mihai Enescu
You first need repository.
您首先需要存储库。
$entityManager->getRepository('Users')->find($id);
instead of
代替
$single_user = $entityManager->find('Users', $id);
'Users' String is the name of the Users repository in doctrine ( depends if you are using Symfony , Zend . . etc ).
'Users' 字符串是指用户存储库的名称(取决于您是否使用 Symfony 、 Zend . . 等)。
回答by ibasaw
do you check your entity as the good comment annotation ?
你检查你的实体作为良好的评论注释吗?
cascade={"persist", "remove"}, orphanRemoval=true
回答by Pradeep
First, You may need to check if 'Users' is your fully qualified class name. If not check, and update it to your class name with the namespace info.
首先,您可能需要检查“用户”是否是您的完全限定类名。如果没有检查,并使用命名空间信息将其更新为您的类名。
Make sure the object returned by find() is not null or not false and is an instance of your entity class before calling EM's remove().
在调用 EM 的 remove() 之前,请确保 find() 返回的对象不为 null 或不为 false,并且是实体类的实例。
Regarding your other question, instead of making doctrine return SQL's I just use my database (MySQL) to log all queries (since its just development environment).
关于你的另一个问题,我只是使用我的数据库(MySQL)来记录所有查询(因为它只是开发环境),而不是让学说返回 SQL。