0
在我的数据库中,我有许多产品具有datecreated属性。 我想在刷新我的主页之后的一段时间(如10或20天)后删除任何产品。过期后从数据库中删除错误
这是我的控制器:
public function indexAction()
{
$datenow = new \DateTimeImmutable();
$datenowStringType = $datenow->format('Y-m-d');
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('ProductBundle:Product')->findAll();
foreach ($products as $produit) {
$CreatedDate = $produit->getCreatedDate();
$CreatedDate_StringType = $CreatedDate->format('Y-m-d');
$duree = '+'.$produit->getDuree();
$date_fin_duree = date('Y-m-d', strtotime($CreatedDate_StringType.$duree));
if (strtotime($datenowStringType)<strtotime($date_fin_duree)) {
$em->remove($produit);
}
}
array_values($products);
return $this->render('product/index.html.twig', array(
'products' => $products,
));
}
有没有返回错误,但产品不会被删除。这可能是一个简单的错误,但我搜索了很多,我认为这是一个不集中。 请帮忙?
你在哪里调用flush? – Cerad
我打电话给我后,我删除,但他删除产品,即使条件是假的 –
谢谢Rhono你有什么解决方案吗? –