Question

So, I have this piece of code:

class ProductsController extends AppController {
var $name = 'Products';
var $paginate = array('limit' => 5);    // sets the number of entries per page  


function index() {      
    $this->Product->recursive = 0;
    // works up to here fine
    $this->set('products', $this->paginate());  // makes the browser hangs
    }
}

When I go to ../products/index the browser just kind of hangs. It tries to load for minutes without giving any error messages, and I'm forced to restart Apache (XAMPP) or wait out a 60 seconds. The problem seems to be with $this->paginate() and all of the associations with Product controller. It has 2 belongsTo (belongs to smaller tables) and 1 hasOne (has one large table, 20K+ rows).

I added a hasOne relationship to the product model, and this seems to be causing the hanging. When I remove this relationship, the URL works and displays all items. However, this hasOne relationship does not cause any errors when I do for example, ../products/view/1.

Any idea on how to make this manageable?

Was it helpful?

Solution

There are a few things you can look at to start debugging. The first is your logs. Check both SQL and PHP logs for slow queries and timeout errors respectively.

If your Product model has a lot of associations, I suggest using ContainableBehavior. Actually, I suggest always setting $recurisve = -1; on the model and using Containable to fetch associations. It will speed up your app dramatically. Why pull data you don't use in your views?

In your app_model, add the behavior:

class AppModel extends Model {

  var $recursive = -1;

  var $actsAs = array('Containable');
}

Then modify your find queries to grab the associated data that you want:

$this->Product->find('all', array(
  'contain' => array
    'Category',
    'Type'
  )
));

Where Category and Type are associated with Product. This will tell Cake to just pull that associated data.

Then look at your queries. Use DebugKit to help you analyze timing and slow queries.

These are some basic methods for speeding up your finds. There's a ton of information out there for optimizing your CakePHP app, which is what sounds like may be the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top