⚠ 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

Callback update



outes
  • profile picture
  • Member

Posted 15 November 2012 - 19:23 PM

Hey!

I tried write a callback functions, and it's work, but functions only effect on callback fields, the other "normal" field doesn't updated.

I don't understand the problem. Here's the code:


public function exhibitions()
{
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('content');
$crud->where('catId = 11');

$crud->set_model('Grocery_crud_plus_model');

$crud->fields('title','text', 'date', 'view', 'fromDate', 'toDate');
$crud->columns('title','text', 'date', 'view', 'fromDate', 'toDate');

$crud->callback_field('fromDate',array($this,'from_call'));
$crud->callback_field('toDate',array($this,'to_call'));
$crud->callback_update(array($this,'update_call'));
$crud->callback_insert(array($this,'insert_call'));

$crud->required_fields('title', 'text', 'fromDate', 'toDate');
$crud->unset_edit_fields('date');
$output = $crud->render();

$this->output($output);
}

/**
* fromDate callback
*/
function from_call($value = '', $primary_key)
{
$query = $this->db->query("SELECT fromDate FROM exhtime WHERE contentId = '$primary_key'");
$value = $query->row('fromDate');

if($query->num_rows() == 0)
{
$value = '';
}
return '<div class="form-input-box" id="date_input_box"><input id="field-fromDate" name="fromDate" type="text" value="'.$value.'" maxlength="17" class="datetime-input" />
<a class="datetime-input-clear" tabindex="-1">Ürít</a>
(yyyy-mm-dd) hh:mm:ss</div>
<div class="clear"></div>';
}

/**
* toDate callback
*/
function to_call($value = '', $primary_key)
{
$query = $this->db->query("SELECT toDate FROM exhtime WHERE contentId = '$primary_key'");
$value = $query->row('toDate');

if($query->num_rows() == 0)
{
$value = '';
}
return '<input id="field-toDate" name="toDate" type="text" value="'.$value.'" maxlength="17" class="datetime-input" />
<a class="datetime-input-clear" tabindex="-1">Ürít</a>
(yyyy-mm-dd) hh:mm:ss</div>
<div class="clear"></div>';
}

/**
* to-fromDate update callback
*/
function update_call($post_array, $primary_key)
{
if(!empty($post_array['fromDate']) && !empty($post_array['toDate']))
{
$data = array(
'fromDate' => $post_array['fromDate'],
'toDate' => $post_array['toDate']
);
return $this->db->update('exhtime', $data, array('contentId' => $primary_key));
}
}

victor
  • profile picture
  • Member

Posted 15 November 2012 - 20:16 PM

HI!
in your case you have only two fields for updating:
$data = array(
'fromDate' => $post_array['fromDate'],
'toDate' => $post_array['toDate']
);


you can change your code:



function update_call($post_array, $primary_key)
{
if(empty($post_array['fromDate']) || empty($post_array['toDate']))
{
unset($post_array);
}
return $this->db->update('exhtime', $post_array, array('contentId' => $primary_key));
}

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

outes
  • profile picture
  • Member

Posted 16 November 2012 - 12:35 PM

Hello!

Thanks for your reply.

The problem is that at the same time I would like to run two updates for two db table. The non callback fields, and the two callback fields update. It's possible?

Thanks

victor
  • profile picture
  • Member

Posted 16 November 2012 - 12:40 PM

You can use http://www.grocerycrud.com/documentation/options_functions/callback_after_update

outes
  • profile picture
  • Member

Posted 16 November 2012 - 16:52 PM

Hi!

I tried the callback_after_update function but doesn't work with callbacks field, only works it without field. But then send false/empty data to db.

What am I doing wrong?

victor
  • profile picture
  • Member

Posted 16 November 2012 - 16:56 PM

Show your controller, please.

outes
  • profile picture
  • Member

Posted 16 November 2012 - 17:20 PM

Here is:


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

