I want to create console command that download REST API data to database. Now I'm using fixtures to insert it into database and i don't know how to create command that is gonna run that kind of function. Can You give me some advance how to create that kind of command?
CodePudding user response:
If you're wondering how to run the fixtures you created, run the command:
php bin/console doctrine:fixtures:load
CodePudding user response:
please try this code:
public function load(ObjectManager $manager)
{
// get remote data from external API
$request = file_get_contents('https://jsonplaceholder.typicode.com/posts');
$response = json_decode($request, true);
foreach ($response as $value) {
$post = new Post();
$post->setTitle($value['title']);
$post->setDescription($value['body']);
$manager->persist($post);
}
$manager->flush();
}