symfony2控制器在视图中工作正常,但在加载期间不会:fixtures

时间:2013-04-08 19:20:53

标签: symfony controller doctrine entity fixtures

我正在努力使用Controller,EntityRepository和我在Symfony2中的Doctrine Fixtures的组合。

我的ImageParts实体是一些图像。我想随机生成图像。

所以,我有一个名为ImagePart的实体,以及一个名为'ImagePartRepository'的EntityRepository。

在该EntityRepository中,我创建了一个名为'getRandomImagePart()'的函数,该函数在使用路由进行测试时工作正常。

但我无法弄清楚如何在我的灯具中使用此功能。我认为我必须申报一项服务,但即便如此,我也无法让它发挥作用。我得到的错误消息类型告诉我,我显然在做一些结构上错误的事情。

此外,我想知道我是否使用Symfony2框架的正确方法,功能。例如:我是否可以在我的灯具中使用EntityRepository。

控制器:

    <?php

    namespace AMM\AMMBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

    use Symfony\Component\DependencyInjection\ContainerInterface;
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    use Symfony\Component\Finder\Finder;

    use AMM\AMMBundle\Entity\ImagePart;
    use Doctrine\ORM\Mapping as ORM;


    class ImagePartController extends Controller implements ContainerAwareInterface
    {
        /**
         * @var ContainerInterface
         */
        protected $container;

        /**
         * {@inheritDoc}
         */
        public function setContainer(ContainerInterface $container = null)
        {
            $this->container = $container;
        }


        public function getRandomImagePartAction($type)
        {
            /*
            $sql = " 
                SELECT a.imagePartBase64 FROM imageparts a
                WHERE a.imagePartCategory = '".$type."'
                ORDER BY RAND()
                LIMIT 1
            ";
            */

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

            $imagePart = $em->createQueryBuilder()
                        ->select('g')
                        ->from('AMMBundle:ImagePart',  'i')
                        ->addOrderBy('i.id', 'ASC')
                        ->getQuery()
                        ->getSingleResult();

          // $conn = $this->get('database_connection');
            //$randomimagepart = $conn->fetchAll($sql);


           // return $randomimagepart[0]['imagePartBase64'];

            return $imagePart;
        }

// ..

public function generateRandomImage()
    {
      $object->getRandomImagePartAction('BACKGROUND');
    }

Fixture文件ImageFixtures.php

<?php
namespace AMM\AMMBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

use AMM\AMMBundle\Entity\ImagePart;

class GenerateImages implements FixtureInterface, ContainerAwareInterface, OrderedFixtureInterface
{
    private $container;

    public function load(Objectmanager $manager)
    {
        $image= new Image();

        $imageGenerator = $this->container
                               ->get('imageGenerator')
                                ->GenerateRandomImage();

        $manager->persist($image);      
        $manager->flush();

}

我的services.yml:

services:
    imageGenerator:
        class:        AMM\AMMBundle\Controller\ImagePartController

所以,基本上我所做的是从同一个控制器中的函数调用一个函数。这在测试时可以正常工作,例如,在视图中。

但是在尝试加载doctrine fixture时,会发生以下错误:

致命错误:在C:\ wamp \ www \ amm \ vendor \ symfony \ symfony \ src \ Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller.php上的非对象上调用成员函数has() 198

堆栈跟踪结束:

5.7077   41983128  15. AMM\AMMBundle\Controller\ImagePartController->getRandomImagePartAction() C:\wamp\www\amm\src\AMM\AMMBundle\Controller\ImagePartController.php:119
5.7077   41983128  16. Symfony\Bundle\FrameworkBundle\Controller\Controller->getDoctrine() C:\wamp\www\amm\src\AMM\AMMBundle\Controller\ImagePartController.php:44

1 个答案:

答案 0 :(得分:0)

好的,我发现了问题。

我必须将此添加到我的services.yml:

calls: - [setContainer, [@service_container]] –
相关问题