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 9087139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:38:48+00:00 2026-06-16T21:38:48+00:00

Hello and happy holidays everyone. Recently I have been tasked with transforming a beta

  • 0

Hello and happy holidays everyone.

Recently I have been tasked with transforming a beta application from pure PHP/jQuery to CakePHP/ExtJS (Which I am new to).

My issue is with the most complex query that populates the main grid.

To keep things simple there are 3 tables with correct baked relationships and models: Projects, ToDo, ExtraToDo

Projects hasMany ToDo and ExtraToDo.

ToDo and ExtraToDo have columns Title and Complete.

My goal is to get a completion percent for each project based on these three tables.
The way I have gone about this is the SUM of the Complete column divided by the COUNT of the Complete column. I am trying in a CakePHP way for readability/performance/otherstuffIdontknowyet.

Originally, in raw SQL I had done it like this:

SELECT
  `idProject`,

  (SELECT
    ROUND((SUM(`Complete`) / COUNT(`Complete`)) * 100),
   FROM
    (SELECT `Complete`, `ProjectID` FROM `ToDo`
      UNION ALL
     SELECT `Complete`, `ProjectID` FROM `ExtraToDo`) orders
   WHERE
    `ProjectID` = `idProject`
  ) AS 'Completion'

FROM 
  `Projects`

I also got this to work in the Kohana PHP MVC framework fairly easily which I tried before deciding on CakePHP. I LOOOVED how their queries were created…:

private function get_completion() {
  $Query = DB::select('ProjectID', array(DB::expr('ROUND((SUM(`Complete`) / COUNT(`Complete`)) * 100)'), 'Completion'))
    ->from(array('ToDo', 'ExtraToDo'))
    ->group_by('ProjectID');

  return $Query;
}

public function get_all() {
  $Query = DB::select()
    ->from('Projects')
    ->join(array(self::get_completion(), 'Completion'))
    ->on('projects.id', '=', 'Completion.ProjectID')
    ->execute()
    ->as_array();

  return $Query;      
}

Unfortunately I have completely struggled to get this working in CakePHP while doing it the CakePHP way.

I’m pretty sure virtualFields are the key to my answer but after reading the documents and trying x, y, AND z. I have been unable to comprehend them and how they relate.

Thank you in advance
-T6

  • 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-06-16T21:38:49+00:00Added an answer on June 16, 2026 at 9:38 pm

    That is a lot of nested selects. IMO you would be better off building a better query.

    This should get you going.

    class Project extends AppModel {
        public $findMethods = array(
            'completion' => true
        );
    
        // other code
    
        protected function _findCompletion($state, $query, $results = array()) {
            if ($state == 'before') {
                $this->virtualFields['total'] = 'ROUND((SUM(Todo.Complete + TodoExtra.Complete) / (COUNT(Todo.Complete) + COUNT(TodoExtra.Complete))) * 100)';
    
                $query['fields'] = array(
                    $this->alias . '.' . $this->primaryKey,
                    'total'
                );
    
                $query['joins'] = array(
                    array(  
                        'table' => 'todos', 
                        'alias' => 'Todo', 
                        'type' => 'left', 
                        'foreignKey' => false, 
                        'conditions'=> array('Todo.project_id = ' , $this->alias . '.' . $this->primaryKey) 
                    ),
                    array(  
                        'table' => 'todo_extras', 
                        'alias' => 'TodoExtra', 
                        'type' => 'left', 
                        'foreignKey' => false, 
                        'conditions'=> array('TodoExtra.project_id = ' . $this->alias . '.' . $this->primaryKey) 
                    ),
                );
    
                $query['group'] = array(
                    $this->alias . '.' . $this->primaryKey
                );
                return $query;
            }
    
            return $results;
        }
    
        // other code
    }
    

    Now you have a custom find method that can be used like find('first') or find('all').

    From the controller:

    $this->Project->find('completion');
    

    Or in the Project model

    $this->find('completion');
    

    It should return something like this:

    $results = array(
        0 => array(
            'Project' => array(
                'id' => 1,
                'total' => 50
            )
        ),
        1 => array(
            'Project' => array(
                'id' => 2,
                'total' => 75
            )
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i'm doing a application in php, and i have a list of items
Hello all and a Happy New Year SITUATION: I have some tables in MySQL
Hello everyone (this is my first post) I have some simple AD code that
I have got html as below <a href= data-href=/somefile.php?id=10&refcode=/test.php>Hello</a> and I need to replace
Hello all happy holidays! :) I'm trying to insert comments in my wordpress blog
Hello I have a Session ID variable I'm passing through from on Oracle package
Let's say I have: <input type=text id=first attribute1=test attribute2=hello attribute3=happy> <input type=text id=first attribute1=test
I have a ProgressDialog that retrieves in background data from database by executing php
As the title suggests, I have copied verbatim the hello.cl and hello.c files from
Hello! I've been programming for a long time but just started developing for android,

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.