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.

how to handle has_many option in custom module

edited June 2016 in Bug Reports
hi i am using the latest version of fuel :
i have used a relation like as follow
public $has_many = array('speaker_id' => array('model' => array( FUEL_FOLDER => 'speakers_model' ),'where' => array('is_disabled' => 0)));
it is generation a filed multi select every thing is quit fine if i select a single option but it is giving error while i am selecting multiple option. like as follow :

A PHP Error was encountered
Severity: Notice

Message: Array to string conversion

Filename: core/MY_DB_mysqli_driver.php

Line Number: 277

i have tried : following function's in model

function save($record=array(), $validate=TRUE,$ignore_on_insert=TRUE, $clear_related=NULL)
{
if(isset($record['speaker_id']))
{
$record['speaker_id']=(is_array($record['speaker_id']))?implode(',', $record['speaker_id']):$record['speaker_id'];
}
return parent::save($record, $validate, $ignore_on_insert, $clear_related);
}
function form_fields($values = array(),$related=array())
{

if(isset($values['speaker_id']))
{

$values['speaker_id']= explode(',', $values['speaker_id']);
}
$fields = parent::form_fields($values);
return $fields;
}
this fixed the error but not showing the selected items while editing

in this table speaker_id is varchar


what is the mistake i am doing while using the normal way - can one help me with it ???? what way should i take to resolve it ???

Comments

  • edited 1:53PM
    Where does your "speakers_model" live? If it is in the fuel/application/models folder then I'd try the following:
    public $has_many = array('speaker_id' => array('model' => 'speakers_model', 'where' => array('is_disabled' => 0)));
    Note that the array(FUEL_FOLDER => 'speakers_model') was replaced with just 'speakers_model' which refers to the main application/model folder.
  • edited 1:53PM
    as per that i have changed it to
    public $has_many =array('speaker_id' => array('model' => array( 'events'=>'speakers_model'), 'where' => array('is_disabled' => 0)));

    but the condition have not changed :
    it doing the add save function properly
    but while editing it is not showing the selected options ...as selected ...
  • edited 1:53PM
    Does the following syntax fix your problem:
    public $has_many =array('speaker_id' => array( 'events'=>'speakers_model', 'where' => array('is_disabled' => 0));
  • edited 1:53PM
    hi finally the issue get resolved by removing

    if(isset($record['speaker_id']))
    {
    $record['speaker_id']=(is_array($record['speaker_id']))?implode(',', $record['speaker_id']):$record['speaker_id'];
    }
    from save

    and $values['speaker_id']= explode(',', $values['speaker_id']); from orm_fields function and just by adding the fild to $serialized_fields
Sign In or Register to comment.