Class Associations

Types of Associations in CakePHP 4(Added in Model/Initialize)


Controller Action

    function associations(){
        /*
         // Class name: CodeBlocks
         // belongsTo
        $this->belongsTo('CodeBlockTypes', [
            'foreignKey' => 'code_block_type_id'
        ]);

        // Class name: CodeBlockTypes
         // hasMany
        $this->hasMany('CodeBlocks', [
            'foreignKey' => 'code_block_type_id',
        ]);

        //class name:Locations
          // belongsToMany
        $this->belongsToMany('Users', [
            'joinTable' => 'locations_users',
            'foreignKey' => 'location_id',
            'targetForeignKey' => 'user_id', //join table column - product_id
        ]);
            // class name:Users
         // belongsToMany
        $this->belongsToMany('Locations', [
            'joinTable' => 'locations_users',
            'foreignKey' => 'user_id',
            'targetForeignKey' => 'location_id', //join table column - product_id
        ]);

        */