1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    19
    Vote Rating
    0
    Marrbacca is on a distinguished road

      0  

    Default Associated Models + PHP & Mysql

    Associated Models + PHP & Mysql


    Hey everybody.
    First, I'm not sure if I shouldn't post it on Sencha Touch forum, but the problem looks related strictly to ExtJs, not Touch.
    I'm trying to implement some associated models and stores in ExtJS and combined them with data getting from MySQL db using some simple PHP classes. Then I want to show those data in some lists using Sencha Touch.
    My example code shows some clients. Those clients can have multiple bookings, and one booking can have one client. Association should be pretty clear.
    My current Model and Stores code looks like this:
    Code:
    App.models.BookingModel = Ext.regModel('BookingModel', {
        idProperty: 'bookingID',
        fields: [
            'bookingID', 
            'clientID', 
            'stylistID', 
            'salonID',
            'serviceID', 
            'startDate',
            'splitStartDate',
            'splitEndDate',
            'endDate',
            'status',
            'clientNotes',
            'stylistNotes',
            'reminderSent',
            'charge',
            'offset'
        ],
        proxy: {
            type: 'ajax',
            url : 'includes/data_api.php?model=Booking',
            reader: {
                type: 'json',
                root: 'items'
            }
        },
        associations: [
            {type: 'belongsTo', model: 'ClientModel', getterName: 'getClient', primaryKey: 'clientID', foreignKey: 'clientID'}
        ]
    });
    Code:
    App.stores.BookingStore = new Ext.data.Store({    model : 'BookingModel',
        sorters: 'startDate',
        autoLoad: true,
        getGroupString : function(record) {
            var dt = new Date(record.get('startDate'));
            return Ext.util.Format.date(dt, 'D Y M j');
         }
    });
    Code:
    App.models.ClientModel = Ext.regModel('ClientModel', {
        idProperty: 'clientID',
        fields: [
            'clientID',
            'firstName',
            'lastName',
            'email',
            'phone1',
            'extension1',
            'phone2',
            'extension2',
            'workPh',
            'workExt',
            'address1',
            'address2',
            'city',
            'province',
            'country',
            'reminderType',
            'postal',
            'noteID',
            'referredBy',
            'quickLogin',
            'creationDate',
            'modifiedDate',
            'birthDate'
        ],
        proxy: {
            type: 'ajax',
            url : 'includes/data_api.php?model=Client',
            reader: {
                type: 'json',
                root: 'items'
            }
        },
        associations: [
            {type: 'hasMany', model: 'BookingModel', primaryKey: 'clientID', foreignKey: 'clientID', name: 'bookings'}
        ]
    });
    Code:
    App.stores.ClientStore = new Ext.data.Store({    model : 'ClientModel',
        sorters: 'lastName',
        autoLoad: true,    
        getGroupString : function(record) {
            return record.get('lastName')[0]; // optional char from array removed
        }
    });
    Data is getting correctly from the php script, but I can't get the associated data works in application. When I logging the loaded Client store I see that in "data" node it has some bookings array, but it's empty for all clients. Logging the bookings store nothing indicates any of the associated client data anywhere.
    The whole point of this is to show the list of bookings grouped by days, but I want to use some of the clients data in template.
    Can you help me?

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,649
    Vote Rating
    817
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You have used the getter methods that are created? What happens with those?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Dec 2011
    Posts
    19
    Vote Rating
    0
    Marrbacca is on a distinguished road

      0  

    Default


    mitchellsimoens - thank you for your reply.
    Did I have to use those methods directly? These are not called automatically?
    I tried putting some code into one of the controller action:
    Code:
    var booking = App.stores.BookingStore.getAt(0);        booking.getClient(function(client, operation) {
                console.log(client);
            }, this);
            console.log(booking);
    but it always getting the same client no matter what index I use at getAt method.
    In the network tab on Programmers Tool in Chrome (I'm using it for testing) I see that the .php file specified in Client model proxy is called again after using the getClient function, but it is called without any specific parameter, so it returns standard list of Clients.
    Reading the manual today, I also think I found some mistake in my previous association code in model files.
    I change the association on Client model to:
    Code:
    associations: [        {type: 'hasMany', model: 'BookingModel', primaryKey: 'bookingId', foreignKey: 'clientID', name: 'bookings'}
        ]
    changing the primaryKey property to the one from associated model.

    Do you have any clues how to fix it?
    Also I'm thinking that using Touch and ExtJS combined with PHP & MySQL using related tables is common enough to write some examples how to use it properly. On all web tutorial I can find are always examples of simple ajax proxies getting everything from one static json file...

Thread Participants: 1