我非常喜欢Symfony,我正在尝试设置一个第三方软件包,它读取RSS源,然后将它们插入到数据库中。我尝试使用的第三方软件包称为rss-atom-bundle
阅读说明后,我可以获得RSS源,但由于我对Symfony缺乏了解,我无法将它们插入数据库
这是我拥有的控制器,它可以获取源,然后应该插入数据库
namespace AppBundle\Controller;
use AppBundle\Entity\Feed as Feed;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
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()
方法的documentaion,它应该将Feed插入数据库。我已经关注它但却没有成功
这是我的Feed
实体
namespace AppBundle\Entity;
use Debril\RssAtomBundle\Protocol\FeedInterface;
use Debril\RssAtomBundle\Protocol\ItemIn;
use Doctrine\ORM\Mapping 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 \Debril\RssAtomBundle\Protocol\ItemIn $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[\Debril\RssAtomBundle\Protocol\ItemOut]
*/
public function getItems()
{
// TODO: Implement getItems() method.
}
}
我真的很感激向正确的方向发展,因为我现在真的很无能为力。
答案 0 :(得分:1)
我还没有尝试过这个捆绑包,但我认为您需要告诉学说您要将新创建的Feed保存到数据库中:
$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
将Feed及其项目保存到数据库,则需要创建两个类别,一个用于FeedInterface
,另一个用于ItemInInterface
和ItemOutInterface
。此外,您需要为这些类配置doctrine数据库模式,因此它将知道如何将数据存储在db中。接下来,您需要告诉捆绑包使用您的类,最后调用persist()
和flush()
来实际将Feed及其项目保存到数据库中。