Virtual Attributes and Custom SQL

When generating custom SQL, if you prefix any additional or non-table bound columns with a double-underscore ("__"), the generated object will read in the column as a virtual attribute. You can then use the generated getVirtualAttribute() method to retrieve the value of this data.

In our example here, we create a custom SQL query which uses SQL's COUNT function and subselects to calculate the number of team member for each project.

By utilizing Virtual Attributes, complex queries with calculated values, subselects, etc. can be retrieved in a single database query, and all the values can be stored in the data object, itself.

List All Projects and its Team Member Count

ACME Website Redesign has 5 team members.
State College HR System has 6 team members.
Blueman Industrial Site Architecture has 5 team members.
ACME Payment System has 7 team members.