Hi Again

I'm really getting stuck with my accessing the related data I need
though my controller and into my view. To top it all I seem to have
broken my controller by including a $uses statement to pull in the
other models I want to access.

Here's my model structure. Three models in the following relationship:

Dashboard has many Kpi and a Kpi belongs to many dashboards
A Kpi has many Ranges and a Range belongs to a Kpi
A Range has one Colour and a Colour has many Ranges

I'm pretty confident that all the relationships are set correctly in
the models as I had a set of basic controllers / views working OK
before.

Now I've created a new 'showdashboard' method in my Dashboards
controller along with a showdashboard.ctp view. The purpose of this is
to display all the KPIs on a dashboard coloured according to which
range the KPI value falls into.

>From my (still limited) understanding of Cake I had thought that by
doing this:

$this->Dashboard->recursive = 2;
$dashboard = $this->Dashboard->find('first',
            array('conditions' => array('user_id' => $my_user,
'default' => 1)));

I would be able to access the entire hierachy from dashboard->range-
>colour however I couldn't get this to work. In the end I put in:

var $uses = array('Dashboard', 'Range', 'Colour');

at the top of the controller and this allowed me to do things like:

$colours = $this->Colour->find('all');

So my question is: should I have been able to get at the Range and
Colour data without needing the $uses statement because these are
related to the Dashboard? If this is the correct way of doing things
could someone please post an example because I've totally got me head
in a knot over this.

Using $uses as above is only partially working in my showdashboards
method. Even though the dashboard is being passed to the view, and
this contains each of the 4 KPIs, the only KPI that has its Range data
is the last one in the array.

Here's my showdashboards method in full:

function showdashboard()
    {
        //
        // Get the user ID from the session
        //
        $my_user = $this->Session->read('Auth.User.id');

        //
        // Read the default dashboard for this user
        //
        $this->Dashboard->recursive = 2;
        $dashboard = $this->Dashboard->find('first',
            array('conditions' => array('user_id' => $my_user,
'default' => 1)));
        $kpi_ranges = $this->Range->find('all');
        debug($dashboard);

        //
        // Fetch all the colours back
        //
        $kpi_colours = $this->Colour->find('all');
        // debug($kpi_colours);

        //
        // Logic for deciding the staus of this KPI goes into the
controller here
        //
        $kpi_index = 0;
        foreach ($dashboard['Kpi'] as $kpi)
        {
            $kpi_value = $kpi['kpi_value'];
            debug($kpi);
            //
            // Loop through all the levels testing where the current
value
            // fits into the range.
            //
            $kpi_colour = "";
            foreach($kpi['Range'] as $level)
            {

                debug($level);
                if ($kpi_value >= $level['low'] && $kpi_value < $level
['high'])
                {
                    $kpi_colour = $kpi_colours[$level['kpi_colour_id']]
['Colour']['hexcode'];
                }
            }

            //
            // Check we found a valid level otherwise mark the KPI as
out of range
            //
            if (strcmp($kpi_colour, "") == 0)
            {
                $dashboard['Kpi'][$kpi_index]['kpi_value'] = 'Out Of
Range';
                $dashboard['Kpi'][$kpi_index]['kpi_colour'] =
"000000";

            }
            else
            {
                //
                // Set the corect colour for the kpi on the dashboard
                //
                $dashboard['Kpi'][$kpi_index]['kpi_colour'] =
$kpi_colour;
            }
            $kpi_index++;
        }

        //
        // Set up for the view
        //
        $this->set('dashboard', $dashboard);
    }

This is the top of my $dashboards array from showdashboards.ctp. The
Relative Humidity KPI should have 4 Ranges in this array. This is only
working correctly for the last KPI in the array:

Array
(
    [Dashboard] => Array
        (
            [id] => 3
            [dashname] => David's Dashboard
            [user_id] => 4
            [default] => 1
        )

    [User] => Array
        (
            [id] => 4
            [password] => 8273c2212439e8fdfdf1db521b899d88638dff9c
            [userlevel_id] => 2
            [username] => David
            [Userlevel] => Array
                (
                    [id] => 2
                    [levelname] => General Staff
                )

            [Dashboard] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [dashname] => David's Dashboard
                            [user_id] => 4
                            [default] => 1
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [dashname] => Dashboard No. 2
                            [user_id] => 4
                            [default] => 0
                        )

                )

        )

    [Kpi] => Array
        (
            [0] => Array
                (
                    [id] => 0
                    [title] => Relative Humidity
                    [kpi_type_id] => 1
                    [kpi_value] => Out Of Range
                    [kpi_shortcode] => RHPCT
                    [KpiType] => Array
                        (
                            [id] => 1
                            [name] => Single value KPI
                        )

                    [KpiValue] => Array
                        (
                        )

                    [Range] => Array        ####### This should not be
empty
                        (
                        )

                    [Chart] => Array
                        (
                        )

                    [kpi_colour] => 000000
                )

I'm hoping someone can put me back on the right track on this as it's
really blocking my development.

Cheers

David
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to