tattersoftware / codeigniter4-relations

Entity relationships for CodeIgniter 4
MIT License
87 stars 20 forks source link

Clear Documentation with Some Examples #14

Closed eafarooqi closed 3 years ago

eafarooqi commented 3 years ago

Hi,

This may not be an issue but i didn't find another place to post about detail documentation or examples required.

i have followed all these steps

Install with Composer: > composer require tatter/relations
Add the trait to your model: use \Tatter\Relations\Traits\ModelTrait
Load relations: $users = $userModel->with('groups')->findAll();
Add the trait to your entity: use \Tatter\Relations\Traits\EntityTrait
Load relations: foreach ($user->groups as $group)

but still there is something missing which need to be done, because the error is Table {name} is not known to be related to {related-Table-name}

MGatner commented 3 years ago

Can you share some of the code that gets you that error? And your database schema? If you don't feel comfortable sharing those please recreate a simple example that causes the issue.

eafarooqi commented 3 years ago
CREATE TABLE meetings  (
  id int(11) NOT NULL AUTO_INCREMENT,
  title varchar(50) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
  address varchar(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL,
  PRIMARY KEY (id) USING BTREE
)

CREATE TABLE meeting_teams  (
  id int(11) NOT NULL AUTO_INCREMENT,
  meeting_id int(11) NULL DEFAULT NULL,
  team_id int(11) NULL DEFAULT NULL,
  PRIMARY KEY (id) USING BTREE,
  INDEX fk_mt_meetings_id(meeting_id) USING BTREE,
  INDEX fk_mt_teams_id(team_id) USING BTREE,
  CONSTRAINT fk_mt_meetings_id FOREIGN KEY (meeting_id) REFERENCES meetings (id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_mt_teams_id FOREIGN KEY (team_id) REFERENCES teams (id) ON DELETE CASCADE ON UPDATE CASCADE
)

Model use \Tatter\Relations\Traits\ModelTrait;

protected $table = 'meetings';
protected $primaryKey = 'id';

$this->meetingModel->with('teams')->find(1);

MGatner commented 3 years ago

That looks correct. The join table should be both plural (meetings_teams) but because of the foreign keys it should have picked up on the relationship. Can you please share your actual detected schema? Try php spark schemas -print or the Debug Toolbar collector view.

eafarooqi commented 3 years ago

Tatter\Schemas\Structures\Schema (1) ( public 'tables' -> Tatter\Schemas\Structures\Mergeable (51) (

    public 'meeting_teams' -> Tatter\Schemas\Structures\Table (6) (
        public 'name' -> string (13) "meeting_teams"
        public 'pivot' -> boolean false
        public 'fields' -> Tatter\Schemas\Structures\Mergeable (3) (
            public 'id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (2) "id"
                public 'primary_key' -> integer 1
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'meeting_id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (10) "meeting_id"
                public 'primary_key' -> integer 0
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean true
                public 'default' -> null
            )
            public 'team_id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (7) "team_id"
                public 'primary_key' -> integer 0
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean true
                public 'default' -> null
            )
        )
        public 'indexes' -> Tatter\Schemas\Structures\Mergeable (3) (
            public 'PRIMARY' -> Tatter\Schemas\Structures\Index (3) (
                public 'name' -> string (7) "PRIMARY"
                public 'type' -> string (7) "PRIMARY"
                public 'fields' -> array (1) [
                    0 => string (2) "id"
                ]
            )
            public 'fk_mt_meetings_id' -> Tatter\Schemas\Structures\Index (3) (
                public 'name' -> string (17) "fk_mt_meetings_id"
                public 'type' -> string (5) "INDEX"
                public 'fields' -> array (1) [
                    0 => string (10) "meeting_id"
                ]
            )
            public 'fk_mt_teams_id' -> Tatter\Schemas\Structures\Index (3) (
                public 'name' -> string (14) "fk_mt_teams_id"
                public 'type' -> string (5) "INDEX"
                public 'fields' -> array (1) [
                    0 => string (7) "team_id"
                ]
            )
        )
        public 'foreignKeys' -> Tatter\Schemas\Structures\Mergeable (2) (
            public 'fk_mt_meetings_id' -> Tatter\Schemas\Structures\ForeignKey (5) (
                public 'constraint_name' -> string (17) "fk_mt_meetings_id"
                public 'table_name' -> string (13) "meeting_teams"
                public 'column_name' -> string (10) "meeting_id"
                public 'foreign_table_name' -> string (8) "meetings"
                public 'foreign_column_name' -> string (2) "id"
            )
            public 'fk_mt_teams_id' -> Tatter\Schemas\Structures\ForeignKey (5) (
                public 'constraint_name' -> string (14) "fk_mt_teams_id"
                public 'table_name' -> string (13) "meeting_teams"
                public 'column_name' -> string (7) "team_id"
                public 'foreign_table_name' -> string (5) "teams"
                public 'foreign_column_name' -> string (2) "id"
            )
        )
        public 'relations' -> Tatter\Schemas\Structures\Mergeable (2) (
            public 'meetings' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (8) "meetings"
                public 'type' -> string (9) "belongsTo"
                public 'singleton' -> boolean true
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (13) "meeting_teams"
                        1 => string (10) "meeting_id"
                        2 => string (8) "meetings"
                        3 => string (2) "id"
                    ]
                ]
            )
            public 'teams' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (5) "teams"
                public 'type' -> string (9) "belongsTo"
                public 'singleton' -> boolean true
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (13) "meeting_teams"
                        1 => string (7) "team_id"
                        2 => string (5) "teams"
                        3 => string (2) "id"
                    ]
                ]
            )
        )
    )
    public 'meetings' -> Tatter\Schemas\Structures\Table (8) (
        public 'name' -> string (8) "meetings"
        public 'pivot' -> boolean false
        public 'fields' -> Tatter\Schemas\Structures\Mergeable (10) (
            public 'id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (2) "id"
                public 'primary_key' -> boolean true
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'title' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (5) "title"
                public 'primary_key' -> null
                public 'type' -> string (7) "varchar"
                public 'max_length' -> integer 50
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'start_date' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (10) "start_date"
                public 'primary_key' -> null
                public 'type' -> string (8) "datetime"
                public 'max_length' -> null
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'end_date' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (8) "end_date"
                public 'primary_key' -> null
                public 'type' -> string (8) "datetime"
                public 'max_length' -> null
                public 'nullable' -> boolean true
                public 'default' -> null
            )
            public 'type' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (4) "type"
                public 'primary_key' -> null
                public 'type' -> string (4) "enum"
                public 'max_length' -> null
                public 'nullable' -> boolean false
                public 'default' -> string (6) "online"
            )
            public 'detail' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (6) "detail"
                public 'primary_key' -> null
                public 'type' -> string (7) "varchar"
                public 'max_length' -> integer 255
                public 'nullable' -> boolean true
                public 'default' -> null
            )
            public 'base' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (4) "base"
                public 'primary_key' -> null
                public 'type' -> string (4) "enum"
                public 'max_length' -> null
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'address' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (7) "address"
                public 'primary_key' -> integer 0
                public 'type' -> string (7) "varchar"
                public 'max_length' -> integer 255
                public 'nullable' -> boolean true
                public 'default' -> null
            )
            public 'team_id' -> Tatter\Schemas\Structures\Field (2) (
                public 'name' -> string (7) "team_id"
                public 'primary_key' -> null
            )
            public 'role_id' -> Tatter\Schemas\Structures\Field (2) (
                public 'name' -> string (7) "role_id"
                public 'primary_key' -> null
            )
        )
        public 'indexes' -> Tatter\Schemas\Structures\Mergeable (1) (
            public 'PRIMARY' -> Tatter\Schemas\Structures\Index (3) (
                public 'name' -> string (7) "PRIMARY"
                public 'type' -> string (7) "PRIMARY"
                public 'fields' -> array (1) [
                    0 => string (2) "id"
                ]
            )
        )
        public 'foreignKeys' -> Tatter\Schemas\Structures\Mergeable (0) ()
        public 'relations' -> Tatter\Schemas\Structures\Mergeable (5) (
            public 'meeting_agenda' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (14) "meeting_agenda"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (8) "meetings"
                        1 => string (2) "id"
                        2 => string (14) "meeting_agenda"
                        3 => string (10) "meeting_id"
                    ]
                ]
            )
            public 'meeting_members' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (15) "meeting_members"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (8) "meetings"
                        1 => string (2) "id"
                        2 => string (15) "meeting_members"
                        3 => string (10) "meeting_id"
                    ]
                ]
            )
            public 'meeting_protocol' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (16) "meeting_protocol"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (8) "meetings"
                        1 => string (2) "id"
                        2 => string (16) "meeting_protocol"
                        3 => string (10) "meeting_id"
                    ]
                ]
            )
            public 'meeting_roles' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (13) "meeting_roles"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (8) "meetings"
                        1 => string (2) "id"
                        2 => string (13) "meeting_roles"
                        3 => string (10) "meeting_id"
                    ]
                ]
            )
            public 'meeting_teams' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (13) "meeting_teams"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (8) "meetings"
                        1 => string (2) "id"
                        2 => string (13) "meeting_teams"
                        3 => string (10) "meeting_id"
                    ]
                ]
            )
        )
        public 'model' -> string (31) "App\Models\Meeting\MeetingModel"
        public 'returnType' -> string (20) "App\Entities\Meeting"
    )
    public 'teams' -> Tatter\Schemas\Structures\Table (8) (
        public 'name' -> string (5) "teams"
        public 'pivot' -> boolean false
        public 'fields' -> Tatter\Schemas\Structures\Mergeable (4) (
            public 'id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (2) "id"
                public 'primary_key' -> boolean true
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'team' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (4) "team"
                public 'primary_key' -> null
                public 'type' -> string (7) "varchar"
                public 'max_length' -> integer 50
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'tpregion_id' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (11) "tpregion_id"
                public 'primary_key' -> null
                public 'type' -> string (3) "int"
                public 'max_length' -> integer 11
                public 'nullable' -> boolean false
                public 'default' -> null
            )
            public 'level' -> Tatter\Schemas\Structures\Field (6) (
                public 'name' -> string (5) "level"
                public 'primary_key' -> null
                public 'type' -> string (4) "enum"
                public 'max_length' -> null
                public 'nullable' -> boolean false
                public 'default' -> null
            )
        )
        public 'indexes' -> Tatter\Schemas\Structures\Mergeable (1) (
            public 'PRIMARY' -> Tatter\Schemas\Structures\Index (3) (
                public 'name' -> string (7) "PRIMARY"
                public 'type' -> string (7) "PRIMARY"
                public 'fields' -> array (1) [
                    0 => string (2) "id"
                ]
            )
        )
        public 'foreignKeys' -> Tatter\Schemas\Structures\Mergeable (0) ()
        public 'relations' -> Tatter\Schemas\Structures\Mergeable (3) (
            public 'meeting_teams' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (13) "meeting_teams"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (5) "teams"
                        1 => string (2) "id"
                        2 => string (13) "meeting_teams"
                        3 => string (7) "team_id"
                    ]
                ]
            )
            public 'team_requests' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (13) "team_requests"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (5) "teams"
                        1 => string (2) "id"
                        2 => string (13) "team_requests"
                        3 => string (7) "team_id"
                    ]
                ]
            )
            public 'users' -> Tatter\Schemas\Structures\Relation (4) (
                public 'table' -> string (5) "users"
                public 'type' -> string (7) "hasMany"
                public 'singleton' -> null
                public 'pivots' -> array (1) [
                    0 => array (4) [
                        0 => string (5) "teams"
                        1 => string (2) "id"
                        2 => string (5) "users"
                        3 => string (7) "team_id"
                    ]
                ]
            )
        )
        public 'model' -> string (27) "App\Models\Manage\TeamModel"
        public 'returnType' -> string (17) "App\Entities\Team"
    )
)

)

Called from .../tatter/schemas/src/Archiver/Handlers/CliHandler.php:18 [d()]

MGatner commented 3 years ago

It is detecting meeting_teams as its own table with relationships to both meetings and teams. This is likely because the table name is not plural like I mentioned above. You could change your table name or check out the info at Tatter\Schemas for how to create an override.

eafarooqi commented 3 years ago

yes but that's why i have added the foreign keys so it detect the relation. is this a bug ?

MGatner commented 3 years ago

Not a bug, the foreign keys are detected correctly but there is no logic in the database drafter that says "if I have two foreign keys then I am a join table". You either need to follow the naming convention of PluralTable1_PluralTable2 or specify the relationship manually.

See: https://github.com/tattersoftware/codeigniter4-schemas/blob/4b7fc16e73c2dee2cfcf468a7eff8935fd592920/src/Drafter/Handlers/DatabaseHandler.php#L196