Does CakePHP need a model to do pagination? - cakephp

I have a few Controllers that work with content from Json files. I was wondering if Cakephp could help me paginate them.
I like Cakephp's pagination much. It seems an extreme lot of work of writing pagination yourself and Cake's pagination does everything I'd want for my tabular data from MySQL.
This however leaves another source of content, json-files, that are not paginated on the site. What would be the best solution for this?

I would repurpose ArraySource in the community-maintained Datasources plugin so it reads from JSON files. It already has support for pagination built in [here and here], so you should only need to add in a connect() method that performs the json_decode() operation. This answer provides usage instructions.

Related

Approach for building a Gallery of images (ParentalKey vs StreamField)

I'm trying to decide between using ParentalKey or StreamField, in this case, with the purpose of adding an image gallery to a page.
We don't need any other information than the image itself (given that the image will be anyway a wagtailimages.Image model's instance, so the alt text is already handled there).
Any thoughts on what is better to make it easier for the editor to work even if maintaining around 50 images per page?
And about good practices and code maintainability?
Would you prefer a third party package for the image gallery? (even if that blocks you from upgrading to wagtail 4?)
Would your opinion change if instead of a single image, we needed some more fields?
Many thanks!
For an image gallery, the first recommendation would be to use the Collections feature. You can get pretty far with the nested collection system and even add extra meta data if needed by adding a model that relates to the collection.
If that is not suitable, ParentalKey/InlinePanel would be my next pick. For simple relationships you get all the benefits of StreamField such as re-ordering, add/remove items but with solid database integrity and usage stats working out of the box.
Only go to StreamField if you need to have optional data set against each image. For example if you have an image list but images could be an Image with a RichText OR just an image.
Unfortunately, managing large sets of images is not great (outside of collections) so you may find you need to build a seperate UI for this. If that ends up being the case you will find migration of data already in model relations being easier to do or maybe not even needed with something like ModelAdmin.
Hope it goes well, be sure to write a blog post about what you end up doing.
I would use the ParentalKey with InlinePanel for that. It shows you all the images as a list in a more compact way than the StreamField. One can reorder this list.
A StreamField is more expandable in the future. You could add new blocks, like videos or quotes or whatever at any point. If you define each block as StructBlock, you will be able to add whatever you want in the future to these blocks without loosing existing data (also true for the ParentalKey model).
I would not use Collections for image slideshows as you won’t be able to sort the imagas in a collection via the CMS, right? Collections are meant to keep order in the backend, I think.

Cache and pagination with CakePHP 3.x

Here is how to cache results with pagination with CakePHP 2.x.
I have not tried it yet, but I believe that the same method is valid with CakePHP 3.x.
However, since CakePHP 3.x introduces several new features for using the cache (for example, the find() method takes the cache option), I wanted to know if there is a faster method.
I have seen book and APIs, but is not mentioned anything special in this regard.
One thing: here is explained that
By default the paginate() method will use the default model for a
controller. You can also pass the resulting query of a find method:
Maybe is this? I use the find() method with the cache option and then I pass the results to paginate()?.
Thanks.
I would not avise towards caching queries, specially not the results of a pagination. But you idea is in theory right:
$aKeyThatEncodesQueryStringAndConditions = ...;
$this->paginate($this->Users->find()->cache($aKeyThatEncodesQueryStringAndConditions));

create.js, createPHP and Cakephp

We are trying to implement in page editing for our cake app. We would like to use create.js for the frontend and createphp to handle the connection between create.js and cakephp. I have been doing a lot of research on RDFa and I am generally baffled by how all this links together.
What I have:
Editable interface
Endpoints via actions in cakephp
What I need:
A way to convert the data sent by create.js to my database structure and a way to send data to create.js for rendering.
I have gotten to the point in createphp where you are supposed to create your own mapper. I don't know what the mapper should contain. It mentions that is has built-in mappers (Midgard\CreatePHP\Mapper ?) but I don't know how to load those either.
I have read the documentation but it doesn't give details on how to accomplish these tasks.
Thank you for your help on the two following questions.
1. How can I convert my data from create.js to cake and then back again for the views? (possible solution createPHP but doesn't have to be)
2. How do I create a mapper for createPHP or where could I find information to learn how to create a mapper for my instance?
Yeah, the documentation is unfortunately rather sparse. I will create an issue on createphp linking to this post, to give some hints how the documentation could be improved.
I try to explain how things work:
To convert the data from the REST call to your model, you indeed need a RdfMapper instance. See the setup section of the tutorial how you bootstrap that. the bundle comes with mappers for doctrine, which you can read for inspiration if you do not use doctrine. I recommend to extend AbstractRdfMapper in that case.
To render the rdfa, you need to configure what fields of your class should be what rdf type. You can either use the array mapper as in the tutorial, or use the xml mapping, or your own RdfDriver
The whole process is working fine in the symfony2 CreateBundle.

Backbone.js - Using a real-time (socket.io) data source as a dynamic model

I have zero experience with Backbone.js currently, and before I start having a good look, I wonder if anybody could advise if it is a good fit for my use case.
I have a dashboard where I will present multiple real-time graphs, with the data source being provided a socket.io.
I would like to use the same data source on multiple pages of the dashboard.
Would backbones's models be a good fit here i.e. setup a model that uses the socket.io data source, and then makes it available to all views?
Basically whenever socket.on is called, i need an object in each of the views to be updated.
Is this possible?
Any thoughts would be much appreciated.
Best regards, Ben.
Yeah, that's possible. There's a library for Backbone that replaces the Backbone.sync method with an implementation that works over Socket.IO:
https://github.com/logicalparadox/backbone.iobind

CakePhp - how does it combine files for result

I've seen the diagrams, but I want a description of how it all works -- for example -- cakephp uses the controller file and the view file. What happens? Is there anything out there? It would make using cakephp's mvc easier.
most simple request would look something like the following:
when you request a url, the router figures out what is needed and then uses the Dispatcher to open up the controller and run the corresponding method.
As the controller is fired up it includes and builds up the model that corresponds to that controller.
your method will then run and do what ever it needs to do.
When the controller is done calling all the code you have included the view class is executed which starts the rendering. It will include and render the corresponding view file and then the layout that has been set in the code.
all along the way there are a number of callbacks that are triggered in the various parts of the code, like controller::beforeFilter model::afterFind etc. Best to have a look a the api and book for more detailed information or ask a more specific question about that.
If you're at all familiar with Object Oriented code and php functions, you can start to read the CakePHP core methods. They will fill in a lot of blanks in terms of understanding the internal mechanics and relationships of Models Controllers and Views.

Resources