Quick search:

1. Employee base

This application is situated in the example1 folder. To run it on your own server you need to create its table and data by executing the SQL in the file 'examples.sql/example1.sql'. To try it out on the phpPeanuts website click here.

Apart from the modifications for creating an application we only added
a hyperlink to skinSubmenu.php in the application folder for
accessing the list of employees. It links to index.php?pntType=Employee.
 
The domain model of application consist on only one class: Employee. It is situated in the example1 folder in the classes folder. In the class definition you can see Employee extends PntDbObject. We can conclude that instances of Employee are peanuts and that they are persistent.

Notice that the class PntDbObject has been included by a special framework method 'includeClass'. It is important to use this method for including classes, otherwise the framework can not correctly retrieve the class name from an object. See 'How to include a class'.

Explanation of the methods:
  • constructor Employee is only there for future extension.
  • getTableName defines the name of the database table the objects are stored in and retrieved from.
  • initPropertyDescriptors is the place to define properties. Normally some field properties are defined here, whose values can be set by the user and will be stored in/retrieved from the database. But to keep thinks as simple as possible, we used the addDbFieldProps method to add fieldProperties for all columns in the database. See how to add field properties for database columns automaticly.
    Because addDbFieldProps defines the type and length for each field property, the framework will automatically validate settings done by the user. But if properties are defined manually, one can make more settings to get more complete validation. It will also perform better because less databese queries will be necessary. See the same method in example 2 for how to define these properties manually.
  • We also defined the derived property 'name', whose value will be calculated by its getter method getName. Values of this property are not available in a field and are not stored in the database.
  • method getLabelSort defines how a list of employee peanuts should be sorted. With version 1.4.beta1 and up it can be omitted, the objects will then be sorted by the first persistent field property that is not an id property. 
  • method getLabel is a getter method for the inherited derived property 'label' that is used by the framework as the default way to display an employee peanut to the user. With version 1.4.beta1 and up this method can be omitted. The label will then be the values of the properties according to the result of getLabelSort (or its default). The values will be concatenated separated by spaces. In this example that would have put lastName first, by defining getLabel firstName could be put first.

This is all the information the phpPeanuts framework needs to operate an IndexPage that retrieves Employee peanuts and shows them as rows in a table. If you click on a row you get an EditDetailsPage that contains a form in which you can edit the values of the properties of the Employee that are not readOnly.
In the IndexPage you can select some rows with the checkboxes and then press report for a report, or press delete to delete them (on your own web server, on the phpPeanuts website it is blocked).

User Contributed Notes

Henk
2008-12-03 15:01:37
 
An opinion of Ercan posted on this page has been moved to the Forum. Please post notes that are additions to the text above that you think will usefull to other users. Please post questions and opinions on the forum.
Henk
2008-12-03 15:53:46
 
Step by step to make this example yourself after installing phpPeanuts:
1) execute the content of examples.sql/example1.sql on your database
2) follow the above link 'creating an application' and perform the steps described there, using 'example1' as the new folders names.
3) Edit skinSubmenu.php and add a hyperlink to index.php?pntType=Employee
4) Copy classes/pnt/db/classTemplate.php to classes/example1 and rename it to classEmployee.php and open it in an editor
5) replace the PntDbObjectTemplate class name by Employee
6) replace the PntDbObjectTemplate constructor name by Employee
7) in the method getTableName replace 'testdbobjects' by the name of the employees table: 'ex_employees'
8) in the getClassDir method replace 'pnt/db' by the name of the classes folder: 'example1'
9) Save the classEmplpoyee.php file.
10) Try out the application use your internet browser to request index.php from the example1 folder. Click on the link 'Employees' to see the Employees table.
11) To make the Employees table sort properly, in classEmployee.php method getLabelSort replace 'id' by 'lastName'. Copy the line you just adapted to a line below and there replace 'lastName' by 'firstName'. Save the file, then in your browser refresh the Employees table to see the result.
12) to add a derived property 'name' add the following line to the bottom of the method initPropertyDescriptors in in classEmployee.php:
$this->addDerivedProp('name', 'string');
13) in in classEmployee.php add a method getName and make it return the result of concatenating $this->get('firstName') and $this->get('lastName').
15) Save the file, then in your browser refresh the Employees table. You should see a new column 'name' with the name of each employee.