Sylius - Error: Call to a member function get() on null

30 views
Skip to first unread message

Дмитрий Димитрий

unread,
May 28, 2017, 4:44:36 AM5/28/17
to Sylius
I'm learning Sylius and I'm following the documentation. I'm trying to get a list of products on the homepage. Here is my controller code:
 <?php

namespace AppBundle\Controller\Shop;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Repository\ProductRepository;

class HomepageController extends ResourceController

{ /** * @var EngineInterface */
private $templatingEngine;

/**
 * @param EngineInterface $templatingEngine
 */

public function __construct(EngineInterface $templatingEngine)
{
    $this
->templatingEngine = $templatingEngine;

}

/**
 * @param Request $request
 *
 * @return Response
 */

public function indexAction(Request $request)
{
    $productRepository
= $this->container->get('sylius.repository.product');

    $first
= $productRepository->findByOnHand();

   
return $this->templatingEngine->renderResponse('@SyliusShop/Homepage/index.html.twig', array('first' => $first));
}

}


But I'm constantly getting an error with this line:
$productRepository = $this->get('sylius.repository.product');

my Repository:
<?php

namespace AppBundle\Repository;

use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Core\Model\ChannelInterface;

class ProductRepository extends BaseProductRepository
{
 
/**
 * {@inheritdoc}
 */

 
public function findLatestByChannelAndTaxonCode(ChannelInterface $channel, $code, $count)
 
{
 
return $this->createQueryBuilder('o')
 
->innerJoin('o.channels', 'channel')
 
->addOrderBy('o.createdAt', 'desc')
 
->andWhere('o.enabled = true')
 
->andWhere('channel = :channel')
 
->innerJoin('o.taxons', 'taxon')
 
->andWhere('taxon.code = :code')
 
->setParameter('channel', $channel)
 
->setParameter('code', $code)
 
->setMaxResults($count)
 
->getQuery()
 
->getResult()
 
;
 
}
}


my config:

sylius_product:
resources:
    product:
        classes:
            repository: AppBundle\Repository\ProductRepository

my routing:

sylius_shop_homepage:
    path: /en_US/
    methods: [GET]
    defaults:
        _controller: sylius.controller.shop.homepage:indexAction

and service:

services:
    sylius.controller.shop.homepage:
        class: AppBundle\Controller\Shop\HomepageController
        arguments: ['@templating']

And generally, that I did not do according to the documentation - nothing works. please, help!

Дмитрий Димитрий

unread,
May 28, 2017, 4:46:36 AM5/28/17
to Sylius
sorry, thats my rout:
 app_shop_partial_product_index_full:
    path: /en_US/ # configure a new path that has all the needed variables
methods: [GET]
defaults:
_controller: sylius.controller.product:indexAction # you make a call on the Product Controller's index action
_sylius:
template: $template
repository:
method: findLatestByChannelAndTaxonCode # here use the new repository method
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- $code
- $count
Reply all
Reply to author
Forward
0 new messages