⚠ 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

double relation problem



oVERDRIVE
  • profile picture
  • Member

Posted 14 February 2013 - 19:55 PM

Hi, this is my first post, here, and I want to greet everyone ...

 

I'm a codeigniter coder, and use grocery crud recently, so, my question maybe trivial, but I tried searching the answer in the forums but I wasn't luky.

 

I have 3 tables in my db, named brand, model and vehicle.

every vehicle has an id, a model_id, and one description

every model has an id, one id_brand, and a description too

and finally a brand, that has an id and a description.

 

I'd like to know, what is the best way to show in a table, the complete details of each vehicle

 

brand description - model description - vehicle description

 

Best regards

 

F


davidoster
  • profile picture
  • Member

Posted 14 February 2013 - 21:13 PM

I believe this is probably more appropriate for your case:

http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n


oVERDRIVE
  • profile picture
  • Member

Posted 14 February 2013 - 21:27 PM

no, I think that this solution not works, because the relation table must contain 2 references ...

(I say heresy???)

 

 

http://www.asciiflow.com/#Draw7788045165846672860

 

I was thinking of using the set_method function ...


davidoster
  • profile picture
  • Member

Posted 14 February 2013 - 23:07 PM

Your tables' structure if it is translated to grocery crud becomes like this:

 

$crud->set_table('brand');

$crud->set_relation_n_n('models', 'model', 'vehicle', 'id_brand', 'model_id', 'vehicle.description');

For this to work you need to make a new field within brand called models.

For guidance use the examples database of GC lib.

 

But if we consider your verbal description

[every vehicle has an id, a model_id, and one description

every model has an id, one id_brand, and a description too

and finally a brand, that has an id and a description.]

it becomes like this:

$crud->set_table('vehicle');

 

$crud->set_relation_n_n('models''model''brand''id_brand', [this should be 'vehicle.id']'vehicle.description');

but this last set_relation is totally wrong sing there isn't a connection brand and vehicle!!!


oVERDRIVE
  • profile picture
  • Member

Posted 15 February 2013 - 09:38 AM

 yes U R right, but if I create a new field within brand tables, the logical structure become wrong...

For this reason, I thought that the better way to show the brand description is to create a module
, so I create a join to the tables, and after show the table ...

My idea, was like this:

 

class My_BrandModelVehicle extends grocery_CRUD_Model  {
 
    function createJoin($tableOne, $tableTwo, $tableOneID, $tableTwoID){
        $this->db->select('*');
        $this->db->from($tableOne);
        $this->db->join($tableOne, $tableOne . '.' . StableOneID . '=' . $tableTwo . '.' . $tableTwoID);

        $data = $this->db->get();
 
       if($data->num_rows() > 1)
          return $data->result();
       else
         return FALSE;

    } // end createJoin function

}// End My_BrandModelVehicle
 

 


        

So I can use my new model in the controller
 

 

function viewVehicles()
{    
    $crud = new grocery_CRUD();
    $crud->set_model('My_BrandModelVehicle');
    ...
    ...
 

 




you think it's possible?


by the way, what examples you refear, when U tell me "database of GC lib" ?

 

PS: what's the way, to make a code in different color ?? :(

Thanks a lot for your attention


davidoster
  • profile picture
  • Member

Posted 15 February 2013 - 11:55 AM

I still don't understand what you are trying to achieve.

But this code about the extended model, My_BrandModelVehicle is certainly feasible/possible!

The examples database when you download the grocery crud, which contains the SQL for creating the tables (and inserts data) used on the example of the actors/films.

 

 

P.S. The colors are inherited if you copy with colors from your text/code editor.


Pascal
  • profile picture
  • Member

Posted 20 February 2013 - 23:41 PM

Hey oVERDRIVE,

 

Does your class work? I'm need the same feature. I the following model (see below) and I need see
category.name + subCategory.name + type.name when I ADD or Edit Product.

 

products                    categories               subCategories            types

========                 =========             ============         ======

productId                   categoryId               subCategoryId           typeId

name                          subCategoryId        typeId                         name

                                   name                       name