无法配置Symfony(第三方)捆绑包

3
我对Symfony还比较陌生,正在尝试安装一个第三方的bundle,它可以读取RSS feeds并将它们插入到数据库中。我要使用的第三方bundle名叫rss-atom-bundle
阅读了说明后,我可以获取RSS feeds,但由于我对Symfony的知识不足,无法将其插入到数据库中。
这是我拥有的控制器,它可以获取feeds并应该将其插入到数据库中。
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()方法,它应该将源插入数据库。我已经按照它的步骤进行了操作,但仍然没有成功。这是我的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.
    }
}

我真的希望能得到正确方向上的帮助,因为此时我真的一无所知。

1个回答

1
我还没有尝试过这个捆绑包,但我认为您需要告诉Doctrine您希望将新创建的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将订阅及其项目持久化到数据库中,您需要创建两个类,一个用于FeedInterface,另一个用于ItemInInterfaceItemOutInterface。此外,您还需要为这些类配置Doctrine数据库模式,以便它知道如何在数据库中存储它们的数据。接下来,您需要告诉bundle使用您的类,并最终调用persist()flush()将订阅及其项目实际保存到数据库中。


很抱歉,这会导致列xyz不能为空的错误。根据文档,我获取的源应该使用$reader->readFeed($url, $feeds, $date);添加到数据库中。 - Saadia
你遇到了错误?请将该错误信息插入到问题中。 - Vadim Ashikhman
我并没有在运行操作时遇到错误,我是在说当我应用你的解决方案时出现了错误。 - Saadia
我在文档中没有看到有关将新创建的 feed 持久化到数据库中的任何信息。您会收到此错误是因为 Doctrine 尝试将 feed 实体持久化到数据库中。因此,我认为您需要修复此错误。 - Vadim Ashikhman
$feeds为空,所以当我尝试持久化时会出现错误,这是可以理解的。我的问题是如何填充/链接$feeds与RSS项,以便在持久化时它不为空。 - Saadia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接