Problem With Simple Relations


I have these two tables. Folder and File. You can probably imagine the relations between these two.

In the Folder class I have this relations:

return [

	'files' => [self::HAS_MANY, 'File', 'folder_id'],

	//'folder' => [self::BELONGS_TO, 'Folder', 'folder_id'],

	//'folders' => [self::HAS_MANY, 'Folder', 'folder_id'],

	//'institution' => [self::BELONGS_TO, 'Institution', 'institution_id'],


But the $model->files returns empty.

On other hand I have this relations in File class:

return [

	'folder' => [self::BELONGS_TO, 'Folder', 'folder_id'],

	//'institution' => [self::BELONGS_TO, 'Institution', 'institution_id'],


Which works without any problem.

Do you know why?

Any help is appreciated :)

[s]Try this:

'files' => [self::BELONGS_TO, 'File', 'folder_id'],


Ignore that, I completely misread your problem. It looks like your relation should work. Can you show more of the code surrounding the problem?

Hi friend,

I believe that I found my mistake.

I had declared __construct in the Folder class, and it obviously overwrites attributes.

I found that isNewRecord keep on saying true when findByPk or findByAttributes and this led me to the solution.

Thanks a lot for your time and answer.