gpt4 book ai didi

symfony - 关联实体与 onDelete : CASCADE option and both soft-deleteable not working

转载 作者:行者123 更新时间:2023-12-02 01:10:26 27 4
gpt4 key购买 nike

大家好!

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

当我用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

Action

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

`

最佳答案

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

关于symfony - 关联实体与 onDelete : CASCADE option and both soft-deleteable not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com