【问题标题】:Symfony 2.8 Doctrine FixturesSymfony 2.8 教义装置
【发布时间】:2017-05-23 23:21:56
【问题描述】:

我正在学习 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;

希望有人可以提供帮助,已经转了一圈。

谢谢

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:

    您正在尝试将setName 与User Entity 联系起来。

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

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

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

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

    【讨论】:

    • 谢谢你修复它!
    • @user1841749 当用户解决您的问题时,他的回答应该被标记为已接受。您会在向下投票箭头下方找到“接受”标志;-)。
    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2018-03-20
    • 2017-09-13
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多