⚠ 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

Problem with set_relation_n_n and read_only fields



sparklet

sparklet
  • profile picture
  • Member

Posted 17 May 2015 - 22:43 PM

Hi all,

 

I would like to connect two tables by n:n relation - for example "students" should be able to select "courses".

On that form the students shouldn't be able to change their own name or class.

 

My first approach to this was:

public function choose()
    {
        $crud = new grocery_CRUD();
        $crud->set_subject('Choices');
        $crud->set_table('students');
        $crud->set_relation_n_n('SelectedCourses','selected','courses','st_ID','co_ID','CourseName');
        $crud->columns('Name','First Name','Class','SelectedCourses');
        $crud->fields('Name','First Name','Class','SelectedCourses');
        $crud->field_type('Name','readonly');
        $crud->field_type('First Name','readonly');
        $crud->field_type('Class','readonly');
        $crud->callback_column('SelectedCourses', array($this, '_line_break'));
        $crud->unset_add();
        $crud->unset_read();
        $crud->unset_delete();
        $output = $crud->render();
        
        $this->_example_output($output);        
    }

function _line_break($value, $row)
    {
        return $value = str_replace(',',',<br>',$row->SelectedCourses);
    }    

Everything looks fine, but I get an error when trying to save the record.

The error only occurs when all fields are set to "read_only" - possibly because there is nothing to save in the "students" table if everything is read_only.

But on the other hand that is exactly what I want - there souldn't be made any changes to the "students" table, only to the relation table (called "selected" in my example).

 

 

As a workaround, I had to add an extra field to the "students" table and make it hidden (I called it "Dummy") - see lines 8 and 12.

 public function choose()
    {
        $crud = new grocery_CRUD();
        $crud->set_subject('Choices');
        $crud->set_table('students');
        $crud->set_relation_n_n('SelectedCourses','selected','courses','st_ID','co_ID','CourseName');
        $crud->columns('Name','First Name','Class','SelectedCourses');
        $crud->fields('Name','First Name','Class','SelectedCourses','Dummy');
        $crud->field_type('Name','readonly');
        $crud->field_type('First Name','readonly');
        $crud->field_type('Class','readonly');
        $crud->field_type('Dummy','hidden');
        $crud->callback_column('SelectedCourses', array($this, '_line_break'));
        $crud->unset_add();
        $crud->unset_read();
        $crud->unset_delete();
        $output = $crud->render();
        
        $this->_example_output($output);        
    }

function _line_break($value, $row)
    {
        return $value = str_replace(',',',<br>',$row->SelectedCourses);
    }    

So is this a bug or did I have the wrong approach?

 

Thanks in advance :)

David

 

 

PS: 

Oh by the way there is another issue I stumbled over ...

 

In application/config/grocery_crud.php I set the uk-date format 

$config['grocery_crud_date_format']      'uk-date';

 

The date format shows fine in the table itself, but not when I use it as a choice in set_relation_n_n:   (there it's shown in yyyy-mm-dd instead)

$crud->set_relation_n_n('SelectedCourses','selected','courses','st_ID','co_ID','{CourseName} - {CourseTime}');