Php 如何获取与当前表关联的特定类型的所有表名
我想获取与当前表关联的所有表名。 让我们考虑一下当前表是<强>产品>,我的联想类似Php 如何获取与当前表关联的特定类型的所有表名,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我想获取与当前表关联的所有表名。 让我们考虑一下当前表是产品>,我的联想类似 $this->belongsToMany('Categories', [ 'className' => 'Categories', 'foreignKey' => 'product_id', 'targetForeignKey' => 'category_id', 'joinTable' => 'categories_prod
$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'
]);
我想要的是与关联类型为BelongToMany的产品关联的表名
我所做的是下面的代码,但它返回一个受保护数据的对象
$this->Products->associations()->type('belongsToMany')
您可以循环关联并获取表名
$associations = $this->Products->associations()->type('belongsToMany');
$tables = [];
foreach($associations as $association)
{
$tables[] = $association->getTarget()->getTable();
}
可能还有一种更快的使用集合的方法它给出了一个错误“Unknown method”getTarget“@阿里利亚