⚠ 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

Adding new column



Piotr Nowak
  • profile picture
  • Member

Posted 20 February 2014 - 14: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?


Robert
  • profile picture
  • Member

Posted 20 February 2014 - 15:14 PM

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


Piotr Nowak
  • profile picture
  • Member

Posted 20 February 2014 - 22:23 PM

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

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


Amit Shah
  • profile picture
  • Member

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.


Robert
  • profile picture
  • Member

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 .. 


Amit Shah
  • profile picture
  • Member

Posted 21 February 2014 - 21:35 PM

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


Piotr Nowak
  • profile picture
  • Member

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


Piotr Nowak
  • profile picture
  • Member

Posted 24 February 2014 - 17:38 PM

Can anyone help?


Amit Shah
  • profile picture
  • Member

Posted 24 February 2014 - 17: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.


rkrajnik
  • profile picture
  • Member

Posted 02 November 2020 - 22:45 PM

But, grocery CRUD Maser, dosen't that break upgrades to GCE?  If so, this is just  one-off use, not very approachable for longterm projects w/GCE.