⚠ 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 Drop Down Issue



fieldju
  • profile picture
  • Member

Posted 31 May 2012 - 08:11 AM

Summary:
When the set_relation function is used to reference a table such as my BinaryLookup table, and the edit page is accessed only values that where >0 are showed with their set values.

for example if I had a table such as this


[font=Arial, sans-serif][size=2]"students"[/size][/font]
[font=Arial, sans-serif][size=2]+------------------------------------------+[/size][/font]
[font=Arial, sans-serif][size=2]| id | student | enrollmentStatus |
| 0 | Justin | 1 |
| 1 | Billy | 0 |[/size][/font]
[font=Arial, sans-serif][size=2]+------------------------------------------+[/size][/font]

[font=Arial, sans-serif][size=2]"BinaryLookup"[/size][/font]
[font=Arial, sans-serif][size=2]+-----------------------------------+[/size][/font]
[font=Arial, sans-serif][size=2]| id | EnrolledPreenrolled |[/size][/font]
[font=Arial, sans-serif][size=2]| 0 | Pre-Enrolled |[/size][/font]
[font=Arial, sans-serif][size=2]| 1 | Enrolled |[/size][/font]
[font=Arial, sans-serif][size=2]+-----------------------------------+[/size][/font]

and I used set_relation('enrollmentStatus','BinaryLookup','EnrolledPreenrolled');

Justin's status would show "Enrolled" on the edit page, but Billy's status would say "select enrollmentStatus" and have a drop down of the status's

Here are some screen shots of the issue.

What you initially see

What the drop down looks like

What the BinaryLookup Table looks like



// set up relationships
->set_relation('ClassID', 'Classroom', 'ClassName', array('Enabled' => '1'))
->set_relation('ProgramID', 'Program', '{AcademicYear} {Title}', array('Enabled' => '1'))
->set_relation('IsEnrolled','BinaryLookup','EnrolledPreenrolled')


It should have had Pre-enrolled selected, and in all cases where the id is >0 the proper result is seen.

Platforms:
Chrome, Firefox, and Internet Explorer

Versions
Codeignighter: 2.1.0
AG_Auth: 2.0
Fancybox: 2.0.6
Grocery Crude: 1.2.3


How to reproduce:
1. Create a CRUD with a 1-n relation using the set_relation(‘exampleColumn’,’lookupTable’,’NewValue’) function
2. In the main table you are using for the CRUD edit the exampleColumn value to be ‘0’
3. Observe the value of the entry for the exmapleColumn upon entering edit mode and you will see that rather than being the value of the data in the lookupTable, it will say “select exampleColumn”

fieldju
  • profile picture
  • Member

Posted 04 June 2012 - 06:18 AM

I updated this in an attempt to make the issue more clear

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 09 June 2012 - 10:27 AM

Hello [member='fieldju'] well the problem is that grocery CRUD handles the 0 as a NULL variable at the set_relation. I will explain the reasons why:
1. If the field for the relation is not required and you have added for example a save. Then by default (if you don't have setted to NULL) the mySQL add the 0 value at this field
2. I already have lot of IF ELSE with scenarios such as "is it empty? check if we can add it to NULL" , "is it empty then add an empty string" , "is it empty AND allow NULLS, then add it to NULL" for example code such this:

if( isset($types[$field->field_name]->db_null) && $types[$field->field_name]->db_null && $post_data[$field->field_name] === '' )
and it's a bit complicated for me to add another scenario for the 0 also.
3. It is really rare to use 0 as a primary key and it is not a good SQL technique because you will probably have some problems in the future with the default 0 value or the 0 id value.

I can understand the problem and I will look at it and I will find some time. For now you can just change the id 0 to something else :)