Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:58:27+00:00 2026-05-23T02:58:27+00:00

I’m using CakePHP to build an application which, among other things, generates dozens of

  • 0

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.

Sample GraphComponent:

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->getData($node);
    }
}

Sample component for individual graph:

class StoreRevenueGraphComponent extends Object {
    var $name = 'StoreRevenueGraph';
    var $components = array('Calculations');

    function getData($node) {
        var_dump(isset($this->Calculations));
    }
}

However, when I run this code, the Calcluations component does not successfully load, and isset($this->Calculations) returns false.

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?

Solution

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:

function startup(&$controller) {
    foreach ($this->components as $component_name) {
        App::import('Component', $component_name);
        $component_class = "{$component_name}Component";
        $this->$component_name = new $component_class();
        $this->$component_name->startup($this);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:58:28+00:00Added an answer on May 23, 2026 at 2:58 am

    The $components property can only be used in controllers. If you want one component to load multiple other components you can override the startup method in your component and create the component and manually assign it to the ->Calculations property.

    Like this:

    function startup( $controller ) {
      $this->Calculations = new Foo();
      $this->Calculations->startup($component);
    }
    

    However, this is a bit of an odd thing to do. What is more likely in most apps is that you want to load it into the controller itself. So the code becomes:

    $component->Calculations = new Foo();
    $component->Calculations->startup($component);
    

    If the component is not ever used directly by the controller it may be better to put the classes in the vendors directory and use them as external libraries.

    [edit]

    See comments after this answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In order to apply a triggered animation to all ToolTip s in my app,
I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.