Home > Blockchain >  Call to a member function has() on null
Call to a member function has() on null

Time:10-16

im learning symfony. I have a twig using twig extension and i found that i cannot call a function from other controller inside twig extension. so i have decide to use a service since the twig extension is using the service. so i tried to call a function from controller in the service and call the function from the service in the twig extension. Then, i have an error message that [Call to a member function has() on null], where $doct = $this->getDoctrine()->getManager(); in the controller.

this is the function in the controller that i'm trying to use

edited

namespace Customize\Controller;

use Customize\Entity\CustomRing;
use Customize\Entity\Ring;
use Customize\Repository\CustomProductRepository;
use Eccube\Controller\AbstractController;
use Eccube\Entity\BaseInfo;
use Eccube\Entity\Product;
use Eccube\Entity\ProductClass;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\ProductClassRepository;
use Eccube\Service\CartService;
use Eccube\Service\OrderHelper;
use Eccube\Service\PurchaseFlow\PurchaseContext;
use Eccube\Service\PurchaseFlow\PurchaseFlow;
use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
use Google\Service\Monitoring\Custom;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class CartController extends AbstractController
{
    /**
     * @var ProductClassRepository
     */
    protected $productClassRepository;

    /**
     * @var CartService
     */
    protected $cartService;

    /**
     * @var PurchaseFlow
     */
    protected $purchaseFlow;

    /**
     * @var BaseInfo
     */
    protected $baseInfo;

    /**
     * @var CustomProductRepository
     */
    protected $customProductRepository;

    /**
     * CartController constructor.
     *
     * @param ProductClassRepository $productClassRepository
     * @param CartService $cartService
     * @param PurchaseFlow $cartPurchaseFlow
     * @param BaseInfoRepository $baseInfoRepository
     * @param CustomProductRepository $customProductRepository
     */
    public function __construct(
        ProductClassRepository $productClassRepository,
        CartService $cartService,
        PurchaseFlow $cartPurchaseFlow,
        BaseInfoRepository $baseInfoRepository,
        CustomProductRepository $customProductRepository
    ) {
        $this->productClassRepository = $productClassRepository;
        $this->cartService = $cartService;
        $this->purchaseFlow = $cartPurchaseFlow;
        $this->baseInfo = $baseInfoRepository->get();
        $this->customProductRepository = $customProductRepository;
    }
public function image_route(){
        $Carts = $this->cartService->getCarts();
        $doct = $this->getDoctrine()->getManager();
        $custom_images = array();
        $Custom_product = $this->customProductRepository->customProductFindByName();
        $Product = $doct->getRepository(Product::class)->find($Custom_product[0]->getId());
        foreach ($Carts as $Cart) {
            $items = $Cart->getCartItems();
            foreach($items as $item ){
                if($item->getProductClass()->getProduct()->getId() == $Product->getId()){
                    $custom = $item->getProductClass()->getCode();
                    
                    $custom = $doct->getRepository(CustomRing::class)->find($custom);
                    $ring = $doct->getRepository(Ring::class)->find($custom->getRingBaseId());
                    $upload_directory= $this->getParameter('uploads_directory'); 
                    $ring_shape = $ring->getRingShape();
                    $ring_type = $ring->getRingType();
                    $upload = $upload_directory.'/customRing/ring/'.$ring_shape.'/'.$ring_type.'/';
                    $images = glob($upload."*.{jpg,png,jpeg,JPG,JPEG,PNG}", GLOB_BRACE);
                    for($i=0;$i<count($images);$i  ){
                        $aa = explode('save_image/', $images[$i]);
                        $images[$i] = $aa[1];
                    }
                    array_push($custom_images,$images[0]);
                }
            }
        }
        return $custom_images;
use Customize\Controller\CartController;
 /**
 * @var CartController
 */
 protected $cartController;


public function __construct(
        SessionInterface $session,
        EntityManagerInterface $entityManager,
        ProductClassRepository $productClassRepository,
        CartRepository $cartRepository,
        CartItemComparator $cartItemComparator,
        CartItemAllocator $cartItemAllocator,
        OrderRepository $orderRepository,
        TokenStorageInterface $tokenStorage,
        AuthorizationCheckerInterface $authorizationChecker,
        CartController $CartController
    ) {
        $this->session = $session;
        $this->entityManager = $entityManager;
        $this->productClassRepository = $productClassRepository;
        $this->cartRepository = $cartRepository;
        $this->cartItemComparator = $cartItemComparator;
        $this->cartItemAllocator = $cartItemAllocator;
        $this->orderRepository = $orderRepository;
        $this->tokenStorage = $tokenStorage;
        $this->authorizationChecker = $authorizationChecker;
        $this->cartController = $CartController;
    }
/*other functions
.
.
*/

public function imagess(){
        $temp = $this->cartController->image_route();
        
        return $temp;
    }

and this is the twig extension

use Eccube\Service\CartService;
class CartServiceExtension extends AbstractExtension
{
    /**
     * @var CartService
     */
    protected $cartService;

    public function get_image_route(){
        $t = $this->cartService->imagess();
        return $t;
        //return ['11', '12'];
    }

}

The line i got error in controller is

$doct = $this->getDoctrine()->getManager();

and when go inside getDoctrine()

trait ControllerTrait{

/**
     * Shortcut to return the Doctrine Registry service.
     *
     * @return ManagerRegistry
     *
     * @throws \LogicException If DoctrineBundle is not available
     *
     * @final
     */
    protected function getDoctrine()
    {
        if (!$this->container->has('doctrine')) {
            throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require symfony/orm-pack".');
        }

        return $this->container->get('doctrine');
    }

I don't know why but it seems has() function inside getDoctrine() returns null

how can i use the function from the controller so that i can pass the values of $custom_images into the twig?

CodePudding user response:

To get Doctrine in any controller action, you can do away with that function/trait entirely and just inject the EntityManagerInterface into your action.

For example:

class SomeCustomController extends AbstractController
{
    public function someControllerAction(Request $request, EntityManagerInterface $em): Response
    {
        // business logic
    }
}
  • Related