3

I am faily new to Symfony and I am trying to setup a third party bundle that reads RSS feeds and then insert them into database. The third party bundle I am trying to use is called rss-atom-bundle

After reading the instructions I can get the RSS feeds however I am not able to insert them into database probably due to my lack of knowledge of Symfony

This is the controller I have that fetches the feeds and then should insert into database

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');
    }

}

I do not see any error and I do not see any feeds inside my database table as well.

Here is the documentaion of the readFeed() method the which is supposed to insert feeds into database. I have followed it but yet no success

This is my Feed Entity

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.
    }
}

I will really appreciate a push in right direction as I am really clueless at this point.

1 Answer 1

1

I havent tried this bundle yet, but i think you need to tell doctrine that you want to save your newly created feed into the database:

$feeds = new Feed;
$reader->readFeed($url, $feeds, $date);

$em = $this->getDoctrine()->getManager();
$em->persist($feeds);
$em->flush();

return $this->render('default/index.html.twig');

UPDATE

According to the docs if you want to use doctrine to persist feed and its items to the database you need to create two classses, one for FeedInterface the other one for ItemInInterface and ItemOutInterface. Also, you need to configure doctrine database schema for these classes, so it will know how to store their data in the db. Next you need to tell the bundle to use your classes and finally call persist() and flush() to actually save feed and its items into the database.

5
  • I am afraid not that leads to error of column xyz cannot be blank, the feeds i fetch should get added to database using $reader->readFeed($url, $feeds, $date); as per the documentaion
    – Saadia
    Commented Jun 22, 2015 at 11:06
  • So you getting an error? Insert this error message into the question. Commented Jun 22, 2015 at 11:08
  • no i am not getting an error when i run the action, i am saying the error occures when i apply your solution
    – Saadia
    Commented Jun 22, 2015 at 11:13
  • I dont see any word about persisting newly created feed into the database in the docs. You getting this error because doctrine tries to persist feed entity into the db. So i think you need to fix this error. Commented Jun 22, 2015 at 11:16
  • $feeds is empty so when i try to presist i get the error which is fine. My problem is how to fill/link the $feeds with the RSS items so it is not empty when i am persisting it
    – Saadia
    Commented Jun 22, 2015 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.