Symfony 2.6, извлеките другой репозиторий пакетов, контейнер осведомлен о приспособлениях

Я пытаюсь получить данные из другого репозитория сущностей комплектов в файле фикстур данных, для этого я пытаюсь реализовать контейнер, но он не работает, есть ли способ выполнить запрос к другому репозиторию сущностей комплектов изнутри файл данных фикстур?

Фикстуры комментариев

namespace DomNoble\BlogBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use DomNoble\BlogBundle\Entity\Blog\Comments;
use DomNoble\BlogBundle\Entity\Blog\Posts;
use DomNoble\UserBundle\Entity\Users;

class CommentFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{

private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function load(ObjectManager $manager)
{
$em = $this->container->get('doctrine')->getEntityManager('default')->getRepository('DomNobleUserBundle:Users');

$comment = new Comments();
$user = $em->loadUserByUsername('superadmin');
$comment->setUserID($user->getId());
$comment->setContent('To make a long story short. You can\'t go wrong by choosing Symfony! And no one has ever been fired for using Symfony.');
$comment->setPost($manager->merge($this->getReference('post-1')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('bradpit');
$comment->setUserID($user->getId());
$comment->setContent('To make a long story short. Choosing a framework must not be taken lightly; it is a long-term commitment. Make sure that you make the right selection!');
$comment->setPost($manager->merge($this->getReference('post-1')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('thejoker');
$comment->setUserID($user->getId());
$comment->setContent('Anything else, mom? You want me to mow the lawn? Oops! I forgot, New York, No grass.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('trollhat');
$comment->setUserID($user->getId());
$comment->setContent('Are you challenging me? ');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:15:20"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('thejoker');
$comment->setUserID($user->getId());
$comment->setContent('Name your stakes.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:18:35"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('trollhat');
$comment->setUserID($user->getId());
$comment->setContent('If I win, you become my slave.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:22:53"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('thejoker');
$comment->setUserID($user->getId());
$comment->setContent('Your SLAVE?');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 06:25:15"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('trollhat');
$comment->setUserID($user->getId());
$comment->setContent('You wish! You\'ll do shitwork, scan, crack copyrights...');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 06:46:08"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('thejoker');
$comment->setUserID($user->getId());
$comment->setContent('And if I win?');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 10:22:46"));
$manager->persist($comment);

$comment = new Comments();
$em = $this->container()->get('doctrine')->getEntityManager();
$user = $em->loadUserByUsername('trollhat');

$comment->setUserID($user->getId());
$comment->setContent('Make it my first-born!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 11:08:08"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('thejoker');
$comment->setUserID($user->getId());
$comment->setContent('Make it our first-date!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-24 18:56:01"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('trollhat');
$comment->setUserID($user->getId());
$comment->setContent('I don\'t DO dates. But I don\'t lose either, so you\'re on!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-25 22:28:42"));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('doggg');
$comment->setUserID($user->getId());
$comment->setContent('It\'s not gonna end like this.');
$comment->setPost($manager->merge($this->getReference('post-3')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('voltman');
$comment->setUserID($user->getId());
$comment->setContent('Oh, come on, Stan. Not everything ends the way you think it should. Besides, audiences love happy endings.');
$comment->setPost($manager->merge($this->getReference('post-3')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('oldboy');
$comment->setUserID($user->getId());
$comment->setContent('Doesn\'t Bill Gates have something like that?');
$comment->setPost($manager->merge($this->getReference('post-5')));
$manager->persist($comment);

$comment = new Comments();
$user = $em->loadUserByUsername('mylittlepony');
$comment->setUserID($user->getId());
$comment->setContent('Bill Who?');
$comment->setPost($manager->merge($this->getReference('post-5')));
$manager->persist($comment);

$manager->flush();
}

public function getOrder()
{
return 2;
}
}

Когда я пытаюсь запустить команду fixtures, я получаю сообщение об ошибке:

команда

doctrine:fixtures:load

ошибка

Attempted to call method "container" on class "DomNoble\BlogBundle\DataFixt
ures\ORM\CommentFixtures".

что я тут не так делаю? все, что я хочу сделать, это заменить идентификатор пользователя на имя пользователя с помощью репозитория пользователей. Как я могу это сделать?

Комментарии Entity

namespace DomNoble\BlogBundle\Entity\Blog;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="DomNoble\BlogBundle\Entity\Repository\CommentsRepository")
* @ORM\Table(name="comments")
* @ORM\HasLifecycleCallbacks
*/
class Comments
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="DomNoble\UserBundle\Entity\Users")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $userID;

/**
* @ORM\Column(type="boolean")
*/
private $approved;

/**
* @ORM\ManyToOne(targetEntity="Posts", inversedBy="comments")
* @ORM\JoinColumn(name="post", referencedColumnName="id")
*/
private $post;

/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;

/**
* @var array
*
* @ORM\Column(name="img", type="array")
*/
private $img;

/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;

/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime")
*/
private $updated;

public function __construct()
{
$this->setCreated(new \DateTime());
$this->setUpdated(new \DateTime());

$this->setApproved(true);
}

/**
* @ORM\PreUpdate
*/
public function setUpdatedValue()
{
$this->setUpdated(new \DateTime());
}public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('content', new NotBlank(array(
'message' => 'You must enter a comment'
)));
}/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set userID
*
* @param string $userID
* @return Comments
*/
public function setUserID($userID)
{
$this->userID = $userID;

return $this;
}

/**
* Get userID
*
* @return string
*/
public function getUserID()
{
return $this->userID;
}

/**
* Set post
*
* @param string $post
* @return Comments
*/
public function setPost($post)
{
$this->post = $post;

return $this;
}

/**
* Get post
*
* @return string
*/
public function getPost()
{
return $this->post;
}

/**
* Set approved
*
* @param integer $approved
* @return Comments
*/
public function setApproved($approved)
{
$this->approved = $approved;

return $this;
}

/**
* Get approved
*
* @return integer
*/
public function getApproved()
{
return $this->approved;
}

/**
* Set content
*
* @param string $content
* @return Comments
*/
public function setContent($content)
{
$this->content = $content;

return $this;
}

/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}

/**
* Set img
*
* @param array $img
* @return Comments
*/
public function setImg($img)
{
$this->img = $img;

return $this;
}

/**
* Get img
*
* @return array
*/
public function getImg()
{
return $this->img;
}

/**
* Set created
*
* @param \DateTime $created
* @return Comments
*/
public function setCreated($created)
{
$this->created = $created;

return $this;
}

/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}

/**
* Set updated
*
* @param \DateTime $updated
* @return Comments
*/
public function setUpdated($updated)
{
$this->updated = $updated;

return $this;
}

/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}}

было несколько проблем с вышеупомянутым кодом, упоминалось также одно гнездо ошибки getEntityManager стал getManager и я пытался предоставить идентификатор, а не объект пользователя для объекта комментария. вот оно сейчас работает:

Фикстуры комментариев

namespace DomNoble\BlogBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use DomNoble\BlogBundle\Entity\Blog\Comments;
use DomNoble\BlogBundle\Entity\Blog\Posts;
use DomNoble\UserBundle\Entity\Users;

class CommentFixtures extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{

private $container;

public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

public function load(ObjectManager $manager)
{
$em = $this->container->get('doctrine')->getManager();
$userRepo = $em->getRepository('DomNobleUserBundle:Users');

$comment = new Comments();
$user = $userRepo->loadUserByUsername('superadmin');
$comment->setUser($user);
$comment->setContent('To make a long story short. You can\'t go wrong by choosing Symfony! And no one has ever been fired for using Symfony.');
$comment->setPost($manager->merge($this->getReference('post-1')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('bradpit');
$comment->setUser($user);
$comment->setContent('To make a long story short. Choosing a framework must not be taken lightly; it is a long-term commitment. Make sure that you make the right selection!');
$comment->setPost($manager->merge($this->getReference('post-1')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('thejoker');
$comment->setUser($user);
$comment->setContent('Anything else, mom? You want me to mow the lawn? Oops! I forgot, New York, No grass.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('trollhat');
$comment->setUser($user);
$comment->setContent('Are you challenging me? ');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:15:20"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('thejoker');
$comment->setUser($user);
$comment->setContent('Name your stakes.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:18:35"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('trollhat');
$comment->setUser($user);
$comment->setContent('If I win, you become my slave.');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-23 06:22:53"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('thejoker');
$comment->setUser($user);
$comment->setContent('Your SLAVE?');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 06:25:15"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('trollhat');
$comment->setUser($user);
$comment->setContent('You wish! You\'ll do shitwork, scan, crack copyrights...');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 06:46:08"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('thejoker');
$comment->setUser($user);
$comment->setContent('And if I win?');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 10:22:46"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('trollhat');
$comment->setUser($user);
$comment->setContent('Make it my first-born!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-23 11:08:08"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('thejoker');
$comment->setUser($user);
$comment->setContent('Make it our first-date!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2014-07-24 18:56:01"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('trollhat');
$comment->setUser($user);
$comment->setContent('I don\'t DO dates. But I don\'t lose either, so you\'re on!');
$comment->setPost($manager->merge($this->getReference('post-2')));
$comment->setCreated(new \DateTime("2011-07-25 22:28:42"));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('doggg');
$comment->setUser($user);
$comment->setContent('It\'s not gonna end like this.');
$comment->setPost($manager->merge($this->getReference('post-3')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('voltman');
$comment->setUser($user);
$comment->setContent('Oh, come on, Stan. Not everything ends the way you think it should. Besides, audiences love happy endings.');
$comment->setPost($manager->merge($this->getReference('post-3')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('oldboy');
$comment->setUser($user);
$comment->setContent('Doesn\'t Bill Gates have something like that?');
$comment->setPost($manager->merge($this->getReference('post-5')));
$manager->persist($comment);

$comment = new Comments();
$user = $userRepo->loadUserByUsername('mylittlepony');
$comment->setUser($user);
$comment->setContent('Bill Who?');
$comment->setPost($manager->merge($this->getReference('post-5')));
$manager->persist($comment);

$manager->flush();
}

public function getOrder()
{
return 2;
}
}

Комментарий сущности

namespace DomNoble\BlogBundle\Entity\Blog;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity(repositoryClass="DomNoble\BlogBundle\Entity\Repository\CommentsRepository")
* @ORM\Table(name="comments")
* @ORM\HasLifecycleCallbacks
*/
class Comments
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="DomNoble\UserBundle\Entity\Users")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
private $user;

/**
* @ORM\Column(type="boolean")
*/
private $approved;

/**
* @ORM\ManyToOne(targetEntity="Posts", inversedBy="comments")
* @ORM\JoinColumn(name="post", referencedColumnName="id")
*/
private $post;

/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;

/**
* @var array
*
* @ORM\Column(name="img", type="array")
*/
private $img;

/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;

/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime")
*/
private $updated;

public function __construct()
{
$this->setCreated(new \DateTime());
$this->setUpdated(new \DateTime());

$this->setApproved(true);
}

/**
* @ORM\PreUpdate
*/
public function setUpdatedValue()
{
$this->setUpdated(new \DateTime());
}public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('content', new NotBlank(array(
'message' => 'You must enter a comment'
)));
}/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set user
*
* @param string $userID
* @return Comments
*/
public function setUser($user)
{
$this->user = $user;

return $this;
}

/**
* Get user
*
* @return string
*/
public function getUser()
{
return $this->user;
}

/**
* Set post
*
* @param string $post
* @return Comments
*/
public function setPost($post)
{
$this->post = $post;

return $this;
}

/**
* Get post
*
* @return string
*/
public function getPost()
{
return $this->post;
}

/**
* Set approved
*
* @param integer $approved
* @return Comments
*/
public function setApproved($approved)
{
$this->approved = $approved;

return $this;
}

/**
* Get approved
*
* @return integer
*/
public function getApproved()
{
return $this->approved;
}

/**
* Set content
*
* @param string $content
* @return Comments
*/
public function setContent($content)
{
$this->content = $content;

return $this;
}

/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}

/**
* Set img
*
* @param array $img
* @return Comments
*/
public function setImg($img)
{
$this->img = $img;

return $this;
}

/**
* Get img
*
* @return array
*/
public function getImg()
{
return $this->img;
}

/**
* Set created
*
* @param \DateTime $created
* @return Comments
*/
public function setCreated($created)
{
$this->created = $created;

return $this;
}

/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}

/**
* Set updated
*
* @param \DateTime $updated
* @return Comments
*/
public function setUpdated($updated)
{
$this->updated = $updated;

return $this;
}

/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}}

другое решение этой проблемы было бы использовать OrderedFixtureInterface и ссылаться на все комментарии для пользователей

1

Решение

заменить линию

    $em = $this->container()->get('doctrine')->getEntityManager();

с

    $em = $this->container->get('doctrine')->getEntityManager();
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]