2
votes

i want to delete a product after a known date this is my Controller :

public function indexAction()
{
    $em = $this->getDoctrine()->getManager();
    $productsInitial = $em->getRepository('ProductBundle:Product')->findAll();

    $productsAfterdelete=$em->getRepository('ProductBundle:Product')->deleteApresDure($productsInitial);


    return $this->render('product/index.html.twig', array(
        'products' => $productsAfterdelete,
    ));
}

my function in repository class :

public function deleteApresDuree($products)
    {
        $datenow=new \DateTimeImmutable();
        foreach ($products as $produit){
            $datecreation=$produit->getCreatedDate();
            $result = $datecreation->format('Y-m-d');
            $date_fin = date('Y-m-d', strtotime($result.' +15 days'));
            if ( $datenow>$date_fin )
            {
                unset($products[$produit]);
                array_values($products);

            }
        }
    return $products;

    }

/** * Produit * @ORM\Entity(repositoryClass="\ProductBundle\Repository\ProductRepository") * @Vich\Uploadable * @ORM\Table(name="produit") */

error :

Undefined method 'deleteApresDure'. The method name must start with either findBy or findOneBy!

pleaaase ANY HELP !!

1
The error message means your repository is not properly mapped to the entity. Make sure the cache has been cleared and that you have no typos. Other more obscure things could also be wrong. There are several hundred other questions with the exact same problem.Cerad
i mention that ! that i didn't have any annotation problem, i've searched in the several hundrer others question and it was a different problem.Jamel Mustapha
any way i copied my function to the controller and it worked thnx by the wayJamel Mustapha
Glad it is working but what you described is more of a hack than a solution. In the long run, figuring out how to use custom repositories will pay off.Cerad
yep i know that i didn't have the time to read about reposotories i'll do it later thank youJamel Mustapha

1 Answers

1
votes

you should change repositoryClass="\ProductBundle\Repository\ProductRepository with repositoryClass="ProductBundle\Repository\ProductRepository