大家好!
我有两个关联实体(User,Picture),带有cascade =“remove”选项,并且都可以删除软件
当我使用softdelete删除用户时,应该将图片删除。现在它不起作用。
没有softdelete一切都很完美。
任何人都可以帮我解决这个问题吗?
我的代码:
Picture.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
User.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'));
}
`
答案 0 :(得分:5)
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