We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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"
));


637
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'
        );