Order of afterSave in CakePHP 3.x models and plugin models? - cakephp

We have a CakePHP 3.x app that uses plugins. One plugin is a Customer list.
Another plugin is Complaints.
The main app has forms. When the main form is entered, or a new Customer is added, we want the Complaints plugin to collect the information into it's own tables: complaints_main_form, complaints_customers, complaints_customer_permissions. So then, when the user creates a new complaint in the Complaints plugin, the complaint form can autocomplete info from the main form and Customer list plugins. Also the Complaints plugin also has a permission system based on Customer type (from the Customer list plugin), and what action they can do in the Complaints system.
So my idea was to have a behaviour provided by the Complaints plugin, that would apply to the main app model. This would then oversee the saves made to Customer plugin models, the main app forms, and then can appropriately fill its own tables.
[ Main App [ Complaints.behaviour] ]
[ Customer List ][ other plugins...][Complaints ]
Customer List is an independent plugin, used in different apps, so I don't want to touch that.
So would this work? Can a Comlpaints.behaviour in the main app model, detect afterSave of the plugin models? Is the afterSave in the app model called last, so by that time all the plugins have done their saves? Or is there another way to update one plugin based on changes in another?

So would this work? Can a Comlpaints.behaviour in the main app model, detect afterSave of the plugin models? Is the afterSave in the app model called last, soby that time all the plugins have done their saves? Or is there another way to update one plugin based on changes in another?
Yes, all of that is possible. Use events and set a priority if you want to control the order. Check the subject of an event, if the subject is an instance of the plugins table object you know something happened there for the given event.
https://book.cakephp.org/3.0/en/core-libraries/events.html

Related

How to Add Records to 2sxc data from DNNSharp's Action Form

We have DNN Sharp's Action Form being used by client-partners to post new job listings. The Locations and Categories for these jobs are managed in a little 2sxc apps. We want to extend the ActionForm a little so that the user has the ability to add a new Location or Category when they post the job listing.
What method/action would I use in Action Form to be able to add a record to Locations or Categories data which are both inside the 2sxc App?
At the moment there is no quick way, because there is no adapter. You could create some manual code or create an adapter - but that's probably a day or so of work since you probably don't know the Action-Form APIs for all that.
But if you do do it, please share :)
I had a similar project. I wanted a form configuration tool for commonly used forms so that a staff person could fill out date, price etc. without getting under the hood.
I kludged it together using
var formFields;//Angular Controller For Form Fields
formFields = getScope('ActionFormCtrl').form.fields;
Then shoved in my 2SXC values
formFields.EventDate = "#Content.SessionDates";
formFields.$digest();
This is definitely not the angular way but it does work. Note that you need to run the JS after the form is rendered.

Best practice for loading non model data in ExtJS

Let's assume that I have 2 tables in my database, a user table and a folder table. They are matched by two models (beans) in my backend java code and similarly by to models in my extjs client side. Using viewmodel architecture, I want to create a form that shows the username and the amount of folders they have created using viewmodel bindings. Each folder in the database has a CREATE_USER_ID field that contains the id of the user that created the folder. How do I go about loading the required data?
Viewmodels are not designed to load data directly (and not designed to load non model data at all). You've got two options:
Go model (recommended). Two sub-options:
Add folder_amount field to your client side user model. On the server side this does not necessarily need to be added as well as you can adjust your API feeding data to client to add that field dynamically.
If you want to keep your client models exactly matching their server mates, use viewmodel in conjunction with associations (see example here, scroll down to Associations) to load folders themselves, though in the UI only show the amount of them. Mind that you don't need to load all folder fields but just their IDs.
Stick to your own fancy non model approach, but this won't have anything to do with viewmodel bindings. This may be, for example, making an AJAX call to retrieve the number of folders when user data is rendered in the UI.

Can i call .ctp file from another model in cakephp?

