OK, I think I've sorted this out. At least I've got something that
works even if it's not the prettiest code in the world.

Basically I had to take a step back and let things settle in my mind
before coming back to it a few hours later. After that things started
to fall into place and I could see what I was doing wrong, which was
lots of things.

The painful thing is that I know I could have written a couple of
loops and a few SQL statements and done it a lot quicker than it took
me to figure out how to manipulate CakePHP into doing it. Hopefully,
now I've done this once, it'll become a lot easier.

Cheers,

David

On Jun 23, 3:06 pm, DavidH <[email protected]> wrote:
> 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 manyKpiand aKpibelongs to many dashboards
> AKpihas many Ranges and a Range belongs to aKpi
> 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 theKPIvalue 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 onlyKPIthat 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 thisKPIgoes 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 theKPIas
> 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 thekpion 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 HumidityKPIshould have 4 Ranges in this array. This is only
> working correctly for the lastKPIin 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 valueKPI
>                         )
>
>                     [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