alexdebril / feed-io

A PHP library to read and write feeds in JSONFeed, RSS or Atom format
https://alexdebril.github.io/feed-io/
MIT License
255 stars 53 forks source link

Call to a member function append() on null in FeedIo/Feed/ElementsAwareTrait.php:66 #165

Closed nicodemuz closed 6 years ago

nicodemuz commented 6 years ago

Any idea where I'm going wrong when I see the above error? I'm using the latest RssAtomBundle bundle with Symfony4.

PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function append() on null in /platform/symfony/vendor/debril/feed-io/src/FeedIo/Feed/ElementsAwareTrait.php:66
Stack trace:
#0 /platform/symfony/vendor/debril/feed-io/src/FeedIo/Rule/OptionalField.php(30): FeedIo\Feed\Node->addElement(Object(FeedIo\Feed\Node\Element))
#1 /platform/symfony/vendor/debril/feed-io/src/FeedIo/Parser/XmlParser.php(114): FeedIo\Rule\OptionalField->setProperty(Object(App\Entity\Feed), Object(DOMElement))
#2 /platform/symfony/vendor/debril/feed-io/src/FeedIo/Parser/XmlParser.php(95): FeedIo\Parser\XmlParser->handleNode(Object(App\Entity\Feed), Object(DOMElement), Object(FeedIo\RuleSet))
#3 /platform/symfony/vendor/debril/feed-io/src/FeedIo/Parser/XmlParser.php(53): FeedIo\Parser\XmlParser->parseNode(Object(App\Entity\Feed), Object(DO in /platform/symfony/vendor/debril/feed-io/src/FeedIo/Feed/ElementsAwareTrait.php on line 66
alexdebril commented 6 years ago

I think I know what's going on here, correct me if I'm wrong : you extended \FeedIo\Feed in an Entity class (something like \App\Entity\Feed or \App\Entity\News) and you didn't call parent::__construct() in your Entity class. If so, just add parent::__construct() in the __construct() of your class and it will work. Not obvious, I should add it in the documentation.

If I'm wrong : could you paste the code please ?

nicodemuz commented 6 years ago

That could have been the issue yeah.. I did a whole bunch of changes last night and got it working..

As a quickfix I had changed the addElement method to:

/**
 * @param  ElementInterface $element
 * @return $this
 */
public function addElement(ElementInterface $element)
{
    if ($this->elements === null) {
        $this->elements = new \ArrayIterator();
    }
    $this->elements->append($element);

    return $this;
}

Removing the quickfix seems to work now. I'm also calling the parent constructor now for my feed. Thanks for your help! :)