Php 无法配置Symfony(第三方)捆绑包
我是Symfony的新手,我正在尝试设置一个第三方包,它可以读取RSS提要,然后将它们插入数据库。我尝试使用的第三方包名为 阅读说明后,我可以获得RSS提要,但我无法将它们插入数据库,可能是因为我对Symfony缺乏了解 这是我的控制器,它获取提要,然后插入数据库Php 无法配置Symfony(第三方)捆绑包,php,symfony,rss,Php,Symfony,Rss,我是Symfony的新手,我正在尝试设置一个第三方包,它可以读取RSS提要,然后将它们插入数据库。我尝试使用的第三方包名为 阅读说明后,我可以获得RSS提要,但我无法将它们插入数据库,可能是因为我对Symfony缺乏了解 这是我的控制器,它获取提要,然后插入数据库 namespace AppBundle\Controller; use AppBundle\Entity\Feed as Feed; use Sensio\Bundle\FrameworkExtraBundle\Configurat
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()
方法的示例,该方法用于将提要插入数据库。我遵循了它,但没有成功
这是我的提要
实体
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.
}
}
名称空间AppBundle\Entity;
使用Debril\RssAtomBundle\Protocol\FeedInterface;
使用Debril\RssAtomBundle\Protocol\ItemIn;
使用条令\ORM\Mapping作为ORM;
/**
*喂
*/
类提要实现了提要接口
{
/**
*@var整数
*/
私人$id;
私人帐户$LASTMODIFED;
私人产权;
私人说明;
私人$link;
私人$publicId;
/**
*取得身份证
*
*@返回整数
*/
公共函数getId()
{
返回$this->id;
}
/**
*Atom:feed.entry
*Rss:Rss.channel.item
*@param\Debril\RssAtomBundle\Protocol\ItemIn$item
*/
公共功能附加项(ItemIn$item)
{
//TODO:实现addItem()方法。
}
公共函数setLastModified(\DateTime$lastModified)
{
$this->lastModified=$lastModified;
退还$this;
}
公共函数setTitle($title)
{
$this->title=$title;
退还$this;
}
公共函数setDescription($description)
{
$this->description=$description;
退还$this;
}
公共函数setLink($link)
{
$this->link=$link;
退还$this;
}
公共函数setPublicId($id)
{
$this->publicId=$id;
退还$this;
}
/**
*Atom:feed.updated
*Rss:Rss.channel.lastBuildDate
*@return\DateTime
*/
公共函数getLastModified()
{
返回$this->lastModified;
}
/**
*Atom:feed.title
*Rss:Rss.channel.title
*@返回字符串
*/
公共函数getTitle()
{
返回$this->title;
}
/**
*Atom:feed.subtitle
*Rss:Rss.channel.description
*@返回字符串
*/
公共函数getDescription()
{
返回$this->description;
}
/**
*Atom:feed.link
*Rss:Rss.channel.link
*@返回字符串
*/
公共函数getLink()
{
返回$this->link;
}
/**
*Atom:feed.id
*Rss:Rss.channel.id
*@返回字符串
*/
公共函数getPublicId()
{
返回$this->publicId;
}
/**
*Atom:feed.entry
*Rss:Rss.channel.item
*@return数组[\Debril\RssAtomBundle\Protocol\ItemOut]
*/
公共函数getItems()
{
//TODO:实现getItems()方法。
}
}
我真的很感激你能给我一个正确的方向,因为我现在真的不知道。我还没有尝试过这个捆绑包,但我想你需要告诉我你想把你新创建的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');
更新
根据文档,如果您想使用
doctor
将提要及其项持久化到数据库中,您需要创建两个类,一个用于FeedInterface
,另一个用于ItemInInterface
和ItemInInterface
。此外,您还需要为这些类配置条令数据库模式,以便它知道如何将它们的数据存储在数据库中。接下来,您需要告诉bundle使用您的类,并最终调用persist()
和flush()
将提要及其项实际保存到数据库中。恐怕不会导致xyz列的错误不能为空,我获取的提要应该使用$reader->readFeed($url,$feed,$date)添加到数据库中代码>根据文档,是否有错误?在问题中插入此错误消息。不,我运行操作时没有收到错误,我是说错误发生在我应用您的解决方案时。我在文档中没有看到任何关于将新创建的提要持久化到数据库中的文字。出现此错误是因为条令试图将实体持久化馈送到数据库中。因此,我认为您需要修复此错误。$feeds是空的,因此当我尝试预列表时,我会得到正确的错误。我的问题是如何用RSS项填充/链接$feed,以便在持久化它时它不是空的