⚠ 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

How about callback_field for custom fields in read state?



Paul Savostin
  • profile picture
  • Member

Posted 05 May 2014 - 16:10 PM

With version 1.4.1 in read state callback_field works fine but also shows warnings like :
 

A PHP Error was encountered

Severity: Warning

Message: in_array() expects parameter 2 to be array, null given

Filename: libraries/Grocery_CRUD.php

Line Number: 160

That's was just for custom fields that i get with callback_field.

Then i download new version of GC 1.4.2 and warnings has gone, but now in read state that custom fields just empty...

Any suggestions how to get rid of this warnings? (of course i could just turn off warning errors, but i just need to know how GC working in read state)

Thanks:)

 

 


Amit Shah
  • profile picture
  • Member

Posted 06 May 2014 - 05:38 AM

Well the warning is coming probably because the parameter passed in is not as expected. Share you piece of code in order to understand as where is the trouble!


Paul Savostin
  • profile picture
  • Member

Posted 06 May 2014 - 08:00 AM

Hi, thanks for response!

So picture is:

Let's say i have two tables - contractors and objects

Relation:

objects - id, street,...., contractor_id
contractors - id, name and so on.

I want implement adding, editing two table in the same time on one page. For that i get main table "objects" ($crud->set_table("objects")) and for contractors data i get them in callback column, callback fields and so on.

So:

Just for example  we will work with one field from contractors - name

First of all add custom field "contractor_name" to $crud->fields(....,'contractor_name')
Then create callback_field for this field like $crud->callback_field('contractor_name',array($this,'_field_callback_name'))
And for display name - $crud->display_as('contractor_name', lang('name'))

In callback_field i just get value of field name from contractors table like:

public function _field_callback_name($value = null){
        if($obj = $this->session->userdata('contractor')){   // here we get contractor object from session
            $value = $obj->name;
        }
        return '<input type="text" maxlength="50" value="'.$value.'" name="name" style="width:462px">'; //for edit,insert state

       // or

       return $value; // for read state
    }

 

 

I have two different callbacks for read and edit/insert state - i don't need input field in read state

And in controller i do something like this:

if(in_array($state,array('edit','update','read'))){
           
            $primary_key = $state_info->primary_key;
            $cur_contractor = $this->db->get_where('objects',array('id'=>$primary_key))->row()->contractor_id;
            ($obj = $this->db->get_where('contractors',array('id'=>$cur_contractor))->row()) == false ? show_404(): "'';
            
            $this->session->set_userdata('contractor',$obj);

}

 

 

Here i get contractor object from contractors by contractor_id which is in object row and save to the session

Finally in callbacks before insert or update object i get from post array values about contractor fields and save them or update

So all works perfect in edit insert list state, but in read state all values presents but shows warnings that i mention in the begin of this post

What i am doing wrong? Can you help me?

tips and advices will come in handy! Thanks!

P.S. OFFTOPIC: I'm just thinking, what if I parse url string to find read state and return from callback_field in edit/insert - input field and in read just value. Then i no need two callbacks for one field in different states any more. How do you think, it's a good solution? just advise me plz:)


Amit Shah
  • profile picture
  • Member

Posted 07 May 2014 - 05:36 AM

well i dont see it anyways in relation to the issue with callback for custom column. If you notice the code on line 160 - it deals in with issues of required fields. Here what i believe is happening in is - when the function is being called in for - for some reason, by this time, this required_fields object is not yet being initiated and hence it is giving the error. It was supposed to be an array but when the function got called up - it still was null. That is the error. If you want to resolve it - b4, check the flow of the code and b4 it reaches to this function call.. make sure the required field is initialized / set to an array. That way - it wont give u error.


Paul Savostin
  • profile picture
  • Member

Posted 07 May 2014 - 09:15 AM

Hi, thanks for help! So I do not see any error too... I will not initialize the required_fields of GC by myself,

 

this can lead to more worst troubles then warnings...

Well thanks anyway for trying to helping me!


Amit Shah
  • profile picture
  • Member

Posted 07 May 2014 - 15:14 PM

well. technically it should not - i did it myself whn i created custom calls in the library - and never found much problem.. theres 1 single call which initiates all such variables.. u can just make a single call 2 the same or if not.. u can just give an additional check.. if(is_array(....)) to the variable and proceed with the same call.,

 

Wtever is good 4 u to proceed go yr way..

Happy GCing:)


Paul Savostin
  • profile picture
  • Member

Posted 07 May 2014 - 18:49 PM

I make simple check for null in GC before that place where warning was and now everything is OK. Thank you very much,honestly!


Amit Shah
  • profile picture
  • Member

Posted 08 May 2014 - 07:27 AM

Most welcome and - Happy  GCing :)