Associations: Linking Models

Note: Since Bancha 2.1 now also support sending of nested associated data. This makes your apps even faster.

Defining relations between different objects in your application should be a natural process. For example: in a article database, a article may have many comments, articles have a single author, and authors may have many articles. Defining the way these relations work allows you to access your data in an intuitive and powerful way.

Bancha will share all associations and also send associated data to the browser. Let's take a deeper look. For the above described association you would define a Author model like the following:

class Article extends AppModel {
     * Expose this model for Ext JS and Sencha Touch
    public $actsAs = array('Bancha.BanchaRemotable');

     * An Article has many Comments
    public $hasMany = array('Comment');

     * An Article belongs to an Author
    public $belongsTo = array('Author');

This simple model definition will also automatically be set up in Ext JS/Sencha Touch.

If you ArticlesController::index method now returns articles including the associated data, Bancha will recognize the association and send the data as well in the format Ext JS and Sencha Touch expects it.

Nested Data

The question if you want to send associated (=nested) data or not is a decision about performance. If you app will use 90% of the nested data in the next minute it will make sense to send all the data in just one request. On the other hand, if you would only use 10% it will most likely be better to request the 10% from the server when you need it.

It is also a decision based on the amount of data. If your requests have hugh amounts of data it will slow down your browser and might need a lot of memory on your users device.

There is not one right solution for all situations, you will need to find the solution best matching your apps need. For this reason we have designed a flexible solution, which allows you to adopt the behavior to your own needs. The result looks pretty simple, and is really flexible.

Bancha transforms all the data it get's from the controller action. This means if your return value includes associated data (e.g. a $Model->find() with $Model->recursive=1), it will send this associated data to the client, if you return only the primary record ($Model->recursive=-1), the client will only receive the primary record. In most cases you will need to find a middle ground by specificly defining the associated model records you want to load. The CakePHP Containable behavior provides a nice way to archieve this.

Filtering of nested data

From all the associated data, Bancha will always send only the exposed fields or models. This garantees that you never have to worry about hidding data yourself, you just need to configure the models correctly using the exclude feature.

Hiding associations

If you want to hide an association from the frontend, you also want to hide the associated key field. Otherwise the user would be able to figure out the association anyway. For this reason, to hide an association from the fontend simple exclude the associated model field from the list of exposed fields. Bancha will recognize this and also hide the association. E.g. to hide that an article belongs to an author:

class Article extends AppModel {
     * Expose this model for Ext JS and Sencha Touch
    public $actsAs = array('Bancha.BanchaRemotable' => array(
        'excludedFields' => array('author_id')


See also your associations example.


Add a comment