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

Relations between 2 namespace models.

Hi all,

Here is the problem:

Model A:

namespace Testing\Models;
class A extends \Phalcon\Mvc\Model
{
       public function initialize()
       { 
                $this->belongsTo("b_id", "B", "id");
       }
}

Model B:

namespace Testing\Models;
class B extends \Phalcon\Mvc\Model
{
       public function initialize()
       { 
                $this->hasMany("id", "A", "b_id");
       }
}

Controller A:

use Testing\Models\A;
use Testing\Models\B;
class AController extents ControllerBase
{
        public someAction()
        {
              $a =  A::findFirst(1);
              echo $a->B->oneField;              
        }
}

Screen print out: Model 'B' could not be loaded.



7.0k
Accepted
answer
edited Oct '14

Never mind, solved the problem.

Changes made in model A:

$this->belongsTo("b_id", "Testing\Models\B", "id", array(
      "alias" => "B"
));


643
edited Apr '15

Hello, everyone

Is it possible to set up an alias here?

        $this->hasManyToMany(
            'id',
            'Imhoimho\Frontend\Models\SurveysSurveyquestions',
            'surveys_id', 'surveyquestions_id',
            'Imhoimho\Frontend\Models\Surveyquestions',
            'id'
        );