PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 14, 2022

[FIXED] How to get all the tables name of particular type of associations with current table

 January 14, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I want to get all the table names, associated with my current table. Let's consider my current table is Products, I have association like

$this->belongsToMany('Categories', [
        'className' => 'Categories',
        'foreignKey' => 'product_id',
        'targetForeignKey' => 'category_id',
        'joinTable' => 'categories_products'
    ]);
$this->belongsToMany('Images', [
        'className' => 'Images',
        'foreignKey' => 'product_id',
        'targetForeignKey' => 'image_id',
        'joinTable' => 'images_products'
    ]);

What I want is the associated table name with products with association type of belongsToMany.

What I did yet is the following code but It returns a object of protected data.

$this->Products->associations()->type('belongsToMany')

Solution

you can loop through the associations and get the table name

$associations =  $this->Products->associations()->type('belongsToMany');

$tables = [];

foreach($associations as $association)
{
    $tables[] = $association->getTarget()->getTable();
}

probably there is also a faster way using Collections



Answered By - arilia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing