从存储库-Symfony

问题描述

我想获取添加到另一个灯具my problem is that i want to do that with doctrine中的一个灯具中的数据,诸如此类,但是我得到一个空数组

$em = $this->container->get('doctrine')->getEntityManager('default');
$repository = $em->getRepository('ModelBundle:Post');
$entities = $repository->findAll();

那么解决方案是什么?
可以做某事吗?
我认为,因为我在dataFixture中使用了刷新功能,所以它在我写入时将数据加载到数据库中吗?
CommentFixtures.PHP

<?PHP

namespace ModelBundle\DataFixtures\ORM;

use Blog\ModelBundle\Entity\Comment;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class CommentFixtures extends Fixture implements FixtureInterface,ContainerAwareInterface
{
    const MIN_COMMENT_COUNT = 1;
    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
    
    public function load(ObjectManager $manager)
    {
        $em = $this->container->get('doctrine')->getEntityManager('default');
        $repository = $em->getRepository('ModelBundle:Post');
        $entities = $repository->findAll();
    }

    public function getorder()
    {
        return 4;
    }
}

PostFixtures.PHP

<?PHP

namespace ModelBundle\DataFixtures\ORM;

use Blog\ModelBundle\Entity\Post;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class PostFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        for ($i = 0; $i < Objects::POST_COUNT; $i++) {
            $post = new Post();
            $post->setBody('Body '.$i);
            $post->setTitle('Title '.$i);
            $post->setAuthor(Objects::getInstance(Objects::AUTHOR,$this));
            $manager->persist($post);
        }

        $manager->flush();
    }

    public function getorder()
    {
        return 2;
    }
}

AuthorFixtures.PHP

<?PHP

namespace ModelBundle\DataFixtures\ORM;

use Blog\ModelBundle\Entity\Author;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class AuthorFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        for ($i = 0; $i < Objects::AUTHOR_COUNT; $i++) {
            $author = new Author();
            $author->setName('Name '.$i);
            $manager->persist($author);
            $this->addReference(Objects::AUTHOR.$i,$author);
        }

        $manager->flush();
        $this->addReference("ddddd",$author);

    }

    public function getorder()
    {
        return 1;
    }
}

然后我编写此Objects类以更轻松地处理对象 Objects.PHP

<?PHP

namespace ModelBundle\DataFixtures\ORM;


class Objects {
    const AUTHOR = 'author';
    const AUTHOR_COUNT = 10;

    const POST = 'post';
    const POST_COUNT = 30;

    const COUNT_POSTFIX = 'count';

    public static function getInstance($entityName,$fixture) {
        $entityCountKey = strtoupper($entityName)."_".strtoupper(self::COUNT_POSTFIX);
        $entityCount = constant('self::'. $entityCountKey);
        return $fixture->getReference($entityName.rand(0,($entityCount - 1)));
    }

    public static function getInstanceWithId($entityName,$fixture,$id) {
        $entityCountKey = strtoupper($entityName)."_".strtoupper(self::COUNT_POSTFIX);
        $entityCount = constant('self::'. $entityCountKey);
        return $fixture->getReference($entityName.$id);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)