06.09 php saved
davemo
Tags add more
containable  
Note
A web page can have children which in turn can have children (think 4 levels deep). I want the array to reflect that so I can parse it with a for each loop in order to build my site navigation dynamically. Right now the 1st childwebpage shows up, but any subsequent children do not show up inside the child web page array.
  1. // web_page.php (model)
  2.  
  3. <?php
  4. class WebPage extends AppModel {
  5.  
  6.     var $name = 'WebPage';
  7.     var $actsAs = array('Containable');
  8.  
  9.     var $belongsTo = array(
  10.             'ParentWebPage' => array('className' => 'WebPage',
  11.                                 'foreignKey' => 'parent_web_page_id',
  12.                                 'conditions' => '',
  13.                                 'fields' => '',
  14.                                 'order' => ''
  15.             ),
  16.  
  17. var $hasMany = array(
  18. 'ChildWebPage' => array('className' => 'WebPage',
  19.                                 'foreignKey' => 'parent_web_page_id',
  20.                                 'dependent' => false,
  21.                                 'conditions' => '',
  22.                                 'fields' => '',
  23.                                 'order' => '',
  24.                                 'limit' => '',
  25.                                 'offset' => '',
  26.                                 'exclusive' => '',
  27.                                 'finderQuery' => '',
  28.                                 'counterQuery' => ''
  29.             );
  30.  
  31. // app_controller.php (in my app directory, overriding the cake dir one).
  32.  
  33. function getNavigation() {
  34.  
  35. pr($this->WebPage->find('all', array('contain' => 'ChildWebPage'));
  36.  
  37. }
  38.  
  39. // results of calling getNavigation()
  40.  
  41. (
  42.     [Navigation] => Array
  43.         (
  44.             [0] => Array
  45.                 (
  46.                     [WebPage] => Array
  47.                         (
  48.                             [id] => 1
  49.                             [title] => Gallery
  50.                             [created] => 2008-06-09 11:03:51
  51.                             [modified] => 2008-06-09 11:03:51
  52.                             [content] => gallery content.
  53.                             [parent_web_page_id] => 0
  54.                             [user_id] => 3
  55.                             [web_page_type_id] => 5
  56.                             [team_id] => 1
  57.                             [site_id] => 0
  58.                             [is_active] => 0
  59.                         )
  60.  
  61.                     [ChildWebPage] => Array
  62.                         (
  63.                             [0] => Array
  64.                                 (
  65.                                     [id] => 2
  66.                                     [title] => Home
  67.                                     [created] => 2008-06-09 11:04:32
  68.                                     [modified] => 2008-06-09 11:04:32
  69.                                     [content] => gallery content.
  70.                                     [parent_web_page_id] => 1
  71.                                     [user_id] => 3
  72.                                     [web_page_type_id] => 1
  73.                                     [team_id] => 1
  74.                                     [site_id] => 0
  75.                                     [is_active] => 0
  76.                                 )
  77.  
  78.                         )
  79.  
  80.                 )
  81.  
  82.             [1] => Array
  83.                 (
  84.                     [WebPage] => Array
  85.                         (
  86.                             [id] => 2
  87.                             [title] => Home
  88.                             [created] => 2008-06-09 11:04:32
  89.                             [modified] => 2008-06-09 11:04:32
  90.                             [content] => gallery content.
  91.                             [parent_web_page_id] => 1
  92.                             [user_id] => 3
  93.                             [web_page_type_id] => 1
  94.                             [team_id] => 1
  95.                             [site_id] => 0
  96.                             [is_active] => 0
  97.                         )
  98.  
  99.                     [ChildWebPage] => Array
  100.                         (
  101.                         )
  102.  
  103.                 )
  104.  
  105.             [2] => Array
  106.                 (
  107.                     [WebPage] => Array
  108.                         (
  109.                             [id] => 3
  110.                             [title] => News
  111.                             [created] => 2008-06-09 11:32:45
  112.                             [modified] => 2008-06-09 11:32:45
  113.                             [content] =>
  114.                             [parent_web_page_id] => 0
  115.                             [user_id] => 3
  116.                             [web_page_type_id] => 5
  117.                             [team_id] => 1
  118.                             [site_id] => 1
  119.                             [is_active] => 1
  120.                         )
  121.  
  122.                     [ChildWebPage] => Array
  123.                         (
  124.                             [0] => Array
  125.                                 (
  126.                                     [id] => 4
  127.                                     [title] => Home
  128.                                     [created] => 2008-06-09 11:33:10
  129.                                     [modified] => 2008-06-09 11:33:10
  130.                                     [content] =>
  131.                                     [parent_web_page_id] => 3
  132.                                     [user_id] => 3
  133.                                     [web_page_type_id] => 1
  134.                                     [team_id] => 1
  135.                                     [site_id] => 1
  136.                                     [is_active] => 1
  137.                                 )
  138.  
  139.                         )
  140.  
  141.                 )
  142.  
  143.             [3] => Array
  144.                 (
  145.                     [WebPage] => Array
  146.                         (
  147.                             [id] => 4
  148.                             [title] => Home
  149.                             [created] => 2008-06-09 11:33:10
  150.                             [modified] => 2008-06-09 11:33:10
  151.                             [content] =>
  152.                             [parent_web_page_id] => 3
  153.                             [user_id] => 3
  154.                             [web_page_type_id] => 1
  155.                             [team_id] => 1
  156.                             [site_id] => 1
  157.                             [is_active] => 1
  158.                         )
  159.  
  160.                     [ChildWebPage] => Array
  161.                         (
  162.                             [0] => Array
  163.                                 (
  164.                                     [id] => 5
  165.                                     [title] => Other Information
  166.                                     [created] => 2008-06-09 11:54:29
  167.                                     [modified] => 2008-06-09 11:54:29
  168.                                     [content] => Content
  169.                                     [parent_web_page_id] => 4
  170.                                     [user_id] => 3
  171.                                     [web_page_type_id] => 1
  172.                                     [team_id] => 1
  173.                                     [site_id] => 1
  174.                                     [is_active] => 1
  175.                                 )
  176.  
  177.                         )
  178.  
  179.                 )
  180.  
  181.             [4] => Array
  182.                 (
  183.                     [WebPage] => Array
  184.                         (
  185.                             [id] => 5
  186.                             [title] => Other Information
  187.                             [created] => 2008-06-09 11:54:29
  188.                             [modified] => 2008-06-09 11:54:29
  189.                             [content] => Content
  190.                             [parent_web_page_id] => 4
  191.                             [user_id] => 3
  192.                             [web_page_type_id] => 1
  193.                             [team_id] => 1
  194.                             [site_id] => 1
  195.                             [is_active] => 1
  196.                         )
  197.  
  198.                     [ChildWebPage] => Array
  199.                         (
  200.                         )
  201.  
  202.                 )
  203.  
  204.         )
  205.  
  206. )
Parsed in 0.274 seconds, using GeSHi 1.0.7.14

Modify this Paste