PageRenderTime 38ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sgl/assets/_core/php/examples/qcubed_query/association.php

http://logisticsouth.googlecode.com/
PHP | 105 lines | 77 code | 18 blank | 10 comment | 7 complexity | 200bf857e296ff9a97410cfaeadd44f3 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php require_once('../qcubed.inc.php'); ?>
  2. <?php require('../includes/header.inc.php'); ?>
  3. <div class="instructions">
  4. <h1 class="instruction_title">QQ and Association Tables (Many-to-Many Relationships)</h1>
  5. One key feature of <b>QCubed Query</b> is its ability to treat relationships in Association tables just like
  6. any other foreign key relationship. <b>QQ</b> has the ability to perform the full set of <b>QQ</b> functionality
  7. (including conditions, expansions, ordering, grouping, etc.) on tables related via association tables
  8. just as it would on tables related via a direct foreign key.<br/><br/>
  9. Naming standards for the many to many relationship are the same as the naming standards for the public methods
  10. for associating/unassociating in the class, itself. So just as <b>$objPerson->Get<span style="text-decoration: underline;">ProjectAsTeamMember</span>Array</b> will
  11. retrieve an array of Project objects that are associated to this Person object as a "Team Member",
  12. <b>QQN::Person()->ProjectAsTeamMember</b> will refer to the "team_member_project_assn" association table joined against
  13. the "person" table.<br/><br/>
  14. And again, because all the <b>QQ Nodes</b> are linked together, you can go from there to pull the project table, itself, as
  15. well as any columns from that project table. In fact, the linkages can go indefinitely.
  16. <b>QQN::Person()->ProjectAsTeamMember->Project->ManagerPerson->FirstName</b> refers to the "first name of the manager
  17. of any project that this person is a team member of."<br/><br/>
  18. More importantly, when performing <b>QCubed Queries</b> across association tables, we can <b>Expand</b> on the many-to-many
  19. relationship, which would use a special virtual attribute to help describe the individual object, itself, which was involved for the join.
  20. In this case, if we were to do a query of the person table, expanding on any ProjectAsTeamMember objects, the actual project that is joined is available
  21. to the Person object as $objPerson->_ProjectAsTeamMember.<br/><br/>
  22. And finally, on a similar note, you could instead use <b>ExpandAsArray</b> which would do a similar expansion
  23. on the associated object, but store it as an array. See below for the differences of each.
  24. </div>
  25. <h3>Get All People Who Are on a Project Managed by Karen Wolfe (Person ID #7)</h3>
  26. <?php
  27. $objPersonArray = Person::QueryArray(
  28. QQ::Equal(QQN::Person()->ProjectAsTeamMember->Project->ManagerPersonId, 7),
  29. // Because we are doing a join on a many-to-many relationship, we may end up with repeats (e.g. someone
  30. // who is a team member of more than one project that is managed by karen wolfe). Therefore, we declare this as DISTINCT
  31. // to get rid of the redundant entries
  32. QQ::Clause(
  33. QQ::Distinct(),
  34. QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
  35. )
  36. );
  37. foreach ($objPersonArray as $objPerson) {
  38. _p($objPerson->FirstName . ' ' . $objPerson->LastName);
  39. _p('<br/>', false);
  40. }
  41. ?>
  42. <br/>
  43. <h3>Get All People Who Are on a Project Managed by Karen Wolfe (Person ID #7)<br/>showing the Project which is involved in the JOIN via Expand()</h3>
  44. <i>Notice how some people may be listed twice, once for each project which he or she is part of that is managed by Karen Wolfe.</i><br/><br/>
  45. <?php
  46. $objPersonArray = Person::QueryArray(
  47. QQ::Equal(QQN::Person()->ProjectAsTeamMember->Project->ManagerPersonId, 7),
  48. // Let's expand on the Project, itself
  49. QQ::Clause(
  50. QQ::Expand(QQN::Person()->ProjectAsTeamMember->Project),
  51. QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName)
  52. )
  53. );
  54. foreach ($objPersonArray as $objPerson) {
  55. printf('%s %s (via the "%s" project)<br/>',
  56. QApplication::HtmlEntities($objPerson->FirstName),
  57. QApplication::HtmlEntities($objPerson->LastName),
  58. // Use the _ProjectAsTeamMember virtual attribute, which gives us the Project object
  59. QApplication::HtmlEntities($objPerson->_ProjectAsTeamMember->Name));
  60. }
  61. ?>
  62. <br/>
  63. <h3>Same as above, but this time, use ExpandAsArray()</h3>
  64. <i>Notice how each person is only listed once... but each person has an internal/virtual <b>_ProjectAsTeamMemberArray</b> which may list more than one project.</i><br/><br/>
  65. <?php
  66. $objPersonArray = Person::QueryArray(
  67. QQ::Equal(QQN::Person()->ProjectAsTeamMember->Project->ManagerPersonId, 7),
  68. QQ::Clause(
  69. // Let's ExpandArray on the Association Table, itself
  70. QQ::ExpandAsArray(QQN::Person()->ProjectAsTeamMember),
  71. // ExpandArray dictates that the PRIMARY sort MUST be on the root object (in this case, QQN::Person())
  72. // Any secondary sort can follow
  73. QQ::OrderBy(QQN::Person()->LastName, QQN::Person()->FirstName, QQN::Person()->ProjectAsTeamMember->Project->Name)
  74. )
  75. );
  76. foreach ($objPersonArray as $objPerson) {
  77. _p($objPerson->FirstName . ' ' . $objPerson->LastName);
  78. _p('<br/>', false);
  79. // Now, instead of using the _ProjectAsTeamMember virtual attribute, we will use
  80. // the _ProjectAsTeamMemberArray virtual attribute, which gives us an array of Project objects
  81. $strProjectNameArray = array();
  82. foreach ($objPerson->_ProjectAsTeamMemberArray as $objProject)
  83. array_push($strProjectNameArray, QApplication::HtmlEntities($objProject->Name));
  84. printf('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;via: %s<br/>', implode(', ', $strProjectNameArray));
  85. }
  86. ?>
  87. <?php require('../includes/footer.inc.php'); ?>