⚠ 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

Help with set_relation



Jorge Muñoz

Jorge Muñoz
  • profile picture
  • Member

Posted 14 October 2014 - 20:36 PM

Hello..!!

 

As I get data from the relation 1_n in my DB but my FK is not in central table.

I need obtain this values and modify your field because i need set any data.

 

 

Example:

 

tb_person

id

name

size

                                  

tb_team

id

fk_person

name_team        

 

 

When I are add a new person, if this person will be in a new group I need add a new group in a same field not in other page.

Always use set_relation but in this case is not similar the rest.

 

Please I need help, this is my first proyect with codeigniter and grocery_crud.

 

 

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 15 October 2014 - 07:22 AM

Hi there,

 

I understand your situation .. here

but u dont have a readymade solution for the same in GroceryCRUD

u need to alter the solution (custom made) ... ofcourse u can still continue using grocery crud...

but then in the form u need to attach it to javascript which which u can dynamically attach a new field (group name)

 

Step 2 -

add a callback_befoore_insert

check if this new field u planned for - if exists - add the extract the value and add the same to the group table.

and remember to unset the same from the post_array (variable from function) and return the same array back

 

 

This way u can manage to add new group dynamically..

 

Hope this helps u

 

Happy GCing :)


Jorge Muñoz

Jorge Muñoz
  • profile picture
  • Member

Posted 15 October 2014 - 13:52 PM

Hi Amit Shah,

 

Thk for your help.

I have tried it before but my problem is not when data inserted , my data inseerted correctly, my problem is when I have review this data or edit data. because I used the set_relation_n_n when any modificaction for my situation.

 

My real situation now is not example.

 

 tb_sites

id
name
url
image
......

......
fk_category
fk_type
fk_language
fk_owner

 

tb_add_text

id
text
fk_site

 

My insertion is true but my link from the list to edit selected element is not correct, this is the error return.

I know this relationship is not correct but I not know other form to represent.

Error Number: 1066

Not unique table/alias: 'add_texts_site'

SELECT *, text as s1cb251ec FROM (`add_texts_site`) JOIN `add_texts_site` ON `add_texts_site`.`fk_site` = `add_texts_site`.`id` WHERE `fk_site` = '130' ORDER BY `add_texts_site`.`text`

Filename: C:\xampp\htdocs\admin\system\database\DB_driver.php

Line Number: 330

 This is my bad relation

$crud->set_relation_n_n('add_text', 'add_texts_site', 'add_texts_site', 'fk_site', 'fk_site', 'text');

This is my controller

try {
            $crud = new grocery_CRUD();
            $crud->set_theme('flexigrid');
            $crud->set_table('sites');
            $crud->set_subject('Directorio de Sitios');
            $crud->set_language('spanish');
            $crud->unset_export();
          
            $crud->set_field_upload('image', 'assets/uploads/files');

            $crud->columns('name', 'url', 'description', 'fk_type', 'fk_owner', 'active');

            $crud->set_relation('fk_category', 'categories_site', 'category');
            $crud->set_relation('fk_type', 'types_site', 'type');
            $crud->set_relation('fk_language', 'languages', 'language');
            $crud->set_relation('fk_owner', 'owners_site', 'name');

            $crud->set_relation_n_n('tags', 'sites_tags', 'tags_site', 'fk_site', 'fk_tag', 'tag');
            $crud->set_relation_n_n('parent', 'parents_site', 'sites', 'parent_site', 'child_site', 'name');

            /**
             * Relaciones n-1 donde la FK no esta en 
             * la tabla que estamos haciendo render
             */
            $crud->display_as('name', 'Nombre')
                    ->display_as('url', 'URL')
                    ->display_as('description', 'Descripción')
                    ->display_as('fk_type', 'Tipo')
                    ->display_as('fk_owner', 'Propietario')
                    ->display_as('image', 'Imagen')
                    ->display_as('active', 'Estado')                    
                    ->display_as('priority', 'Prioridad')
                    ->display_as('fk_category', 'Categoría')
                    ->display_as('fk_language', 'Idioma')
                    ->display_as('parent', 'Sitios Padre')
                    ->display_as('tags', 'Palabras Claves')
                    ->display_as('add_text', 'Texto Adicional')
                    
     //This is my prblem

        $crud->set_relation_n_n('add_text', 'add_texts_site', 'add_texts_site', 'fk_site', 'fk_site', 'text');

        $crud->callback_add_field('add_text', array($this, 'edit_field_add_text_callback'));

            /**
             * Insertando campos en las tablas "icons_site" y "add_text" 
             */

            $crud->callback_after_insert(array($this, 'after_insert'));

            $crud->unset_back_to_list();
            $output = $crud->render();

            $this->load->view('sites/edit_sites_view', $output);

        } catch (Exception $e) {
            /* Si algo sale mal mostramos error */
            show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
        }

Callback after insert

 function after_insert($post_array, $primary_key) {

     
        //Inserta el texto adicional en la tabla "add_text"
           $insert = array("text" => $post_array['add_text'],
          "fk_site" => $primary_key
          );
          $this->db->insert('add_texts_site', $insert); 
    }

Callback add field

 function edit_field_add_text_callback($value, $primary_key) {
        return '<textarea rows="1" cols="30" name="add_text"></textarea>';
    }

Please excuse my bad English :)