简体   繁体   English

删除 Doctrine 中的记录

[英]Delete records in Doctrine

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?另外:我如何回显查询以查看这里发生了什么?

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 reference is 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首先对应于有效实体,则第二种方法更好,因为它会在尝试删除实体之前查询数据库中的实体。

For my understanding if you need to delete a record in doctrine that have a doctrine relationship eg.据我了解,如果您需要删除具有学说关系的学说中的记录,例如。 OneToMany, ManyToMany and association cannot be easy deleted until you set the field that reference to another relation equal to null. OneToMany、ManyToMany 和关联不能轻易删除,除非您将引用另一个关系的字段设置为 null。

...... ......

you can use this for non relation doctrine您可以将其用于非关系学说

  $entityManager=$this->getDoctrine()->getManager();
 $single_user=$this->getDoctrine()->getRepository(User::class)->findOneBy(['id'=>$id]);

    $entityManager->remove($single_user);

    $entityManager->flush();
                       

but for relation doctrine set the field that reference to another relation to null但是对于关系学说,将引用另一个关系的字段设置为空

$entityManager=$this->getDoctrine()->getManager();
     $single_user=$this->getDoctrine()->getRepository(User::class)->findOneBy(['id'=>$id]);
     {# assume you have field that reference #}
       
       $single_user->setFieldData(null);
        $entityManager->remove($single_user);

    $entityManager->flush();

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);
  }
}

try a var_dump() of your $single_user . 尝试$single_uservar_dump() If it is " null ", it doens't exist ? 如果它是“ 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

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'String是doctrine中Users用户存储库的名称(取决于您使用的是Symfony,Zend。等)。

你检查你的实体是好注释注释吗?

cascade={"persist", "remove"}, orphanRemoval=true

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(). 确保find()返回的对象不为null或不为false,并且在调用EM的remove()之前是实体类的实例。

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)来记录所有查询(因为它只是开发环境),而不是让doctrine返回SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM