eko / FeedBundle

A Symfony bundle to build RSS feeds from your entities
http://vincent.composieux.fr
MIT License
141 stars 50 forks source link

Having problem adding RSS feeds to Entity #52

Closed shairyar closed 9 years ago

shairyar commented 9 years ago

Hi,

I am trying to save the RSS feeds being fetched to my database, following the instructions this is the code inside my controller

       $reader = $this->get('eko_feed.feed.reader');       
        $items = $reader->load('http://rss.indeed.com/rss?q=Singapore')->populate('AppBundle\Entity\Rss');

This is my Rss Entity

title = $title;
    }

    /**
     * Returns a description or content
     *
     * @return string
     */
    public function setFeedItemDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Returns item link
     *
     * @return string
     */
    public function setFeedItemLink($link)
    {
        $this->link = $link;
    }

    /**
     * Returns publication date
     *
     * @return \DateTime
     */
    public function setFeedItemPubDate(\DateTime $date)
    {
        $this->date = $date;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Get link
     *
     * @return string
     */
    public function getLink()
    {
        return $this->link;
    }

    /**
     * Get publication date
     *
     * @return \DateTime
     */
    public function getPublicationDate()
    {
        return $this->date;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Rss
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Rss
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Set link
     *
     * @param string $link
     * @return Rss
     */
    public function setLink($link)
    {
        $this->link = $link;

        return $this;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     * @return Rss
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime 
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set publicationDate
     *
     * @param \DateTime $publicationDate
     * @return Rss
     */
    public function setPublicationDate($publicationDate)
    {
        $this->publicationDate = $publicationDate;

        return $this;
    }

    /**
     * Get feedItemTitle
     *
     * @return string 
     */
    public function getFeedItemTitle()
    {
        return $this->feedItemTitle;
    }

    /**
     * Get feedItemDescription
     *
     * @return string 
     */
    public function getFeedItemDescription()
    {
        return $this->feedItemDescription;
    }

    /**
     * Get feedItemLink
     *
     * @return string 
     */
    public function getFeedItemLink()
    {
        return $this->feedItemLink;
    }

    /**
     * Get feedItemPubDate
     *
     * @return \DateTime 
     */
    public function getFeedItemPubDate()
    {
        return $this->feedItemPubDate;
    }
}

When i run the action the error i get is as following and i don't understand why. What am i doing wrong?

Error: Call to a member function hydrate() on null
eko commented 9 years ago

Hi @shairyar,

This is because you have to use an hydrator class (or the following default hydrator class: https://github.com/eko/FeedBundle/blob/master/Hydrator/DefaultHydrator.php).

Your code should be:

$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());

$items = $reader->load('http://rss.indeed.com/rss?q=Singapore')->populate('AppBundle\Entity\Rss');

Hope it helps.

shairyar commented 9 years ago

Hi @eko

Thanks, that solved the problem, how do i persist it to the database then

I tried the following

$reader = $this->get('eko_feed.feed.reader');
        $reader->setHydrator(new DefaultHydrator());
        $items = $reader->load('http://rss.indeed.com/rss?q=Singapore')->populate('AppBundle\Entity\Rss');
        dump($items);
        $em = $this->getDoctrine()->getManager();
        $em->persist($items);
        $em->flush();
        return $this->render('default/index.html.twig');

but that gives me an error of

EntityManager#persist() expects parameter 1 to be an entity object, array given.
eko commented 9 years ago

@shairyar,

You have to persist item per item:

$reader = $this->get('eko_feed.feed.reader');
$reader->setHydrator(new DefaultHydrator());
$items = $reader->load('http://rss.indeed.com/rss?q=Singapore')->populate('AppBundle\Entity\Rss');

$em = $this->getDoctrine()->getManager();

foreach ($items as $item) {
    $em->persist($item);
}

$em->flush();

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

Can you please close this issue?

Thank you

shairyar commented 9 years ago

Just one last question, is it possible to fetch feeds after the specific date? or that needs to be handled by me in my code logic?

eko commented 9 years ago

This is not possible through this bundle, you have to develop this logic.