public function toggleAction(Request $request, AbstractAdmin $admin)
    {
        $id = $request->attributes->get('id');
        $entity = $this->getDoctrine()->getRepository($admin->getEntityClass())->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('The resource cannot be found');
        }

        $this->secure($admin, ContentVoter::UPDATE_CONTENT, $entity);
        $this->checkCsrf($request->attributes->get('token'));

        $field = $request->attributes->get('field');

        // Get current entity property value
        $accessor = PropertyAccess::createPropertyAccessor();
        $current = $accessor->getValue($entity, $field);
        // Update it to its contrary
        $accessor->setValue($entity, $field, !$current);

        try {
            // Apply
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return new JsonResponse(!$current, 200);
        } catch (Exception $e) {
            // return json response
            return new JsonResponse($current, 500);
        }
    }
public function toggleAction(Request $request, AbstractAdmin $admin)
    {
        $id = $request->attributes->get('id');
        $entity = $this->getDoctrine()->getRepository($admin->getEntityClass())->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('The resource cannot be found');
        }

        $this->secure($admin, ContentVoter::UPDATE_CONTENT, $entity);
        $this->checkCsrf($request->attributes->get('token'));

        $field = $request->attributes->get('field');

        // Get current entity property value
        $accessor = PropertyAccess::createPropertyAccessor();
        $current = $accessor->getValue($entity, $field);
        // Update it to its contrary
        $accessor->setValue($entity, $field, !$current);

        try {
            // Apply
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return new JsonResponse(!$current, 200);
        } catch (Exception $e) {
            // return json response
            return new JsonResponse($current, 500);
        }
    }
public function toggleAction ( Request $ request , AbstractAdmin $ admin ) { $ id = $ request -> attributes -> get ( 'id' ) ; $ entity = $ this -> getDoctrine ( ) -> getRepository ( $ admin -> getEntityClass ( ) ) -> find ( $ id ) ; if ( ! $ entity ) { throw $ this -> createNotFoundException ( 'The resource cannot be found' ) ; } $ this -> secure ( $ admin , ContentVoter :: UPDATE_CONTENT , $ entity ) ; $ this -> checkCsrf ( $ request -> attributes -> get ( 'token' ) ) ; $ field = $ request -> attributes -> get ( 'field' ) ; // Get current entity property value $ accessor = PropertyAccess :: createPropertyAccessor ( ) ; $ current = $ accessor -> getValue ( $ entity , $ field ) ; // Update it to its contrary $ accessor -> setValue ( $ entity , $ field , ! $ current ) ; try { // Apply $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ em -> persist ( $ entity ) ; $ em -> flush ( ) ; return new JsonResponse ( ! $ current , 200 ) ; } catch ( Exception $ e ) { // return json response return new JsonResponse ( $ current , 500 ) ; } }
Toggles the value of a given field

@throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the resource cannot be loaded

@param \Symfony\Component\HttpFoundation\Request $request
@param \Wanjee\Shuwee\AdminBundle\Admin\AbstractAdmin $admin
@return \Symfony\Component\HttpFoundation\Response
Toggles the value of a given field