Jump to content


Adding new column

Column grocerycrud

  • Please log in to reply
8 replies to this topic

#1 Piotr Nowak

Piotr Nowak

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 20 February 2014 - 02:21 PM

Hello,

 

I've got a table 'upload'

id tinyint(4)
title varchar(60)
filename varchar(255)
datentime datetime

I want to add extra column called 'filepath' ('filepath' field does not exsist in table) which can display the path of the stored file. I've tried to use callback_column function like that

$crud->columns('id', 'title', 'filename', 'filepath', 'datentime');
$crud->callback_column('filepath', array($this,'givefilepath'));

function givefilepath($value, $row)
{
        return $this->path . '/' . $row->filename;
}

Is it a good idea to use callback_column to return new value to extra column?

Why variable $value is NULL?

Do you have any other idea to solve this problem?



#2 Robert

Robert

    Advanced Member

  • Members
  • PipPipPip
  • 220 posts

Posted 20 February 2014 - 03:14 PM

Create a column in the table filepath, that will fix the error



#3 Piotr Nowak

Piotr Nowak

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 20 February 2014 - 10:23 PM

Create a column in the table filepath, that will fix the error

Ok, but I dont want to create another field ;-)



#4 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 21 February 2014 - 04:31 AM

Well --- if you plan just to use the field to display the value and not store it .. then what u can do is .. use a callback_before_insert  / Update and unset that field from the $post_array and return the same $post_array back from the function .. this should do the trick.. it wont add the new field to the table.



#5 Robert

Robert

    Advanced Member

  • Members
  • PipPipPip
  • 220 posts

Posted 21 February 2014 - 06:55 AM

If you do that you will need to add Amit Shah GC modification because you will have problems if you click on the column to sort it .. I THINK .. 



#6 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 21 February 2014 - 09:35 PM

Well Robert - you should look at here - i already have provided a patch u need to apply for the same issue



#7 Piotr Nowak

Piotr Nowak

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 22 February 2014 - 01:00 AM

Well --- if you plan just to use the field to display the value and not store it .. then what u can do is .. use a callback_before_insert  / Update and unset that field from the $post_array and return the same $post_array back from the function .. this should do the trick.. it wont add the new field to the table.

 

Exactly!

 

Function which I wrote in my first post works good (adds new column and data into it). But it's not a proper way to add new column I think.

 

Well Robert - you should look at here - i already have provided a patch u need to apply for the same issue

 

You mean that if I use 'callback_before_insert' searching and sorting will not work?

 

 

I've tried something like you wrote and it is not working (setting text to the column). The additional column is still empty.

function file()
    {
	$crud = new grocery_CRUD();
	//$crud->set_theme('datatables');
	$crud->set_table('upload');
	$crud->order_by('date');
	$crud->columns('title', 'filename', 'filepath', 'date');
	
	$crud->display_as('title','Tytuł');
	$crud->display_as('filename','Nazwa pliku');
	$crud->display_as('filepath','Ścieżka');
	$crud->display_as('date','Data i czas');
	
	$crud->set_field_upload('filename','assets/uploads/files');
	
	$crud->callback_before_insert(array($this,'filepath_callback'));
	
	$output = $crud->render();
	
	$this->_example_output($output);
    }
    public function _example_output($output = null)
    {
	$this->load->view('example.php', $output);
    }
    function filepath_callback($post_array)
    {
	$post_array['filepath'] = 'What next?';
	
	return $post_array['filepath'];
    }

Have the file been deleted when I pressed "delete row" button? Or should I try 'callback_after_deleteI and implement deleting file myself when the row is deleting



#8 Piotr Nowak

Piotr Nowak

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 24 February 2014 - 05:38 PM

Can anyone help?



#9 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 24 February 2014 - 05:55 PM

No my dear friend,

 

What Robert wanted to convey the stuff was if u add up an additional field in display - it will create issues in search. First of all, that is not the case - u adding up a field and not column. Had it been the column, u could have patched the GC with the function provided back there in the link in above reply and that would have taken care of search (GC tries to query up all the columns provided - it ignores to identify the issue that the column exists or not. Hence the patch is provided there).

 

What you wanted is to provide an extra field in the form but u dont want to store / create the same in database / table. For that the 1 solution is i have worked out in my applications. Hence u can surely go ahead and use the same.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users