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

Join from the same table

I'm trying to join tables PostsCategories

What i'm trying to do is print all the categories, but if a categorie has a parent categorie, print the categorie :-)

Hope you understand!

( What i am trying so far )

        $this->modelsManager->createBuilder()
        ->columns(array('PostsCategories.name', 'parent_categorie.parent as parent'))
        ->from('PostsCategories')
        ->join('PostsCategories as parent_categorie')
        ->orderBy('PostsCategories.id DESC')
        ->limit('9')
        ->getQuery()
        ->execute();
edited Sep '15

You'll need an alias for both tables:

 $this->modelsManager->createBuilder()
        ->columns('c.name, p.name parent')
        ->from(array('c'=>'PostsCategories'))
        ->join('PostsCategories', 'c.parent_id=p.id', 'p')
        ->orderBy('c.id DESC')
        ->limit(9)
        ->getQuery()
        ->execute();

Docs: https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

I'm trying to do something like this:

Categories:

Test 1
test 2
test 3
    Test 1
    test 2
    test 4
test 4
test 5
    test 1
    test 2

How do i do this??