Defining Static column that doesn't break add/edit

QuestionsDefining Static column that doesn't break add/edit
Mark asked 5 years ago

Hi Abu,

I’m struggling with what I think is a simple problem but I can’t seem to solve it.

I want to include a column on my grid that is not a database column but that I can edit and does not break your insert.
Eg.
I have a table (mytable) with 2 fields. F1 & F2
I want my grid to display columns F1, F2 and F3 where F3 is a non-database column, editable and which I will use in an insert event to modify one of the table fields.

If I define the grid select as “select *, “ ” as F3 from mytable” the grid displays fine but when I try to insert a record it fails with “Couldn’t execute query. Unknown column ‘F3’ in ‘field list’”

I know I can work around this by handling the insert myself in the insert event but before I do I thought I’d ask you first if there was a simpler way

2 Answers
Abu Ghufran Staff answered 5 years ago

For non-db editable column, you need to use custom on_insert event code as grid library does not understand that it does not exist in table.

When using on_insert, There are 2 options.

$e[“on_insert”] = array(“add_client”, null, true); …. OR

$e[“on_insert”] = array(“add_client”, null, false);

When you set 3rd param to true, both your code and grid code will be executed and in later case only your custom code will be executed with no grid based insert query.

So, what you can do is to make 3rd param true and process your custom column f3, then unset it and leave other insert to be performed by grid.

function add_client($data)
{

// perform insert call on f3 and to avoid insert error unset it.
unset($data[“params”][“f3”]);

}

More here: https://www.phpgrid.org/docs/grid-events/

_________________________
Abu Ghufran - Dev Team
Grid 4 PHP Framework
 
Mark Moran answered 5 years ago

Great solution Abu,

Thanks

 

Your Answer

14 + 13 =

Login with your Social Id:

OR, enter

Attach code here and paste link in question.
Attach screenshot here and paste link in question.



How useful was this discussion?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate it.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?