belongsToMany 通过使用 joinTable

belongsToMany through using joinTable

使用 CakePhp 3.0.10

我想使用 through 选项创建 belongsTo 关系,但我还需要设置 joinTable 选项,因为我的 table 有一个前缀

如果我按照书中的例子写:

$this->belongsToMany('Courses', [
    'through' => 'CourseMemberships',
    'joinTable' => 'prefix_course_memberships',

]);

我收到错误:

1146 Table 'course_memberships' doesn't exist

这是一个错误还是有办法强制 cakephp 使用我的 table?

through 选项取代了 joinTable 选项,查看代码,这是预期的行为。

https://github.com/cakephp/.../src/ORM/Association/BelongsToMany.php#L173-L184

因此,如果您想在使用 through 时更改 table 名称,只需在 CourseMembershipsTable class 中更改它,就像您对任何其他 table class

public function initialize(array $config)
{
    $this->table('prefix_course_memberships');
    // ...
}