2017-05-23 20 views
0

symfonyフレームワークの学習中ですが、現在Symfony 2.8がインストールされています。 いくつかのデータBlogFixtures、UserFixtures、およびTagFixturesを読み込むためにDoctrine Fixturesを使用しています(TagFixturesは問題ではありません)。Symfony 2.8 Doctrine Fixtures

php app/console doctrine:fixtures:load私はCatchable Fatal Error: Object of class Proxies\__CG__\BlogBundle\Entity\User could not be converted to stringを取得しようとしています。

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; 
ここ

はBlogFixturesの関数であります

誰かが助けてくれることを願っています。

おかげ

答えて

2

あなたはUser EntitysetNameしようとしています。

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

変更するには、この行に: $this->getReference('john-user')->getName();

それとも、ユーザエンティティに魔法の方法__toString()を追加することができます。例:

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

ありがとうございました! – user1841749

+0

@ user1841749ユーザーがあなたの問題を解決したとき、彼の答えは受け入れられたとフラグを立てる必要があります。あなたはdownvoteの矢印のすぐ下に "accept"フラグを見つけるでしょう;-)。 –

関連する問題