Querying the Database Returns the Entire Table

I am using cakephp version 4. I have a page with tiles and the tiles are hidden from view until a user clicks on a button with the showMe id. The problem is that my controller returns all of the tiles from the database table even when they’re set to the style of display none. Because of the large query to the database, my page sometimes times out and returns an error page saying that maximum execution time had been reached. How can I restrict my controller from querying all of the fields in the database table when the tile is hidden from view? I can’t use the limit clause because the tiles have to be ready to be displayed. This is what I have in my controller:

$this->loadModel('Tiles');
$this->set('tiles', $this->Tiles->find('all'));

You’re probably best off applying some kind of AJAX solution. For instance, go the the front page of this discord forum and scroll down and see items load on demand.

Thanks for the advice, Jawfin

It’s only fetching 15 records of about 15 words each so it doesn’t seem like a lot. Do you think the query is the reason for the slow page loads or do you think it could be something else?

Do a var_dump / debug to see what’s actually being brought through. You can also check your client developer console, Network, Document, Header to see if it’s content encoding is gzip so the data is at least compressed.

There is an insane amount of data attached to CakePHP objects, so it may be smarter to just set that which you actually need - perhaps putting it into an array in your controller and passing only that.

Just because the php variable is available in your template doesn’t necessarily mean it’s going to the browser client (or does it, someone who knows the internals of CakePHP may correct me here) - so perhaps also do a console.log of any data you’ve put into javascript. In the end though all you need do is Ctrl + U and see the webpage its generated, and just see how big it is (which will include any primed javascript variables).

As mentioned by others in your previous questions about timeouts, it could just be unoptimised code in your controllers. For comparison, my CakePHP app can load 11,300 records of a table of 16 columns containing long strings, dates etc in 3 and a half seconds (I have FTTP internet downloading at 55Mbps).

Edit: correction on that timeout. 3.5 seconds is how long it takes for the browser client to AJAX request my web server which is being long-polled by my actual PC here, which gets the request, packages the data, sends it up to the webserver, then back down to the client as the returned AJAX - so the true time for my 11,300 records is most likely under a second.

Your query is saying find('all'), but you’re surprised that it’s returning all the rows? If you want to add a condition to what rows are returned, then you need to add a condition.

No, not surprised, I just haven’t thought about which conditions I want to apply yet.

Thanks, Jawfin for the extensive help with this.