2017-05-23 8 views
0

Ich bin gerade dabei Symfony Framework zu lernen, ich habe gerade Symfony 2.8 installiert. Ich benutze Doctrine Fixtures um einige Daten zu laden BlogFixtures, UserFixtures und TagFixtures (TagFixtures ist nicht das Problem).Symfony 2.8 Doctrine Fixtures

Beim Versuch php app/console doctrine:fixtures:load bekomme ich Catchable Fatal Error: Object of class Proxies\__CG__\BlogBundle\Entity\User could not be converted to string.

Hier Funktionen in 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; 
} 

}

Und hier ist 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; 
} 

Die Beziehung in den Entitäten In Blog

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

Hoffentlich kann jemand helfen, war schon eine Weile im Kreis.

Dank

Antwort

2

Sie versuchen zu setName mit User Entity.

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

Ändern Sie diese Zeile: $this->getReference('john-user')->getName();

Oder könnten Sie magische Methode __toString() in Benutzereinheit hinzuzufügen. Zum Beispiel:

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

Vielen Dank, dass es behoben! – user1841749

+0

@ user1841749 Wenn ein Benutzer Ihr Problem löst, muss seine Antwort als akzeptiert markiert werden. Sie finden die "accept" -Flagge unter dem Downvote-Pfeil ;-). –

Verwandte Themen