⚠ 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

set_relation breaks processing of field with same name returned



ahutt

ahutt
  • profile picture
  • Member

Posted 27 March 2012 - 17:30 PM

Hello,

I've encountered some odd behavior with set_relation:
A very simple scenario illustrates the problem. The table defs are below.

The main table I want to display is a table of items. Since the description is so long
I want to display only the first 40 caracters. So I created a callback :

$this->grocery_crud->callback_column('description',array($this,'item_description_callback'));

function item_description_callback($value, $row) {
return substr($value,0,40);
}

This works as expected. But I further want to display the name of the category that
is associated with the integer category number stored in this table (in field category) This category name is
in the table Category. So I created a set_relation:

$this->grocery_crud->set_relation('category','category','description');

This works as expected for retreiving the description field from the category file
but BREAKS the substr display of the description field in items processed by the callback above !

I think it is odd and undesireable that the set relation function doesn't allow one to
specify which field in the related table (category) to use as the key. Even if the field
happens to be the same name as the source field I wouldn't assume it is the key to be used --
and especially if the field name is different as in this case ie category --> category_id.
Is there NOT a way to explicitly indicate which fields define the relation ??

This lack of explicitly defining the keys may or may not be the cause of this problem
but I thought I'd mention it.

Is there a work around for 1)this problem and 2) this observation re callback syntax??

Thanks! Great product so far !!
alvin


mysql> desc items;
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| item_id | int(11) | NO | PRI | NULL | auto_increment |
| description | varchar(200) | YES | | NULL | |
| seller_id | int(11) | YES | | NULL | |
| category | int(11) | YES | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+


+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| category_id | int(11) | NO | PRI | NULL | auto_increment |
| description | varchar(200) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------

bl4z

bl4z
  • profile picture
  • Member

Posted 29 March 2012 - 10:18 AM

i also had similar problem and the thing is that internaly name of this fields are encoded // so i just use this function (copied form grocery model)


function unique_field_name($field_name) {
return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
}


ant then


$crud->callback_column($this->unique_field_name('vrsta_id'),array($this,'valueToEuro'));
$crud->set_relation('vrsta_id','vrsta','{vrsta_id}');


and works fine // i know its not optimal but it works :)

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 29 March 2012 - 19:35 PM

I didn't know this fix ;) . Clever.
Though I will try to fix this thing with the normal way for the next version.

Thanks.

rteranm

rteranm
  • profile picture
  • Member

Posted 24 July 2012 - 00:46 AM

I have that problem in my application, thanks you bl4z

thanks you
[b] web-johnny[/b]

Robert

Robert
  • profile picture
  • Member

Posted 10 October 2013 - 06:17 AM

Any date on when the fix will be added ?


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 10 October 2013 - 12:04 PM

Well robert - u can add it to the base downloaded gc library of yours and copy and use it elsewhere :)


Robert

Robert
  • profile picture
  • Member

Posted 15 October 2013 - 06:21 AM

Sorry I didn't get what you are trying to say o.0

 

I want to have this :

 

$crud->set_relation('adf_id','users','id');
$crud->change_field_type('adf_id', 'hidden', $user->id);
 

how can i use bl4z temp fix ?


edramirez

edramirez
  • profile picture
  • Member

Posted 10 November 2013 - 15:05 PM

I think the lesson we could learn here is to plan our tables and fieldnames so that there is no conflict. Tables that validate a foreign key should not have any common fieldname as the table it validates other than the foreign key field itself.

 

Grocerycrud has a bunch of  demands that developers have to look out for in the design of tables. It helps to know what these are.

 

Regards,

 

Ed Ramirez

 

P.S. Don't forget to hit the Like button...  :)


Robert

Robert
  • profile picture
  • Member

Posted 12 November 2013 - 06:51 AM

Try that but i cant make it work like that .. Since this was no problem in older versions i was thinking that maiby it can be fix.


edramirez

edramirez
  • profile picture
  • Member

Posted 12 November 2013 - 07:59 AM

Just change the fieldnames of the tables.

 

Let's say you have a table called items:

 

items table

itemcode varchar(10) - pk

description varchar (40)

itemtype varchar(10)

 

As you see the items table requires an itemtype which is a foreign key to items type in another table

 

Here's a wrong way to construct the item type table because you are using description field again:

 

itype table

itemtype varchar(10)

description varchar(40)

 

Here's a better way:

 

itype table

itemtype varchar(10)

typedescription varchar(40)

 

You need to name fields differently when working with grocerycrud so that you would have no problems with set_relation.

 

As I mentioned earlier, Grocerycrud has a bunch of  demands that developers have to look out for in the design of tables. This is one of those demands. If you follow this rule, your code will work instantly. If you have name conflicts, you will have to create workarounds that may somehow cause problems later on. I'm sure we all want to make our work as simple as possible, considering how complex our work already is.

 

