georgringer / news

TYPO3 Extension news
GNU General Public License v2.0
264 stars 358 forks source link

Extending news at custom extension, Error: Typed property GeorgRinger\News\Domain\Model\News::$commentRepository must not be accessed before initialization #2153

Closed topsnet-holgerd closed 1 year ago

topsnet-holgerd commented 1 year ago

When extending news with some additional code in my siteconfig

class News extends \GeorgRinger\News\Domain\Model\News {
    protected CommentRepository $commentRepository;
    public function __construct(CommentRepository $commentRepository)
    {
        $this->commentRepository = $commentRepository;
     }  
    /**
    * @return \T3\PwComments\Domain\Model\Comment
    */
    public function getLatestComment() {
        //some code
        return = $this->commentRepository->findByUid($row['uid']);
    }
}

in the cached files the construct method always is at the end of the file (e.g. typo3temp/var/cache/code/news/tx_news_domain_model_news.php) together with the other constructions out of the standard news extension and after my custom function (getLatestComment()). So I can understand the error message but wonder how this can be fixed. Do you have any hint?

This is, what is in the consolidated cached file (excerpt form the end):

    protected CommentRepository $commentRepository;
    /**
    * @return \T3\PwComments\Domain\Model\Comment
    */
    public function getLatestComment() {
        //some code
    return = $this->commentRepository->findByUid($row['uid']);
    }

    /**
     * Initialize categories and media relation
     *
     * @return \GeorgRinger\News\Domain\Model\News
     */
    public function __construct()
    {
        $this->categories = new ObjectStorage();
        $this->contentElements = new ObjectStorage();
        $this->relatedLinks = new ObjectStorage();
        $this->falMedia = new ObjectStorage();
        $this->falRelatedFiles = new ObjectStorage();
        $this->tags = new ObjectStorage();
    $this->commentRepository = $commentRepository;
    }
georgringer commented 1 year ago

having a repository in the model is really not the way to go. if this is a proper relation in tca, there is no need for such hack - otherwise use a VH to fetch the comments