RedisCache()不接受Predis / Client()作为Redis的实例

时间:2016-12-12 05:41:13

标签: php symfony doctrine-orm redis

我已经坚持了几天。我尝试在网上搜索类似的问题,但没有骰子:(

我遵循了guide

它有点老了,我使用了Doctrine \ Common \ Cache \ RedisCache()而不是snc。问题在于每当我尝试运行时:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Predis\Client;
use Doctrine\Common\Cache\RedisCache;

class DefaultController extends Controller
{
    /**
     * @Route("/Worker", name="homepage")
     */
    public function indexAction()
    {

            $predis = new RedisCache();
            $predis->setRedis(new Client());

        return $this->render('default/index.html.twig', array(
            'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
        ));
    }
}

它给了我一个错误

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Cache\RedisCache::setRedis() must be an instance of Redis, instance of Predis\Client given, called in C:\xampp\htdocs\symfony\src\AppBundle\Controller\DefaultController.php on line 22 and defined

我在composer.json btw

中需要predis / predis 1.1.1
"require": {
    "php": ">=5.3.9",
    "symfony/symfony": "2.8.*",
    "doctrine/orm": "^2.4.8",
    "doctrine/doctrine-bundle": "~1.4",
    "symfony/swiftmailer-bundle": "~2.3",
    "symfony/monolog-bundle": "~2.11.3",
    "sensio/distribution-bundle": "~5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "~2.0",
    "snc/redis-bundle": "2.x-dev",
    "predis/predis": "1.1.1"

2 个答案:

答案 0 :(得分:1)

事实证明,如果我想使用predis,我必须使用Doctrine \ Common \ Cache \ PredisCache()和Predis \ Client()。 (链接中使用的方法已过时)

use Predis\Client;
use Doctrine\Common\Cache\PredisCache;

$predis = new PredisCache(new Client());

答案 1 :(得分:0)

尝试:

        $predis->setRedis(new Client());

而不是:

 $SellerProducts = TableRegistry::get('SellerProducts');
 $resultSet = $SellerProducts->find('all')->hydrate(false)
                    ->select(['SellerProducts.id'])                           
                    ->join([
                        'Products'=>  [
                            'table'      => 'products',
                            'type'       => 'INNER',
                            'conditions' => 'Products.id = SellerProducts.product_id',
                        ],
                        'Brands'=>  [
                            'table'      => 'brands',
                            'type'       => 'INNER',
                            'conditions' => 'Brands.id = Products.brand_id',
                        ],
                        'ProductColors'=>  [
                            'table'      => 'product_colors',
                            'type'       => 'INNER',
                            'conditions' => 'ProductColors.product_id = Products.id',
                        ],
                        'Colors'=>  [
                            'table'      => 'colors',
                            'type'       => 'INNER',
                            'conditions' => 'Colors.id = ProductColors.color_id',
                        ]
                    ])
                    ->where([
                        'Products.subcategory_id' => $id,
                        'SellerProducts.stock >'  => 0
                    ])
                    ->toArray();
    print_r($resultSet);

希望这个帮助