Tell me, friends, there is a table users (id-PKEY). And I want to make profiles for each user (year, place of birth, interests, etc.). It is assumed that each user will correspond to one record (one profile) from the table profiles.

Tell me how to connect these models with each other?

Customuser.php <?php defined('SYSPATH') or die('No direct script access.'); class Model_Customuser extends Model { protected $_table = 'user'; protected $_has_one = array( 'profiles' => array( 'model' => 'Profile', 'foreign_key' => 'user_id', ), ); } 

Profile.php <? Php defined ('SYSPATH') or die ('No direct script access.');

 class Model_Profile extends ORM { protected $_has_one = array( 'user' => array( 'model' => 'Customuser', 'foreign_key' => 'id', ), ); } // End 

table structures:

 | users | CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(254) NOT NULL, `username` varchar(32) NOT NULL DEFAULT '', `password` varchar(64) NOT NULL, `logins` int(10) unsigned NOT NULL DEFAULT '0', `last_login` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `uniq_username` (`username`), UNIQUE KEY `uniq_email` (`email`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 | |profiles | CREATE TABLE `profiles` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `age` int(11) NOT NULL DEFAULT '0', `weight` int(11) NOT NULL DEFAULT '0', `city` int(11) DEFAULT NULL, `nickname` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `UK_profiles_user_id` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | На контроллере пытаюсь получить доступ к профайлу: $user = ORM::factory('customuser',1); $city = $user->profiles->city; print $city; 

As a result, I get the error: Undefined property $profiles;

    2 answers 2

    In version 3.3 you need to specify the name of a case-sensitive model ( except in some cases ). And that means:

    $ user = ORM :: factory ('Customuser', 1);

    The note. Not a good idea to call the has_one link in the plural: profiles . He is one - profile . This time.
    If in one model has_one , then in the second - belongs_to . Differences in the storage location of the link. These are two.

    • absolutely right. everything worked out. thank. - Vfvtnjd

    $_has_one does not load the object into the model! This is how it should be:

     $object->has_one_relation->find()->relation_property; 
    • thanks, so here all the same is not true? kohanaframework.su/database/orm_connections1 - Vfvtnjd
    • @ kemerov4anin, in this case, you are wrong. For $_has_one do not call find() . See the @Vfvtnjd code , dump your $user and see what is there and what is not. - xEdelweiss