⚠ 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

Become null when using before insert



alabi
  • profile picture
  • Member

Posted 27 July 2017 - 02:22 AM

Hi,

I found a problem while using before_insert.

Some value in my field become null when insert to database.

 

Here is some code in my controller : 

$crud->add_fields('from_user_id', 'to_user_id', 'message_content', 'message_status');
$crud->callback_before_insert(array($this,'reply_user'));
function reply_user($value){
if(!empty($value['message_content']))
{
	$value['from_user_id']	= $this->session->userdata("user_id");
	
	$message_id = substr( current_url(), strrpos( current_url(), '/' )+1 );
	$this->load->model('message_model');
	$result = $this->message_model->user_get_message($message_id);

	foreach ($result as $message) {
	        $value['to_user_id'] = $message->from_user_id;
        }
       	
	$value['message_status'] = 'Sent';
	return $value;
	}
	else
	{
		echo "false";
	}
}

Here is the code in my model : 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class message_model  extends CI_Model  {

	public function user_get_message($message_id){
        // $this->db->select('from_user_id');
        $this->db->where('message_id', $message_id);
        
        $this->db->from('messages');

        $query = $this->db->get();

        if ($query->num_rows() ==1) {
            foreach($query->result_array() as $row) 
            {
                return $query->result();
            }
        }
        else{
            return false;
        }
    }
}

I already try echo $value['to_user_id']; to see is it null or not.

Well yeah. its not null. I got the value that i want. But when it inserted to database, it become zero value..

 

 

Can anyone help me to solve this problem? Pretty new here.


Amit Shah
  • profile picture
  • Member

Posted 27 July 2017 - 04:50 AM

Well my friend - u did it right .. by working out on the callback..

no doubt bout it..

but 1 more thing u need to do is.. once u done with the populating of the stuff.. if u wana change the values ..u need to return the same back..

meaning u need to do..

return $value;

 

That should solve your problem.

 

Happy GCing :)


alabi
  • profile picture
  • Member

Posted 27 July 2017 - 06:05 AM

Thanks for your respond Mr. Amit.  :)

 

Well, i already make a return $value; in my code. I put it in my controller, right before the end of if condition. And it store to my database finely.

 

But the issue is, some value become zero after insert to database..

 

You can see what i mean in the image i attach. 

[attachment=1228:1501061819980.jpg]

 

 

What's wrong with it then?


Amit Shah
  • profile picture
  • Member

Posted 27 July 2017 - 08:47 AM

can u share the complete function code..

more often like the function and the callback..

i need to check 1 condition before i can comment on something.


alabi
  • profile picture
  • Member

Posted 27 July 2017 - 09:04 AM

Sure. Here is

public function reply()
{
	$crud = new grocery_CRUD();

	$crud->set_table('messages');
	$crud->set_subject('Message');
	$crud->where('from_user_id', $this->session->userdata("user_id"));

	$crud->columns('to_user_id', 'message_content', 'message_status', 'date');
	$crud->unset_fields('to_user_id');
	$crud->add_fields('from_user_id', 'to_user_id', 'message_content', 'message_status');

	$crud->display_as('from_user_id', 'Message From');
	$crud->display_as('to_user_id', 'Sent to');
	$crud->change_field_type('message_status','invisible');

	$crud->set_relation('from_user_id', 'users', '{first_name} {last_name} ({email})');
	$crud->set_relation('to_user_id', 'users', '{first_name} {last_name} ({email})');

	$crud->callback_add_field('from_user_id', function ($value) {
		return $this->session->userdata("email");
	});


	$crud->callback_column('message_content', array($this,'length_message'));
	$crud->callback_before_insert(array($this,'reply_user'));

	$crud->required_fields('message_content');
	$crud->order_by('date','desc');
	$crud->unset_edit();

	$output = $crud->render();
	$this->_example_output($output);
}
function reply_user($value){
if(!empty($value['message_content']))
{
	$value['from_user_id']	= $this->session->userdata("user_id");
	
	$message_id = substr( current_url(), strrpos( current_url(), '/' )+1 );
	$this->load->model('message_model');
	$result = $this->message_model->user_get_message($message_id);

	foreach ($result as $message) {
	        $value['to_user_id'] = $message->from_user_id;
        }
       	
	$value['message_status'] = 'Sent';
	return $value;
	}
	else
	{
		echo "false";
	}
}

Is this all you need?

Thanks for helping me before :)


Amit Shah
  • profile picture
  • Member

Posted 27 July 2017 - 09:44 AM

there...

$crud->unset_fields('to_user_id');

This statement makes the problem..

now i understand u dont wana show the field in the form..

there u can change the type of the field to be hidden and populate it on the before callback...

but when u unset it .. GC library is going to ignore that field totally.

 

Hope this solves your issue..

 

Happy GCing :)


alabi
  • profile picture
  • Member

Posted 27 July 2017 - 09:50 AM

So thats why!

Ok i'll give it a try and tell you is it working!


alabi
  • profile picture
  • Member

Posted 27 July 2017 - 09:59 AM

Unfortunately that doesn't work  :( 

 

I already remove that part and the value in my database still zero

 

Almost depressed here..


Amit Shah
  • profile picture
  • Member

Posted 27 July 2017 - 17:18 PM

public function reply()
{
	$crud = new grocery_CRUD();

	$crud->set_table('messages');
	$crud->set_subject('Message');
	$crud->where('from_user_id', $this->session->userdata("user_id"));

	$crud->columns('to_user_id', 'message_content', 'message_status', 'date');
	$crud->add_fields('from_user_id', 'to_user_id', 'message_content', 'message_status');

	$crud->display_as('from_user_id', 'Message From');
	$crud->display_as('to_user_id', 'Sent to');
	$crud->change_field_type('message_status','hidden');
        $crud->change_field_type('to_user_id','hidden');

	$crud->set_relation('from_user_id', 'users', '{first_name} {last_name} ({email})');
        if($crud->getState() == 'list' || $crud->getState() == 'read')
	         $crud->set_relation('to_user_id', 'users', '{first_name} {last_name} ({email})');

	$crud->callback_add_field('from_user_id', function ($value) {
		return $this->session->userdata("email");
	});


	$crud->callback_column('message_content', array($this,'length_message'));
	$crud->callback_before_insert(array($this,'reply_user'));

	$crud->required_fields('message_content');
	$crud->order_by('date','desc');
	$crud->unset_edit();

	$output = $crud->render();
	$this->_example_output($output);
}

Have done some updation .. try this now

It might be a possiblity of multiple outcomes of the field to_user_id ... 

now we restrict it .. as field for add / edit and other operations..

only when dealing with listing / read - we use set relation..

 

Hope this solves it..

 

Happy GCing :)


alabi
  • profile picture
  • Member

Posted 31 July 2017 - 05:20 AM

It's working! Thanks Bro for helping me out!  :)


Amit Shah
  • profile picture
  • Member

Posted 31 July 2017 - 05:58 AM

Happy 2 help..

 

Happy GCing :)