alexdebril / rss-atom-bundle

RSS and Atom Bundle for Symfony
MIT License
139 stars 49 forks source link

Unable to insert fetched feeds into database #78

Closed saadiashairyar closed 9 years ago

saadiashairyar commented 9 years ago

After reading the instructions I can get the RSS feeds however I am not able to insert them into database.

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.

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 
     * Rss  : 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 
     * Rss  : rss.channel.lastBuildDate 
     * @return \DateTime
     */
    public function getLastModified()
    {
        return $this->lastModified;
    }

    /**
     * Atom : 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.
    }
}
</code>
</pre>
<p>What am I doing wrong?</p>            </div>
        </div>
    
            <div class="comment">
            <div class="user">
                <a rel="noreferrer nofollow" target="_blank" href="https://github.com/saadiashairyar"><img src="https://avatars.githubusercontent.com/u/12998320?v=4" />saadiashairyar</a>
                commented
                <strong> 9 years ago</strong>            </div>
            <div class="markdown-body">
                <p>You also mention the following statement at <a href="https://github.com/alexdebril/rss-atom-bundle/wiki/Reading-feeds#using-readfeed">https://github.com/alexdebril/rss-atom-bundle/wiki/Reading-feeds#using-readfeed</a> </p>
<blockquote>
<p>The $feed object is then filled with Item instances.</p>
</blockquote>
<p>I am puzzled how it is done? am i suppose to define a service because i do not see doctrine being called anywhere to add fetched feeds in database and linking the variable $feed with Item instances.</p>
<p>My understanding is that <code>readFeed($url, $feeds, $date)</code> will automatically add my feeds in database or am i supposed to add the following doctrine code? if i am supposed to add the following code then how can I link the Item instance with my Entiry so the RSS feeds get added?</p>
<pre>
$em = $this->getDoctrine()->getManager();
$em->persist($feeds);
$em->flush();
</pre>            </div>
        </div>
            <div class="comment">
            <div class="user">
                <a rel="noreferrer nofollow" target="_blank" href="https://github.com/alexdebril"><img src="https://avatars.githubusercontent.com/u/207048?v=4" />alexdebril</a>
                commented
                <strong> 9 years ago</strong>            </div>
            <div class="markdown-body">
                <p>HI,</p>
<p>rss-atom-bundle doesn't explicitely calls Doctrine while reading, it builds a Feed and thren it's yours to store it in database.</p>
<p>I made a test project, look at <a href="https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Command/AbstractCommand.php?at=master#cl-35">https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Command/AbstractCommand.php?at=master#cl-35</a></p>
<p>the command gets a Feed instance from the Reader and then use the FeedRepository to save it : <a href="https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Entity/FeedRepository.php?at=master#cl-100">https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Entity/FeedRepository.php?at=master#cl-100</a></p>
<p>In feed-aggregator-bundle I used custom Feed and Item classes to handle the link in database and I referenced them in the bundle's service parameters : <a href="https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Resources/config/services.xml?at=master">https://bitbucket.org/adebril/feed-aggregator-bundle/src/781c4fc004a4ce9feb89db30aaf5ae4d827c3480/Resources/config/services.xml?at=master</a></p>
<p>I hope those code samples will help.</p>
<p>Cheers</p>            </div>
        </div>
            <div class="comment">
            <div class="user">
                <a rel="noreferrer nofollow" target="_blank" href="https://github.com/saadiashairyar"><img src="https://avatars.githubusercontent.com/u/12998320?v=4" />saadiashairyar</a>
                commented
                <strong> 9 years ago</strong>            </div>
            <div class="markdown-body">
                <p>I cant view the links you shared, i see the error  Access denied </p>            </div>
        </div>
            <div class="comment">
            <div class="user">
                <a rel="noreferrer nofollow" target="_blank" href="https://github.com/alexdebril"><img src="https://avatars.githubusercontent.com/u/207048?v=4" />alexdebril</a>
                commented
                <strong> 9 years ago</strong>            </div>
            <div class="markdown-body">
                <p>Sorry, the repository was private I just made it public</p>            </div>
        </div>
    
    <div class="page-bar-simple">
            </div>

    <div class="footer">
        <ul class="body">
            <li>© <script>
                    document.write(new Date().getFullYear())
                </script> Githubissues.</li>
            <li>Githubissues is a development platform for aggregating issues.</li>
        </ul>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
    <script src="/githubissues/assets/js.js"></script>
    <script src="/githubissues/assets/markdown.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script>
    <script>
        hljs.highlightAll();
    </script>
</body>

</html>