Version: 4.3.4 Reporter: Warble Media —
I am working on an add-on that has a many-to-many relationship with the ee:Channel model.
I set up my relationship as follows:
protected static $_relationships = array(
'Channels' => array(
'type' => 'hasAndBelongsToMany',
'model' => 'ee:Channel',
'pivot' => array(
'table' => 'my_addon_campaigns_channels',
'left' => 'campaign_id',
),
'weak' => true,
'inverse' => array(
'name' => 'Campaigns',
'type' => 'hasAndBelongsToMany',
),
),
);
But was receiving the following error:
Undefined index: pivot
ee/EllisLab/ExpressionEngine/Service/Model/Relation/HasAndBelongsToMany.php, line 200
I tried adding details of the pivot table to the relationship, but it wasn’t picking it up.
After diving into the EE core code, I found that this is because EllisLab\ExpressionEngine\Service\Model\Relation\Relation::getInverseOptions() rebuilds the options for the inverse relationship, but doesn’t include the details for the pivot table.
From what I can tell, this means that there is no way to configure many-to-many relationships to work with foreign models.
I have fixed the issue for myself by adding the following method to the EllisLab\ExpressionEngine\Service\Model\Relation\HasAndBelongsToMany class:
public function getInverseOptions()
{
$options = parent::getInverseOptions();
$options['pivot'] = array(
'table' => $this->pivot['table'],
'left' => $this->pivot['right'],
'right' => $this->pivot['left'],
);
return $options;
}
ExpressionEngine implements Markdown Extra and BBCode. Please see the Markdown Extra docs and the BBCode Wikipedia article for a full reference.
**bold**
, __bold__
, *italics*
, _italics_
, ~strike/del~
, `code()`
bold, italics, strike/del, code()
Link: [link title](https://example.com)
Image: ![alt text](https://example.com/image.jpg)
[blockquote]...[/blockquote]
, [quote]...[/quote]
, and Markdown style:
> Some quoted text. > > This is all one quote.
[code]...[/code]
, and you can also specify the language for syntax highlighting, [code=php]...[/code]
GitHub flavored Markdown code fences are also supported:
``` public function decoderRing($str) { return str_rot13($str); } ```