⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

set_relation when used with change_field_type readonly displays ID rather than related value



fieldju
  • profile picture
  • Member

Posted 04 June 2012 - 08:01 AM

[color=#282828][font=helvetica, arial, sans-serif][b]Summary[/b]:[/font][/color]
When the set_relation function is used with the change_field_type function and the readonly type is selected the CRUD will display the id rather than the related value.

[b]Example[/b]:
using the following tables
[color=#282828][font=Arial, sans-serif][size=2]"students"[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]+------------------------------------------+[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]| student_id | studentName | enrollmentStatus |
| 0 | Justin | 1 |
| 1 | Billy | 0 |[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]+------------------------------------------+[/size][/font][/color]

[color=#282828][font=Arial, sans-serif][size=2]"classGrades"[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]+-----------------------------------+[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]| class_id | grade | student_id |[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]| biology | A | 0 |[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]| english101 | B | 1 |[/size][/font][/color]
[color=#282828][font=Arial, sans-serif][size=2]+-----------------------------------+[/size][/font][/color]


function viewGrades() {
$crud = new grocery_CRUD();
$crud->set_table('classGrades')
->set_relation('student_id','students','studentName')
->change_field_type('student_id','readonly');

$output = $crud->render();

$this->load->view('templates/grid', $output);
}


then the CRUD produced will show the studentID instead of the studentName

[b]Workaround[/b]:
This issue can be worked around through the use of the callback_edit_field function as I did below

function viewGrades() {
$crud = new grocery_CRUD();
$crud->set_table('classGrades');

$crud->callback_edit_field('student_id', array($this, 'getNameFromStudentID'));

$output = $crud->render();

$this->load->view('templates/grid', $output);
}


with this callback function (please note that we used active record and your call back might be different depending on how you interact with your database and how your models are set up here is our model if you want to see it)


function getNameFromStudentID($value, $row){
$name = Student::find_by_studentid($value);
$nameAttr = $name->attributes();
$string = $nameAttr['firstname'] . ' ' . $nameAttr['lastname'];
return $string;
}


this will display the students name rather than the ID
we also included this in our css to vertically center the names


<style type="text/css">
.flexigrid div.form-div input[type=text], .flexigrid div.form-div input[type=password]{
padding: 0 5px !important;
}
#StudentID_input_box, #UserID_input_box{ margin-top: 10px !important; }
.required{
color: #EE0000;
font-weight: bold;
font-size: 18px;
}
</style>


where it says StudentID_input_box must be [the name of your column]_input_box


[color=#282828][font=helvetica, arial, sans-serif][b]Platforms[/b]:[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]Chrome, Firefox, and Internet Explorer[/font][/color]

[b][color=#282828][font=helvetica, arial, sans-serif]Software Versions:[/font][/color][/b]
[color=#282828][font=helvetica, arial, sans-serif]Codeignighter: 2.1.0[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]AG_Auth: 2.0[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]Fancybox: 2.0.6[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]Grocery Crud: 1.2.3[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]php-activerecord: 2.0[/font][/color]


[color=#282828][font=helvetica, arial, sans-serif][b]How to reproduce[/b]:[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]1. Create a CRUD that uses the set_relation() function[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]2. Set the related column to read only via the change_field_type function.[/font][/color]
[color=#282828][font=helvetica, arial, sans-serif]3. Observe that the CRUD displays the ID rather than the related data [/font][/color]

twister88888
  • profile picture
  • Member

Posted 10 July 2012 - 08:41 AM

Gracias por la aportación!!

deib97
  • profile picture
  • Member

Posted 21 May 2013 - 04:07 AM

Thanks bro..


deib97
  • profile picture
  • Member

Posted 21 May 2013 - 05:03 AM

any example.. ?