Regards,

 

Ed


Robert

Robert
  • profile picture
  • Member

Posted 20 November 2013 - 13:59 PM

Just change the fieldnames of the tables.

 

Let's say you have a table called items:

 

items table

itemcode varchar(10) - pk

description varchar (40)

itemtype varchar(10)

 

As you see the items table requires an itemtype which is a foreign key to items type in another table

 

Here's a wrong way to construct the item type table because you are using description field again:

 

itype table

itemtype varchar(10)

description varchar(40)

 

Here's a better way:

 

itype table

itemtype varchar(10)

typedescription varchar(40)

 

You need to name fields differently when working with grocerycrud so that you would have no problems with set_relation.

 

As I mentioned earlier, Grocerycrud has a bunch of  demands that developers have to look out for in the design of tables. This is one of those demands. If you follow this rule, your code will work instantly. If you have name conflicts, you will have to create workarounds that may somehow cause problems later on. I'm sure we all want to make our work as simple as possible, considering how complex our work already is.

 

Regards,

 

Ed

That still dose not fix the problem .. since i need relation and change_field_type on the same field.

So i want to use $crud->change_field_type('adf_id', 'hidden', $user->id); to auto add the user id when they add a new record and then use $crud->set_relation('adf_id','users','username'); to see the name of the user that added the record not there id's ...


mau

mau
  • profile picture
  • Member

Posted 12 December 2013 - 12:23 PM

Hi to all

 

I'm not sure I've the same problem but when I try to use the method  $crud->set_relation('id_gest','cszA_anagest','nome'); it generates the error: Call to a member function result() on a non-object in /var/www/CI/application/models/grocery_crud_model.php on line 87.

I don't understand why and how I used in the wrong way the method if I followed the documentation.

If I understand well using this method it's the same to do a 'SELECT nome FROM cszA_anagest WHERE cszA_anagest.id_gest=tab_cszA_alias.id_gest and, in the output of table tab_cszA_alias it will uses field 'nome' in place of 'id_gest'.

Sorry for the elementary question but I'm new on Grocery CRUD.

Any answer will be appreciated.

Best regards.

Mau


mau

mau
  • profile picture
  • Member

Posted 13 December 2013 - 07:45 AM

Hello

 

I verified the incompatibility between $crud->set_relation method and $crud->where method.

If they have to work togheter in the same function like the following:

public function mostra_alias()
    {
        $session_tipo = $this->session->userdata('menuTipo');
        if($this->_session_is_ok($session_tipo))
        {
            $session_nome = $this->session->userdata('nomeutente');
            $this->_connselDB($session_tipo);
            $crud = new grocery_CRUD();

            $crud->where('tab_cszA_alias.cod_fisc',$session_nome);
            $crud->set_table('tab_cszA_alias');
            $crud->display_as('id_gest','Gestore');
            $crud->display_as('cod_fisc','Codice fiscale');
            $crud->set_subject('Cliente');
            $crud->set_relation('tab_cszA_alias.id_gest','cszA_anagest','cszA_anagest.nome');
            $crud->unset_add();
            $crud->unset_delete();
// aggiungere saldo
            $crud->columns('id_gest','cod_alias','cod_fisc','saldo');
            $output = $crud->render();
            $this->_example_output($output);
        }
    }

they don't work.

Someone could explain me why?

Thanks in advance for your attention.

 

Mau

 

SOLVED: moved $crud->set_table() before  $crud->where() and $crud->set_relation()


juliette

juliette
  • profile picture
  • Member

Posted 08 January 2014 - 08:56 AM

Hello,

 

i also had similar problem and the thing is that internaly name of this fields are encoded // so i just use this function (copied form grocery model)
 

    function unique_field_name($field_name) {
	    return 's'.substr(md5($field_name),0,8); //This s is because is better for a string to begin with a letter and not with a number
    }
ant then

		    $crud->callback_column($this->unique_field_name('vrsta_id'),array($this,'valueToEuro'));
		    $crud->set_relation('vrsta_id','vrsta','{vrsta_id}');
and works fine // i know its not optimal but it works :)

 

 

This works fine for callback_column, but not for callback_field ... Any idea?

 

UPDATE :

Moreover, it breaks the search function: when using the callback_column with $this->unique_field_name and filtering the list, the change_list() function encounters problem here :

if( $has_callbacks && isset($this->callback_column[$field_name]) )
	$list[$num_row]->$field_name = call_user_func($this->callback_column[$field_name], $field_value, $row);
// => echo $list[$num_row]->$field_name fails

I didn't find how to solve it ..

 

Juliette