Symfony 2.8 Doctrine Fixtures

用户1841749

我正在学习 symfony 框架,我目前安装了 Symfony 2.8。我正在使用 Doctrine Fixtures 加载一些数据 BlogFixtures、UserFixtures 和 TagFixtures(TagFixtures 不是问题)。

尝试时php app/console doctrine:fixtures:load我得到Catchable Fatal Error: Object of class Proxies\__CG__\BlogBundle\Entity\User could not be converted to string.

这是 BlogFixtures 中的函数:

class BlogFixtures extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface

{
use ContainerAwareTrait;
use FixturesTrait;

public function load(ObjectManager $manager)
{
    foreach ($this->getRandomPostTitles() as $i => $title) {
        $blog = new Blog();

        $blog->setName($title);
        $blog->setShortDescription($this->getRandomPostSummary());
        $blog->setSlug($this->container->get('slugger')->slugify($blog->getName()));
        $blog->setContent($this->getPostContent());
        // "References" are the way to share objects between fixtures defined
        // in different files. This reference has been added in the UserFixtures
        // file and it contains an instance of the User entity.
        $blog->setAuthor($this->getReference('jane-admin'));
        $blog->setUpdatedAt(new \DateTime('now - '.$i.'days'));
        $blog->setPostedAt(new \DateTime('now - '.$i.'days'));

        // for aesthetic reasons, the first blog post always has 2 tags
        foreach ($this->getRandomTags($i > 0 ? mt_rand(0, 3) : 2) as $tag) {
            $blog->addTag($tag);
        }

        foreach (range(1, 5) as $j) {
            $comment = new Comment();

            $comment->setName($this->getReference('john-user'));
            $comment->setCreatedDate(new \DateTime('now + '.($i + $j).'seconds'));
            $comment->setComment($this->getRandomCommentContent());
            $comment->setBlog($blog);

            $manager->persist($comment);
            $blog->addComment($comment);
        }

        $manager->persist($blog);
    }

    $manager->flush();
}

/**
 * Instead of defining the exact order in which the fixtures files must be loaded,
 * this method defines which other fixtures this file depends on. Then, Doctrine
 * will figure out the best order to fit all the dependencies.
 *
 * @return array
 */
public function getDependencies()
{
    return [
        TagFixtures::class,
    ];
}

private function getRandomTags($numTags = 0)
{
    $tags = [];

    if (0 === $numTags) {
        return $tags;
    }

    $indexes = (array) array_rand($this->getTagNames(), $numTags);
    foreach ($indexes as $index) {
        $tags[] = $this->getReference('tag-'.$index);
    }

    return $tags;
}

public function getOrder()
{
    return 1;
}

}

这是 UserFixtures:

{
use ContainerAwareTrait;

/**
 * {@inheritdoc}
 */
public function load(ObjectManager $manager)
{
    $passwordEncoder = $this->container->get('security.password_encoder');

    $janeAdmin = new User();
    $janeAdmin->setFullName('Jane Doe');
    $janeAdmin->setUsername('jane_admin');
    $janeAdmin->setEmail('[email protected]');
    $janeAdmin->setRoles(['ROLE_ADMIN']);
    $encodedPassword = $passwordEncoder->encodePassword($janeAdmin, 'kitten');
    $janeAdmin->setPassword($encodedPassword);
    $manager->persist($janeAdmin);
    // In case if fixture objects have relations to other fixtures, adds a reference
    // to that object by name and later reference it to form a relation.
    // See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures

    $johnUser = new User();
    $johnUser->setFullName('John Doe');
    $johnUser->setUsername('john_user');
    $johnUser->setEmail('[email protected]');
    $encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten');
    $johnUser->setPassword($encodedPassword);
    $manager->persist($johnUser);

    $manager->flush();

    $this->addReference('jane-admin', $janeAdmin);
    $this->addReference('john-user', $johnUser);
}

public function getOrder()
{
    return 0;
}

博客中实体的关系

/**
 * @var User
 *
 * @ORM\ManyToOne(targetEntity="BlogBundle\Entity\User" )
 * @ORM\JoinColumn(nullable=false)
 */
private $author;

希望有人可以提供帮助,已经绕圈子了一段时间。

谢谢

彼得·波佩列什科

你正试图setNameUser Entity.

$comment->setName($this->getReference('john-user'));

将此行更改为: $this->getReference('john-user')->getName();

或者您可以__toString()在用户实体中添加魔术方法例如:

public function __toString()
{
     return $this->getName();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章