title: Using CakePHP Components within Components via App::import
body: <p>I'm using CakePHP to build an application which, among other things, generates dozens of graphs.  In order to make loading these graphs easy, I'm following a factory pattern using components.  There's one ChartComponent which is responsible for loading components to generate the individual graphs, and these individual graphs in turn may make use of components to prepare their data.</p>
  
  <p>Sample GraphComponent:</p>
  
  <pre><code>class ChartComponent extends Object {
      var $name = 'Chart';
  
      function getChart($category, $chart, $node) {
          // Build the name we expect to find the component as.
          $component_name = Inflector::camelize("{$category}_{$chart}_graph");
          $component_class = "{$component_name}Component";
  
          // Import the component, making sure it actually exists.
          if (! App::import('Component', $component_name)) {
              return false;
          }
  
          $chart = new $component_class();
          return $chart-&gt;getData($node);
      }
  }
  </code></pre>
  
  <p>Sample component for individual graph:</p>
  
  <pre><code>class StoreRevenueGraphComponent extends Object {
      var $name = 'StoreRevenueGraph';
      var $components = array('Calculations');
  
      function getData($node) {
          var_dump(isset($this-&gt;Calculations));
      }
  }
  </code></pre>
  
  <p>However, when I run this code, the Calcluations component does not successfully load, and isset($this->Calculations) returns false.</p>
  
  <p>I assume this is because I am missing out on some initialization code somewhere.  Does anyone know what other steps I must take to use components inside another component via App::import, or is what I am attempting to do not possible inside cake?</p>
  
  <p><strong>Solution</strong></p>
  
  <p>As Andrew pointed out, when manually instantiating components the var $components array is never processed, causing them to never load.  The solution is to manually do this like so:</p>
  
  <pre><code>function startup(&amp;$controller) {
      foreach ($this-&gt;components as $component_name) {
          App::import('Component', $component_name);
          $component_class = "{$component_name}Component";
          $this-&gt;$component_name = new $component_class();
          $this-&gt;$component_name-&gt;startup($this);
      }
  }
  </code></pre>