⚠ 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_before_insert not working for created date



pbcomput

pbcomput
  • profile picture
  • Member

Posted 13 March 2014 - 07:23 AM

Hello,

 

I want to add created date when the record adds to db. For that I used callback_before_insert() but Its not working, Don't know why. Can anyone please check my code and tell me where I am doing wrong?

function newsletter_template ()
	{
		if($this->checkSession())
		{
			$crud = new grocery_CRUD();
				
			$crud->set_table('newsletter_template');
			$crud->set_subject(lang('newsletter_template'));
			$crud->columns('nt_id','nt_name','nt_created_dt');
			$crud->fields('nt_id','nt_name','nt_content','nt_created_dt');
			$crud->required_fields('nt_name','nt_content');
			$crud->unique_fields('nt_name');
			
			$crud->change_field_type('nt_id','invisible');
			$crud->change_field_type('nt_created_dt','invisible');
			
			$crud->display_as('nt_id',lang('nt_id'))
				 ->display_as('nt_name',lang('nt_name'))
				 ->display_as('nt_content',lang('nt_content'))
				 ->display_as('nt_created_dt',lang('nt_created_dt'));
				 
			$crud->callback_before_insert(array($this,'nt_created_dt','created_dt'));
				
			$main_content = 'backoffice/newsletter_template';
			$output  = $crud->render();
			$this->_Crud_output($output,$main_content);
		}
	}
	
	function created_dt($post_array)
	{
	  	$post_array['nt_created_dt'] = date('Y-d-m');
		return $post_array;
	}

Thanks for your help.  :)


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 16 March 2014 - 06:45 AM

hi there - dont change the field type to invisible.. make it hidden and the same procedure will work like a charm


pbcomput

pbcomput
  • profile picture
  • Member

Posted 21 March 2014 - 15:57 PM

Hi Amit,

 

Thanks for your help.

 

I changed type to hidden but still it's not working. Note that, the data type of field is date so I got my mistake and changed date format to 

$post_array['nt_created_dt'] = date('Y-m-d');

I thought it will work now after corrected the date format, but still no use. 

 

can you please check again my code?

 

Thanks.


Alejandro Morales Sanchez

Alejandro Morales Sanchez
  • profile picture
  • Member

Posted 21 March 2014 - 22:08 PM

tengo un problema espero que me puedan ayudar si tengo dos tablas "promociones" y "producto" y creo una relación entre ambas tablas set_ralation para traer datos de la tabla producto "nombre_producto" , e implemento un callback_column para que me muestre una alerta de una promocion que se en cuentra activa o inactiva.
 
el problema en el que me encuentro es que cuando yo omito el callback_column el set_ralation me funciona perfectamente pero no me muestra la alerta y cuando omito el set_ralation si me muestra la alerta pero no me muestra la relación entre las tablas.
 
load->helper(‘url’);
 
$this->load->helper(‘date’);
 
/* Inicializamos la base de datos */
$this->load->database();
 
/* Cargamos la libreria groceru_crud */
$this->load->library(‘grocery_crud’);
 
/* Obtenemos la fecha actual */
$timestamp = now();
$timezone = ‘UM8′;
$daylight_saving = FALSE;
 
$now = gmt_to_local($timestamp, $timezone, $daylight_saving);
$datestring = “%Y-%m-%d %h:%i:%s”;
 
$this->now = mdate($datestring, $now);
}
 
public function index()
{
/* Redirigimos a la funcion promociones() */
redirect(‘admin/promociones’);
/*redirect(‘admin/respuestas’);*/
}
 
public function promociones()
{
try{
$crud = new grocery_CRUD();
 
$crud->set_theme(‘datatables’);
$crud->set_table(‘promociones’);
 
$crud->columns(
‘nombre_promocion’,
‘mensaje_promocion’,
‘fecha_inicio’,
‘fecha_vencimiento’,
‘nombre_producto’,
 
‘estatus’
);
 
$crud->display_as(‘numprod’,'producto nombre_producto’)
->display_as(‘fecha_vencimiento’)
->display_as(‘fecha_inicio’)
->display_as(‘estatus’);
$crud->set_subject(‘promociones’);
 
/* Establecemos español como el lenguaje predeterminado */
$crud->set_language(‘spanish’);
 
//$crud->set_relation(‘numalar’,'promociones’,'nombre_promocion’,'mensaje_promocion’);
$crud->set_relation(‘numprod’,'producto’,'nombre_producto’);
 
/* Añadimos una funcionalidad extra a las columnas */
$crud->callback_column(‘estatus’,array($this,’_GC_Estatus’));
 
//$crud->columns(‘nombre_producto’,'descripcion_producto’,'nombre_promocion’,'mensaje_promocion’,'fecha_inicio’);
/* $crud->required_fields(
‘nombre_promocion’,
‘mensaje_promocion’,
‘fecha_inicio’,
‘fecha_vencimiento’,
‘nombre_producto’,
‘descripcion_producto’
);*/
 
$output = $crud->render();
 
$this->load->view(‘admin/promociones’, $output);
 
}
catch(Exception $e){
show_error($e->getMessage().’ — ‘.$e->getTraceAsString());
}
}
 
public function _GC_Estatus($value, $row) {
 
/* Si la fecha actual es mayor o igual a la del inicio de la promocion y es menor
* a la de la fecha de vencimiento, la promocion esta activa.
*/
 
$fechaVencimiento = ($row->fecha_vencimiento);
$arrStr = explode(“-”, $fechaVencimiento);
$arrStr = explode(“/”, $arrStr[0] );
$fechaVencimiento = $arrStr[1] . “/” . $arrStr[ 0 ] . “/” . $arrStr[2];
$fechaVencimiento = strtotime( $fechaVencimiento );
 
$fechaInicio = ($row->fecha_inicio);
$arrStr = explode(“-”, $fechaInicio);
$arrStr = explode(“/”, $arrStr[0] );
$fechaInicio = $arrStr[1] . “/” . $arrStr[ 0 ] . “/” . $arrStr[2];
$fechaInicio = strtotime( $fechaInicio );
 
if( $fechaVencimiento > time() && $fechaInicio < time())
 
{
return 'Activa’;
 
} else {
 
return ‘Inactiva’;
}
}
 
}