$has_many Tags with non standard $key_field

my model can't output tags because my Primary Key is not named id

Table partner, with PK = partner.an_id.

class Partner_model extends Base_module_model {

    protected $key_field = 'an_id';

    public $has_many = [
        'tags' => [
            //'app' => 'my_fuel_tags_model',
            FUEL_FOLDER  => 'fuel_tags_model',
            'where' => ['fuel_tags.context' => 'partner']

Setting relationship between partner and fuel_tags in backend works fine.

If I want to access $partner->tags in the frontend (via controller) I get an SQL error

Unknown column 'fuel_tags.an_id' in 'where clause'

SELECT fuel_tags.* FROM fuel_tags LEFT JOIN fuel_categories ON fuel_categories.id = fuel_tags.category_id WHERE fuel_tags.an_id IN(9, 11, 10) AND fuel_tags.context = 'partner' AND fuel_tags.published = 'yes'

Filename: /fuel/modules/fuel/core/MY_Model.php
Line Number: 481

MY_Model->_get_relationship() seems to access $id_field = $this->_parent_model->key_field(); to create the WHERE IN query $this->_CI->$foreign_model->db()->where_in("{$related_table_name}.".$id_field, $rel_ids);

If my partner model had an PK field idinstead of an_id I believe it would work, but I can't rename the field.
What is the solution? How can I access the tags?

Appreciate your advice.


Sign In or Register to comment.