Symfony 2.8 Doctrine Fixtures

时间:2017-05-23 15:06:00

标签: symfony doctrine-orm

我正在学习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('jane_admin@symfony.com');
    $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('john_user@symfony.com');
    $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;

希望有人可以提供帮助,一会儿就转了一圈。

谢谢

1 个答案:

答案 0 :(得分:2)

您正试图pinCode.Focus();setName

User Entity

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

或者您可以在用户实体中添加魔术方法$this->getReference('john-user')->getName();。例如:

__toString()