⚠ 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

Pre filled enum value in the eddit form



NicolasB
  • profile picture
  • Member

Posted 29 February 2016 - 15:56 PM

Hi guys!

 

I'm working on an application with Grocery Crud & CodeIgniter, and so far all the docs, tutorials & questions on this forum helped me. But now, i can't figure what to do for my current problem.

 

I've got the following action in my controller : 

 function getClausesRelations($id)
    {
        session_start();
        $_SESSION['first_id'] = $id;
        $crud = new grocery_CRUD();
        
        $crud->set_theme('datatables');
        $crud->set_table('clause_clauses')
        ->set_subject('Clauses Relations')
        ->columns('id', 'name', 'type')
        ->display_as('id', 'Clause Id')
        ->display_as('name', 'Clause Name')
        ->display_as('type', 'Type of Relation');
        
        $crud->set_model('mymodel');
        $crud->fields('name', 'type');
        $crud->required_fields('type');

        
        $crud->callback_edit_field('name', array($this, 'getClauseName'));
        
        $crud->callback_delete(array($this, 'deleteClauseRelations'));

        $crud->basic_model->set_query_str("SELECT C2.id, C2.name, CC.type
                FROM clauses C1, clauses C2, clause_clauses CC
                WHERE C1.id = " . $id . "
                AND C1.id IN (CC.clause_id, CC.related_clause_id)
                AND C2.id IN (CC.clause_id, CC.related_clause_id)
                AND C2.id != C1.id
                ORDER BY C2.id;
        ");

        session_destroy();
        $output = $crud->render();
        $this->output($output);
    }

The fact is that the column/field 'type' is an enum with 2 values (Exclusion/Involvement), and when i want to edit an existing object from my database(Image 1)[spoiler]a934fde910f1ceb30c5a92f861a71d10.png,[/spoiler] the enum field has no default value, and I wish the selected value by default was the value from the dabase (In this Case, Involvement should be the selected default value, and not an empty field)

 

[spoiler]c0ea5e34b5f6678f80f63e90e3116d0e.png[/spoiler] 

I hope you will be able to help me, and sorry for my (bad) english!


Amit Shah
  • profile picture
  • Member

Posted 01 March 2016 - 09:41 AM

well can you possibly attach the project (just having that part is fine)  its interesting to have a look at the same. Just reading the same may not be able to share as how it should work or not.


NicolasB
  • profile picture
  • Member

Posted 01 March 2016 - 10:06 AM

Sure! Got you the methods which are used here.

 

[spoiler]

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Contractbuilder extends CI_Controller{

    /**
     * This method is used to display all the informations of $output
     * */
     
    	function output($output = null)
	{
    
	    $this->load->view('header', array('css_files' => $output->css_files, 'js_files' => $output->js_files));
		$this->load->view('display_informations', array('output' => $output->output));
		$this->load->view('footer');
	}

 
    /**
     * Method only used for getClausesRelations (Update)
     * */
    function getClauseName($value, $primary_key)
    {
     
        $query = $this->db->query("SELECT name FROM clauses WHERE id = ". $primary_key);
        return $query->row()->name;
    }
    
    /**
     * Method only used for getClausesRelations (Delete)
     * */
    function deleteClauseRelations($primary_key)
    {
       return $this->db->query("DELETE FROM clause_clauses WHERE (clause_id = " . $_SESSION['first_id'] . " AND related_clause_id = ". $primary_key . " ) OR 
                                                            (clause_id = " . $primary_key . " AND related_clause_id = " . $_SESSION['first_id'] . " )");
    }
    
    /**
     * Method ony used for getClausesRelations (Add)
     * */
    function addClauseRelation($post_array)
    {
        $query = $this->db->query("SELECT id FROM clauses WHERE name = '" . $post_array['name'] . "'");
        $id = $query->row()->id;
        $this->db->query("INSERT INTO clause_clauses(clause_id, related_clause_id, type)
                        VALUES ( '" . $_SESSION['first_id'] . "', '" . $id . "', '" . $post_array['type'] . "' )" );
    }
    
    function getClausesRelations($id)
    {
        session_start();
        $_SESSION['first_id'] = $id;
        $query = $this->db->query("SELECT c.name
                                FROM clauses c
                                WHERE c.id NOT IN (
                                    SELECT C2.id
                                	FROM clauses C1, clauses C2, clause_clauses CC
                                	WHERE C1.id = " .  $_SESSION['first_id'] . "
                                	AND C1.id IN (CC.clause_id, CC.related_clause_id)
                                	AND C2.id IN (CC.clause_id, CC.related_clause_id)
                                	AND C2.id != C1.id
                                	)
                                AND c.id != " .  $_SESSION['first_id'] . "
                                ;");
        $crud = new grocery_CRUD();
        
        $crud->set_theme('datatables');
        $crud->set_table('clause_clauses')
        ->set_subject('Clauses Relations')
        ->columns('id', 'name', 'type')
        ->display_as('id', 'Clause Id')
        ->display_as('name', 'Clause Name')
        ->display_as('type', 'Type of Relation');
        
        $crud->set_model('mymodel');
        $crud->fields('name', 'type');
        $crud->required_fields('type');
        
        $tmp = array();
        foreach( $query->result() as $row ) 
        {
            array_push($tmp, $row->name);
        }
    

        $crud->field_type('name', 'enum', $tmp);
        $crud->callback_edit_field('name', array($this, 'getClauseName'));

        $crud->callback_insert(array($this, 'addClauseRelation')); 
        
        $crud->callback_delete(array($this, 'deleteClauseRelations'));

        $crud->basic_model->set_query_str("SELECT C2.id, C2.name, CC.type
                FROM clauses C1, clauses C2, clause_clauses CC
                WHERE C1.id = " . $id . "
                AND C1.id IN (CC.clause_id, CC.related_clause_id)
                AND C2.id IN (CC.clause_id, CC.related_clause_id)
                AND C2.id != C1.id
                ORDER BY C2.id;
        ");

        session_destroy();
        $output = $crud->render();
        $this->output($output);
    }

}

[/spoiler]