This topic is locked
[SOLVED]

 Automatic update of a 'created_by' field.

6/16/2012 4:32:41 AM
PHPRunner General questions
L
lmb_hs_sp author

Hi,
I'm trying to find an elegant solution to the following problem:

I' using phprunner 6.1 and a MySQL database and I am trying to build an interface to the database, security mode is 'Database' and 'Dynamic'.
In the database there are several tables like

'company'

'employee'

'application_user'
Table 'employee':

id_employee

first_name

last_name
Table 'application_user' (where the user_id, and password for the interface are stored):

id_user

user_login

user_password

user_full_name

employee_id (foreign key to the 'employee' table).
Table 'company'

id_company

created_by (foreign key to the 'employee' table)

updated_by (foreign key to the 'employee' table)

company_name

comment
What I would like to do: every time a new company id created, the application

1_Retrieve the id_of the logged in user.

2_Find the employee_id of this logged in user.

3_Automatically update the value 'created_by' with the 'employee_id' of the logged in user every time a new company is added.

4_Automatically update the value 'updated_by' with the 'employee_id' of the logged in user every time a company is updated.
Any bright idea on how best this could be done?
Thanks
Franck

C
cgphp 6/16/2012

In the "After successful login" event save a session var with the value of the employee id:

$_SESSION['employee_id'] = $data['employee_id '];


You can now skip point 1 and 2 and update the fields 'created_by' and 'updated_by' easily.

L
lmb_hs_sp author 6/16/2012



In the "After successful login" event save a session var with the value of the employee id:

$_SESSION['employee_id'] = $data['employee_id '];


You can now skip point 1 and 2 and update the fields 'created_by' and 'updated_by' easily.


Thanks Cristian for the super quick reply.
So I have successfully set the session variable but I'm still struggling on what exactly I should do the update in the table.

On the company_add page I tried to use Events and I have a hunch from the doc that should do something with the DAL but I'm not really sure what to do here.

I realize it should be really easy to do but unfortunately I've not succeeded yet...
Thanks a lot for your help,
Franck

L
lmb_hs_sp author 6/16/2012

OK update on this.
As it turned out I was making things too complex for myself...

The solution I found was to go in the Events/ Before recorded added for the 'company_add' page.
Then I just had to add the following code to the event:

$values['created_by']=$_SESSION['employee_id'];


Et voila!

it worked.
Thanks again to Cristian for pointing in the right direction.
Franck