Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

$has_many Tags with non standard $key_field

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

Table partner, with PK = partner.an_id.

<?php
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.

Comments

  • If you change line 5782 to the following, does it work:

    $this->_CI->$foreign_model->db()->where_in("{$related_table_name}.".$this->_CI->$foreign_model->key_field(), $rel_ids);
    
Sign In or Register to comment.