⚠ 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

result of a query in a field



paz_44

paz_44
  • profile picture
  • Member

Posted 12 August 2013 - 00:04 AM

hello ... I wanted to ask for help.
I have three tables are:
 
model
 
id_model
name_model
 
Trademark
 
id_trademark
name_trademark
 
Product
 
id_product
nameProduct
description
serialnumber
id_trademark
id_model
 
From what I've read, grocery crud does not support this. so I made a query to bring me the product name, model and trademark.
 
and brings me the following result: Notebook wer-210  Lenovo
 
this what I want to save in a table assignment.
 
assignment
 
id_asignacion
id_product
id_user
 
as I can save the result of the query in the field of product?

 

 


davidoster

davidoster
  • profile picture
  • Member

Posted 12 August 2013 - 06:28 AM

Hello and welcome to the forums [member=paz_44].

If I were you I wouldn't use the id_product field from the product table.

I would have another field that would be an array('id_product'=>'description') where description is the combination of the fields you have mentioned.

This can be done by using the field_type function. 

 

Now as a more general comment, you mentioned that this you're trying to do isn't supported by Grocery CRUD library.

Well let me rephrase this a bit and say, since this isn't supported from your tables' structure how can it be supported by GC library or any other library?

Explanation: 

Since you have a table called product I suppose you would want to provide a nice combined user experience where in a single table he enters all the data about a single product including the fields trademark and model.

 

Saying this if you add two more fields on table product as name_model and name_trademark as set_relations then:

- you will not need a custom query to produce the desciption you want, e.g. product name, model and trademark

- on the table assignment you will be able to put a field that is a set_relation('description', 'product', '{name_proruct} {name_model} {name_trademark}');


paz_44

paz_44
  • profile picture
  • Member

Posted 13 August 2013 - 17:21 PM

Hello, thank you very much for answering.
Regarding your response I commented that the reason for having the tables model and trademark is I want to avoid entering the same products not write trademark name differently or have errors in the model.
So I was looking at how to create a new model with the query and to associate it with grocery crud.

 


davidoster

davidoster
  • profile picture
  • Member

Posted 14 August 2013 - 20:49 PM

 

Hello, thank you very much for answering.
Regarding your response I commented that the reason for having the tables model and trademark is I want to avoid entering the same products not write trademark name differently or have errors in the model.
So I was looking at how to create a new model with the query and to associate it with grocery crud.

 

 

 

 

Sorry, I can't understand what you mean here.


chavamm

chavamm
  • profile picture
  • Member

Posted 20 August 2013 - 17:45 PM

Hi paz_44,

 

you could add a new 'full_name' field on Products db table,

then you use the callback functions before Insert and before update for modified the value for this field.

 

Please read the documentation: http://www.grocerycrud.com/documentation/options_functions

 

In the callaback function you could:

$sql = "SELECT * FROM Trademark WHERE id_trademark = ?";
$trd = $this->db->query($sql, array($post['id_trademark']))->row();

$sql = "SELECT * FROM Model WHERE id_model = ?";
$mdl = $this->db->query($sql, array($post['id_model']))->row();

$tradename = $trd->name_trademark;
$modelname = $mdl->name_model;

$post['full_name'] = $post['nameProduct'] . ' ' .$tradename . ' ' . $modelname;

Then, on your controller method for 'assignment' db table,

$crud->set_relation('id_product', 'products', 'full_name');

Is Only an idea,

 

Thanks, regards.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 21 August 2013 - 08:31 AM

Can you be more clear / specific as what is you want .. do you want to display the data from 3 tables in 1 or what exactly you looking into? IF you can explain it better / clearer.. we could help you better


paz_44

paz_44
  • profile picture
  • Member

Posted 15 September 2013 - 05:09 AM

Thanks for your help chavamm. I had not checked if I had answered

 

I tried to resolve it with your idea and I found it.

 

 

put into product
$ crud-> callback_before_insert (array ($ this, 'callback_product'));
 
callback_product function ($ post_array){
$ sql = "SELECT * FROM WHERE trademark idtrademark = product.idtrademark";
   $ trd = $ this-> db-> query ($ sql, array ($ post ['nameTrademark']));
   $ tradename = $ trd-> nameTrademark;
$ post_array ['fullName'] = $ tradename;
 
 
 
return $ post_array;
}

 

 

Amit Shah

 

The product table has the following fields:
 
id_product
name
id_model
id_trademark
 
 
Table assignment, I want to leave the following field:
 
    Product                      | User Name
 
Dell Notebook QWE-40 | Mark
    
 
But in doing so I get this:
 
 
   Product       | User Name
 
Notebook 3 2 | Mark
 
 
that would be the name of the product,the id trademark and the id model.

 

 

Thanks for helping!!!