class Admin extends CI_Controller {

function __construct()
{
parent::__construct();
$this->load->library('grocery_CRUD');
$this->load->model('login_model');
$this->output->enable_profiler(TRUE);
$this->is_logged_in();
}

public function index()
{
$output = (object)array('output' => '' , 'js_files' => array() , 'css_files' => array());
$this->load->view('admin/templates/header', $output);
$this->load->view('admin/templates/nav');
$this->load->view('admin/templates/welcome');
$this->load->view('admin/index');
$this->load->view('admin/templates/footer');
}

/**
* Login status
*/
public function is_logged_in()
{
...
}

/**
* Articles
*/
public function articles()
{
...
}

/**
* Exhibitions
*/
public function exhibitions()
{
$crud = new grocery_CRUD();
$crud->set_language("hungarian");
$crud->set_theme('datatables');
$crud->set_table('content');
$crud->where('catId = 11');
$crud->set_model('Grocery_crud_plus_model');
$crud->fields('title','text', 'date', 'view', 'fromDate', 'toDate');
$crud->columns('title','text', 'date', 'view', 'fromDate', 'toDate');
$crud->callback_field('fromDate', array($this, 'from_call'));
$crud->callback_field('toDate', array($this, 'to_call'));

$crud->display_as('title', 'Kiállítás neve');
$crud->display_as('text', 'Szöveg');
$crud->display_as('date', 'Létrehozás dátuma');
$crud->display_as('view', 'Státusz');
$crud->display_as('fromDate', 'Kezdete');
$crud->display_as('toDate', 'Vége');
$crud->required_fields('title', 'text', 'fromDate', 'toDate');
$crud->unset_edit_fields('date');

//$crud->callback_after_insert(array($this,'insert_call'));
$crud->callback_after_update(array($this,'update_call'));
$output = $crud->render();

$this->output($output);
}
/**
* fromDate callback
*/
function from_call($value = '', $primary_key)
{
$query = $this->db->query("SELECT fromDate FROM exhtime WHERE contentId = '$primary_key'");
$value = $query->row('fromDate');

if($query->num_rows() == 0)
{
$value = '';
}
return '<div class="form-input-box" id="date_input_box"><input id="field-fromDate" name="fromDate" type="text" value="'.$value.'" maxlength="17" class="datetime-input" />
<a class="datetime-input-clear" tabindex="-1">Ürít</a>
(yyyy-mm-dd) hh:mm:ss</div>
<div class="clear"></div>';
}
/**
* toDate callback
*/
function to_call($value = '', $primary_key)
{
$query = $this->db->query("SELECT toDate FROM exhtime WHERE contentId = '$primary_key'");
$value = $query->row('toDate');

if($query->num_rows() == 0)
{
$value = '';
}
return '<input id="field-toDate" name="toDate" type="text" value="'.$value.'" maxlength="17" class="datetime-input" />
<a class="datetime-input-clear" tabindex="-1">Ürít</a>
(yyyy-mm-dd) hh:mm:ss</div>
<div class="clear"></div>';
}
/**
* to-fromDate update callback
*/
function update_call($post_array, $primary_key)
{
$update_data = array(
'fromDate' => $post_array['fromDate'],
'toDate' => $post_array['toDate']
);
$this->db->update('exhtime', $update_data, array('contentId' => $primary_key));
return true;
}

/**
* to-fromDate instert callback
* TODO
*/
function insert_call($post_array, $primary_key)
{
$query = $this->db->query("SELECT id FROM exhtime WHERE contentId = '$primary_key'");
$value = $query->row('toDate');

if($query->num_rows() == 0)
{
$value = '';
}


$data = array(
'fromDate' => $post_array['fromDate'],
'toDate' => $post_array['toDate'],
'contentId' => $primary_key
);
echo '<script>alert("'.$primary_key.'");</script>';
return $this->db->insert('exhtime',$data);
}

/**
* Artists
* TODO
*/
public function artists()
{
...
}

function output($output = null)

{
$this->load->view('admin/templates/header', $output);
$this->load->view('admin/templates/nav');
$this->load->view('admin/index');
$this->load->view('admin/templates/footer');
}
}


and thank you for helping!

victor
  • profile picture
  • Member

Posted 16 November 2012 - 17:51 PM

what result do you have here

echo '<script>alert("'.$primary_key.'");</script>';

outes
  • profile picture
  • Member

Posted 16 November 2012 - 19:01 PM

Insert method doesn't work, it's just a plan... But in update method doesn't show popup. Didn't execute the callback_after_update function?

victor
  • profile picture
  • Member

Posted 16 November 2012 - 19:04 PM

send me database dump i try make a test your code.

victor
  • profile picture
  • Member

Posted 16 November 2012 - 21:12 PM

you have many mistakes in your code and table structure.
Tomorrow I'll help you, if I'll have some time.

outes
  • profile picture
  • Member

Posted 16 November 2012 - 22:48 PM

Opps:(

Okay, thanks so much.

outes
  • profile picture
  • Member

Posted 26 November 2012 - 17:39 PM

Hello:)

So, i try to fix erros and i concluded that some weird problem in the code. First, when I add callback field doesn't work, except that I add in the $crud->fields too. The second problem is very similar to previous one, because callback_after_update function neither work.

Callback_update is work, and I try call_user_func wich is also work.

Any idea?

Thanks