create a relation between two models without tables relationship


I have a problem, I have a database with two tables should be related, but they are not and have no way to create that relationship.

one of the tables has a field that could be related to the key field in the other table.

I tried to use the functions of relationship that brings models yii2 default, but does nothing and do not know if I need to do something else to create the relationship through models only.

What are your 2 tables like? Could you show us the schema?

And what are your 2 models like? Are they extending ActiveRecord? Or are they extending Model?

What do you mean by “without tables relationship”? You mean that you can’t establish a foreign key constraint between 2 fields on database level?

I found the problem… it is the type field, one is char and another is varchar. I cloned the DB to check it and when i changed type field i could access the other model.

Yes I can’t establish any foreign key constraint on DB level.


I’ve never tried such a relation relying on char and/or varchar fields, but by manually defining a relation you could achieve some sort of relational functionality with it. For instance, lazy loading of a related model, eager loading of the relation using “with” may work, because they only use “where” clause. But other features using table joining may not work.