I want to call a .ctp file in one 'Model' and the .ctp file is in another 'View'.
Is it possible in cakephp?
Or instead of that should i call that 'Controller' function in my 'Model'?
CakePHP in a nutshell, and the keywords that you might need to search for:
Dispatcher and Routing controlling how URL reaches your controller.
Controller places system logic and controlling individual routing requests from Dispatcher
Component places logics that can be easily shared by Controllers
Model is for all the database related queries, manipulation, selections, deletions
Behavior can be deem as similar to Component, that provides "mixins" to Models to achieve similar behaviors, such as TreeBehavior abstract your database tables into parent-child relationship.
View is used by Controller to render individual pages to the user
Helper placed shared functionalities to help View render certain stuff. For example, FormHelper helps you all sort of form rendering, inputs, etc.
Place globally shared library in app/Lib folder so it can be easily accessed through using App::uses('...', 'Lib'). For example, a Gravatar library that helps you convert emails to md5-hashed strings. So this can be used everywhere in your app.
vendors are for those packaged vendor libraries that do not respect MVC, for example, swiftmailer that helps you send emails. Usually I would abstract them into my Lib folder for ease.
plugins are for those baked CakePHP applications found everywhere in the internet.
There are others in-depth stuff that you might be interested in, but these are the most basic stuff that you need to know before using a MVC framework like CakePHP. Check out their docs before diving in.
You can't access View (.ctp) in Model, it's against MVC architecture and logic. Just tell us more what do you want to do, maybe you're doing something wrong.

in backbone.js can a Model be without any url?

I have an app where the menu system is built dynamically using metadata fetched at startup. Based on this data, and menu selections, I need to craft a "filter box" where user can input search criteria. The "main" View consists of a filter box plus a search results panel where result(s) are rendered in accordance with their classes.
Can I model the Filter Box as a Backbone.js Model? It does not have any data fetches from the backend as its composition depends entirely on the menu selections + the metadata? E.g. when user selects "Sales" menu then the filter box might prompt for "Sales Order Number" whereas when user selects "Material" then the filter box might prompt for something else.
I would then use this widget as component of the "main" View, along with a set of results views made up on the fly. As users make their menu selections, this main View will un-render the existing filter box and recompute and re-render a new one. Other components on the screen could query the Filter Box for its settings.
The examples I have seen so far always have a url and a server fetch, save, etc. The only url-free example on the tutorial page says it is a "contrived" example. I was wondering if a backend provider is necessary and programming will be full of gotchas without conforming to this requirement.
Thanks.
You can have models without url property defined. One of the building blocks of Backbone is the Sync object, that will help you when pulling and pushing data, ideally from/to REST endpoints. For this to work you need to tell where the data are served, and to do so you set a value to url on Models or Collections.
If you don't need server comunication but you just want to use the utilities provided by simple Model or Collection (such as event handling, filtering, etc..) you just don't set url and you are good to go (just keep in mind that methods like fetch or save won't work).
Yes you can use Backbone for your DOM logic too. A model doesn't need to represent data from the server. Do whatever you like with the few basic elements of Backbone, simply use them when you feel like it'd do a great job :)

cakephp plugin model/controller cache issue with main model/controller

I have a plugin with user model, profile model and an user controller, in this user model is associated with profile model. In my main model folder (under app), I have user model and user controller(here I have not associated with profile). Sometimes I'm getting errors saying that user model is not associated with profile model. Also sometimes I'm getting the error - "missing action logout in users controller". I have given the logout action in the app/controller/userscontroller but that method is not available in myplugin/usercontroller. Im using cakephp2.0.. How can I solve this issue ? How cakephp is setting the cache for models and controllers ? I don't want to completely disable the cache.
I've had trouble with this as well. Basically it comes down to the fact that Cake doesn't support controllers with the same class name. So a controller named UsersController on plugin and app level will cause trouble with caching and some components (the Auth component, for example).
Support for identical classnames in various levels of a Cake application will come in Cake 3.0 which will require PHP 5.3, which in turn supports namespaces, a feature needed for correctly handling duplicate class names.
With no word on when Cake 3.0 will be released as the 2.0 branch is just out of beta, I refactored my plugin by prepending the plugin name to my controllers, views and models.
So UserModel became PluginUserModel and UsersController became PluginUsersController. It's a bit of a hassle, because you have to update all the views and variables which use the model's name.
My original question contains some links to the Cake bug tracker where similar questions were raised, should you be interested in some background,

Resources