CakePHP 3.4 foreign key not work

467 views Asked by At

I have a simple structure of tables:

CREATE TABLE `programs` (
`id` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`key` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(30) DEFAULT NULL,
`installation_date` date DEFAULT NULL,
`activation_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `users` (
`id` int(30) NOT NULL,
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(255) NOT NULL,
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`second_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`activated` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `programs`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id` (`user_id`) USING BTREE;

ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);

The PHPMyAdmin scheme: scheme

Then, I generate tables and models classes by bin\cake bake model users and bin\cake bake model programs.

But $SomeSuccessfullyLoadedUser->programs always return null. I am sure to exist row (in table programs) with existing user_id, related with users.id , and I`m sure to entity loaded successfully.

What should I do to use accessing by foreign key?

P.S ProgramsTable

class ProgramsTable extends Table
{
  public function initialize(array $config)
  {
    parent::initialize($config);

    $this->setTable('programs');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->belongsTo('Users', [
        'foreignKey' => 'user_id'
    ]);
  }

//validationDefault


  public function buildRules(RulesChecker $rules)
  {
    $rules->add($rules->existsIn(['user_id'], 'Users'));

    return $rules;
  }
}

UsersTabel

class UsersTable extends Table
{
  public function initialize(array $config)
  {
    parent::initialize($config);

    $this->setTable('users');
    $this->setDisplayField('name');
    $this->setPrimaryKey('id');

    $this->hasMany('Programs', [
        'foreignKey' => 'user_id'
    ]);
    $this->hasMany('SupportTickets', [
        'foreignKey' => 'user_id'
    ]);
  }


//validationDefault


  public function buildRules(RulesChecker $rules)
  {
    $rules->add($rules->isUnique(['email']));

    return $rules;
  }

My app doesn't need ProgramsController, I load Programs model in AppController. UsersController has simple actions like login, register, logout, etc. No specific for use ProgramsModel.

P.P.S For example (in UsersController)

$SomeSuccessfullyLoadedUser= $this->Users->get($this->Auth->user()['id']);
debug($SomeSuccessfullyLoadedUser->email);      //prints user's email
debug($SomeSuccessfullyLoadedUser->programs);   //prints 'null'
1

There are 1 answers

0
Mazba Kamal On BEST ANSWER

you have to add the class name to contain array

$SomeSuccessfullyLoadedUser= $this->Users->get($this->Auth->user()['id'],
[
'contain'=>['Programs']
]);

for details use this link