There are 2 tables: users and rules
Users:

 id name login password email rule_id 

Rules:

 id name` 

There are also 2 models:

Users:

 class Users extends Model { public $id; public $name; public $login; public $password; public $email; public $rule_id; public function initialize() { $this->hasOne("rule_id", "Rules", "id"); } } 

Rules:

 class Rules extends Model { public $id; public $name; public function initialize() { $this->hasMany("id", "Users", "rule_id"); } } 

IndexController:

 class IndexController extends Controller { public function indexAction() { $user = Users::findFirst(23); echo "Id: " . $user->id . ", Name: " . $user->name . ", Login: " . $user->login . ", Password: " . $user->password . ", Email: " . $user->email . ", Rule: " . $user->rules->name . "\n"; } } 

The following error occurs:

Model 'Rules' could not be loaded

Help to understand please.

    1 answer 1

    I would check three items:

    The first. Feedback for hasMany should be set via the belongsTo() method, not hasOne() .

    Thus, the Users model becomes:

     class Users extends Model { public $id; public $name; public $login; public $password; public $email; public $rule_id; public function initialize() { $this->belongsTo("rule_id", "Rules", "id"); } } 

    The second. Did you forget to specify the namespace?
    Suppose the Rules and Users models belong to the Models namespace.
    Then the code will look like this:

    Rules

     class Rules extends Model { public $id; public $name; public function initialize() { $this->hasMany("id", "\Models\Users", "rule_id"); } } 

    Users

     class Users extends Model { public $id; public $name; public $login; public $password; public $email; public $rule_id; public function initialize() { $this->belongsTo("rule_id", "\Models\Rules", "id"); } } 

    Third. If the first two items did not help, try setting alias manually.

    Users

     class Users extends Model { public $id; public $name; public $login; public $password; public $email; public $rule_id; public function initialize() { // На всякий случай проверьте alias и в нижнем регистре $this->belongsTo("rule_id", "\Models\Rules", "id", array('alias' => 'Rules')); } } 
    • Thank you very much, the error disappeared, but now another problem has arisen how to display the associated field? $ user = Users :: findFirst (23); echo "Id:". $ user-> id. ", Name:". $ user-> name. ", Login:". $ user-> login. "Password:". $ user-> password. "Email:". $ user-> email. ", Rule:". $ user-> rules-> id. "\ n"; - El Salvadore
    • Have you checked the contents of $user->rules ? What var_dump($user->rules); displays var_dump($user->rules); ? Have you tried changing the alias register ah? - VenZell
    • Displays null, I created without alias - El Salvadore
    • Check manually if there is an entry in the rules table with id equal to rule_id for entry # 23 in the users table. It looks like an empty sample. - VenZell
    • Yes, there is, Here is the error code Notice: Access to undefined property Multiple\Backend\Models\Users::rules in C:\xampp\htdocs\progbook.uz\apps\backend\controllers\IndexController.php on line 14 NULL - El Salvadore