我是Symfony的新手,我正试图建立一个第三方捆绑包,读取RSS提要,然后将其插入数据库。我尝试使用的第三方捆绑包被称为rss原子捆绑包
阅读说明后,我可以获得RSS提要,但我无法将其插入数据库,可能是因为我对Symfony 缺乏了解
这是我的控制器,它获取提要,然后应该插入数据库
namespace AppBundleController;
use AppBundleEntityFeed as Feed;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
// fetch the FeedReader
$reader = $this->container->get('debril.reader');
// this date is used to fetch only the latest items
$unmodifiedSince = '11/11/2014';
$date = new DateTime($unmodifiedSince);
// the feed you want to read
$url = 'https://example.com/feed/';
// now fetch its (fresh) content
$feed = $reader->getFeedContent($url, $date);
// in developer tool bar I can see the feeds using dump()
dump($feed);
$items = $feed->getItems();
//Insert fetched feeds into database
$feeds = new Feed;
$reader->readFeed($url, $feeds, $date);
return $this->render('default/index.html.twig');
}
}
我没有看到任何错误,也没有在数据库表中看到任何提要。
这是readFeed()
方法的文档,该方法应该将提要插入数据库。我遵循了它,但没有成功
这是我的Feed
实体
namespace AppBundleEntity;
use DebrilRssAtomBundleProtocolFeedInterface;
use DebrilRssAtomBundleProtocolItemIn;
use DoctrineORMMapping as ORM;
/**
* Feed
*/
class Feed implements FeedInterface
{
/**
* @var integer
*/
private $id;
private $lastModified;
private $title;
private $description;
private $link;
private $publicId;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Atom : feed.entry <feed><entry>
* Rss : rss.channel.item <rss><channel><item>
* @param DebrilRssAtomBundleProtocolItemIn $item
*/
public function addItem(ItemIn $item)
{
// TODO: Implement addItem() method.
}
public function setLastModified(DateTime $lastModified)
{
$this->lastModified = $lastModified;
return $this;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function setLink($link)
{
$this->link = $link;
return $this;
}
public function setPublicId($id)
{
$this->publicId = $id;
return $this;
}
/**
* Atom : feed.updated <feed><updated>
* Rss : rss.channel.lastBuildDate <rss><channel><lastBuildDate>
* @return DateTime
*/
public function getLastModified()
{
return $this->lastModified;
}
/**
* Atom : feed.title <feed><title>
* Rss : rss.channel.title <rss><channel><title>
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Atom : feed.subtitle <feed><subtitle>
* Rss : rss.channel.description <rss><channel><description>
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Atom : feed.link <feed><link>
* Rss : rss.channel.link <rss><channel><link>
* @return string
*/
public function getLink()
{
return $this->link;
}
/**
* Atom : feed.id <feed><id>
* Rss : rss.channel.id <rss><channel><id>
* @return string
*/
public function getPublicId()
{
return $this->publicId;
}
/**
* Atom : feed.entry <feed><entry>
* Rss : rss.channel.item <rss><channel><item>
* @return array[DebrilRssAtomBundleProtocolItemOut]
*/
public function getItems()
{
// TODO: Implement getItems() method.
}
}
我真的很感激能朝着正确的方向前进,因为我在这一点上真的毫无头绪。
我还没有尝试过这个捆绑包,但我认为你需要告诉条令,你想把你新创建的提要保存到数据库中:
$feeds = new Feed;
$reader->readFeed($url, $feeds, $date);
$em = $this->getDoctrine()->getManager();
$em->persist($feeds);
$em->flush();
return $this->render('default/index.html.twig');
更新
根据文档,如果您想使用doctrine
将提要及其项持久化到数据库,则需要创建两个类,一个用于FeedInterface
,另一个用于ItemInInterface
和ItemOutInterface
。此外,您需要为这些类配置条令数据库模式,这样它就知道如何将它们的数据存储在数据库中。接下来,您需要告诉bundle使用您的类,并最终调用persist()
和flush()
将提要及其项实际保存到数据库中。