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;