我必須測試我的應用程序,所以我想使用教義的燈具在數據庫中插入數據。我想創建一個與用戶實體(fosuser)具有ManyToOne
關係的實體的新條目(例如博客文章)。所以,我必須檢索第一個用戶來設置爲博客文章作者。問題是,當我運行命令:檢索燈具類中的用戶
php app/console doctrine:fixtures:load
我收到以下錯誤:
Catchable fatal error: Argument 1 passed to BluEstuary\PostBundle\Model\Post::setOwner()
must be an instance of BluEstuary\UserBundle\Model\User, null given, called in /Users/bface007/workspace/BluEstuary/esgis_gabon/src/ESGISGabon/PostBundle/DataFixtures/ORM/Posts.php on line 53 and defined in /Users/bface007/workspace/BluEstuary/esgis_gabon/src/BluEstuary/PostBundle/Model/Post.php on line 296`
我可以檢索非固定裝置類的用戶,但它總是給空值,當我嘗試在燈具之一。有人能幫助我嗎?
這是我的燈具類:
class Posts implements FixtureInterface, ContainerAwareInterface{
public function load(Objectmanager $manager){
$blogposts = array(
array(
"title" => "This is test 1",
"content" => "I am a cool example"
),
array(
"title" => "This is test 2",
"content" => "I am a cool example"
)
);
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserBy(array("id" => 3));
foreach($posts as $i => $post){
$new_posts[$i] = new BlogPost();
$new_posts[$i]->setPostTitle($post["title"])
->setPostContent($post["content"]);
$new_posts[$i]->setAuthor($user);
$manager->persist($new_posts[$i]);
}
$manager->flush();
}
}
,必須先創建用戶,因爲空數據庫。 – malcolm
你是對的。我沒有想到這一點。 – bface007