Jump to content
Larry Ullman's Book Forums

Persistent Objects In Forms


Recommended Posts

I am developing some forms for a content management system and as some of them are becoming very bloated with SQL queries and other code connected with multiple select tags etc I have decided to convert my code to objects.

 

The problem is that I cannot seem to find a way to make the objects persistent when I submit the form.

 

I am quite familiar with C++ so understand OOP but am new to using it with PHP and though I have a couple of Larry's books I do not have this one yet, so would appreciate any tips.

Does the book help at all with this issue?

 

Jon

Link to comment
Share on other sites

If your SQL is bloated, that would probably call for some inner joins and creation of views to make your job easier. The syntax for Inner joins are pretty easy,

 

CREATE VIEW main_view AS

SELECT main.*, (other rows)

FROM main_table as main

INNER JOIN second_tale ON (main.id = second.id)

 

.....

 

When you have written the views you like, SELECT * FROM main_view is enough.

 

You cannot delete/edit or add to every view. It's for display, as the name suggests.

Link to comment
Share on other sites

My SQL is full of inner joins already. That is part of the problem as it was becoming hard to see what was going on!

 

Using a couple of class objects as if they are C type structs has helped to reduce the clutter by encapsulating some of the queries but it would be nice to be able to reduce it even further by avoiding the need to keep instantiating new objects every time a form is submitted by POST, which is quite often.

 

For instance on one form I have four select tags three of which trigger updates of the other selects, so using objects helps to make clear the already quite complex code that handles it.

 

I have tried passing an object by reference in a hidden variable in the form but this does not work.

Having had a look around the web, I suspect that what I need to do is use a php serialize function to turn the object into a string, so that it can be then submitted with POST and then handled in the script by unserializing it.

 

I think this would be a useful technique for anyone to know as it would make very complex forms a lot easier to develop.

 

Does this book mention anything about serializing or does anyone know anything about using it?

 

If your SQL is bloated, that would probably call for some inner joins and creation of views to make your job easier. The syntax for Inner joins are pretty easy,

 

CREATE VIEW main_view AS

SELECT main.*, (other rows)

FROM main_table as main

INNER JOIN second_tale ON (main.id = second.id)

 

.....

 

When you have written the views you like, SELECT * FROM main_view is enough.

 

You cannot delete/edit or add to every view. It's for display, as the name suggests.

Link to comment
Share on other sites

You could serialize the object, but I think it'd be easiest and most foolproof to just store it in a session.

 

Thanks Larry. Thinking about that, it would also avoid the overhead of posting the data back to the client every time the selection changed in a select combo, which could be anything up to 100KB or so every time.

 

I assume I would just store a reference to the object in a session?

 

I have written my own session handler to prevent multiple logins using the same account and plan to use a session timeout by modifying php.ini, so would I be correct in thinking the server would take of deleting the object when the session is over.

 

regards

Jon

Link to comment
Share on other sites

 Share

×
×
  • Create New...