We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model relation with namespace

So i'm havimg problems with model relations , when i try to see all comments from the article ( i tried to var_dump , yet it seems not to exist , returns null ) see my code :

Articles Modal:


<?php

namespace Frontend\Models;

class Articles extends \Phalcon\Mvc\Model
{

    public function getSource()
    {
        return "news";
    }

    public function initialize()
    {
        $this->hasMany("_", "Frontend\Models\ArticleComments", "post");
    }

}

Articles Comments Modal:


<?php

namespace Frontend\Models;

class ArticleComments extends \Phalcon\Mvc\Model
{

    public function getSource()
    {
        return "news_comments";
    }

    public function initialize()
    {
        $this->belongsTo("post", "Frontend\Models\Articles", "_");
    }

}

Atricles Controller :


<?php

namespace Frontend\Controllers;

use Frontend\Models\Articles as Articles,
    Frontend\Models\ArticleComments as ArticleComments,
    Frontend\Models\ArticleCategories as ArticleCategories;

class ArticlesController extends ControllerBase
{

    public function PostAction()
    {

        $article  = Articles::findFirstByUrlrequest($this->dispatcher->getParam(0));
        $comments = $article->articlecomments;
        $category = ArticleCategories::findFirst($article->category);

        $this->view->setVar("article", $article);
        $this->view->setVar("comments", $comments);
        $this->view->setVar("category", $category);

    }

}

you have a field in database called "_" ?

you have a field in database called "_" ?

Yes , the "_" is the Primary key ( insted of calling it id )

That's pretty non-standard & might be causing some issues - certain layers might convert "my_id" to "myId" for example. Try renaming the column & see what happens.

Also I believe you'll want to use an alias when using namespaces. https://docs.phalcon.io/en/latest/reference/models.html#aliasing-relationships

Also I believe you'll want to use an alias when using namespaces. https://docs.phalcon.io/en/latest/reference/models.html#aliasing-relationships

Still returns null :/