2

每个人!

我有两个关联的实体(用户,图片),带有 cascade="remove" 选项并且都是可软删除的

当我使用softdelete删除用户时,应该把图片都删除了。现在它不工作了。

没有softdelete,一切都完美无缺。

谁能帮我解决这个问题?

我的代码:

图片.orm.yml

Leo\TestBundle\Entity\Picture:
type: entity
repositoryClass: Leo\TestBundle\Entity\PictureRepository
table: null
manyToOne:
   user:
       targetEntity: User
       inversedBy: pictures
       joinColumn:
           name: user_id
           referencedColumnName: id
           onDelete: CASCADE
gedmo:
    soft_deleteable:
      field_name: deletedAt
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    name:
        type: string
        length: 255
    path:
        type: string
        length: 255
    deletedAt:
        type: datetime
        nullable: true

用户.orm.yml:

Leo\TestBundle\Entity\User:
type: entity
table: null
repositoryClass: Leo\TestBundle\Entity\UserRepository
oneToMany:
    pictures:
        targetEntity: Picture
        mappedBy: user
gedmo:
    soft_deleteable:
      field_name: deletedAt
fields:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
    username:
        type: string
        length: 255
    email:
        type: string
        length: 255
    password:
        type: string
        length: 255
    sex:
        type: integer
    bloodtype:
        type: string
        length: 10
    birthday:
        type: date
    pr:
        type: text
    salt:
        type: string
        length: 255
    deletedAt:
        type: datetime
        nullable: true
lifecycleCallbacks: {  }

应用程序/配置/config.yml

doctrine:
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
        filters:
            softdeleteable:
                class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                enabled: true

行动

public function deleteAction()
{
    $token = $this->getRequest()->get('token');
    if (! $this->get('form.csrf_provider')->isCsrfTokenValid('user_list', $token)) {
        //TODO use setFlashBag
        $this->get('session')->setFlash('notice', 'Woops! Token invalid!');
        return $this->redirect('user_list');
    }
    //$em = $this->getDoctrine()->getEntityManager();
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $pictures = $user->getPictures();
    $em->remove($user);
    /* foreach( $pictures as $p ){
        unlink($p->getAbsolutePath());
    } */
    $em->flush();
    $this->get('security.context')->setToken(null);
    $this->getRequest()
         ->getSession()
         ->invalidate();
    return $this->redirect($this->generateUrl('leo_test_homepage'));
}

`

4

1 回答 1

6

OK,问题终于找到了。是我的粗心。

Leo\TestBundle\Entity\Picture:
type: entity
repositoryClass: Leo\TestBundle\Entity\PictureRepository
table: null
manyToOne:
   user:
       targetEntity: User
       inversedBy: pictures
       joinColumn:
           name: user_id
           referencedColumnName: id
           #**Comment out the line of code**
           #onDelete: CASCADE
gedmo:
    soft_deleteable:
      field_name: deletedAt

用户实体

Leo\TestBundle\Entity\User:
type: entity
table: null
repositoryClass: Leo\TestBundle\Entity\UserRepository
oneToMany:
    pictures:
        targetEntity: Picture
        mappedBy: user
        # add cascade option here
        cascade: [persist, remove]
gedmo:
    soft_deleteable:
      field_name: deletedAt
于 2013-08-09T01:53:39.730 回答