Saturday, August 4, 2012

Re: Choose what field show when depth level is 2

It is working, but how can I make a sort of conditional where? For example, I get a json like this 

{
    "Offices": [
        {
            "Person": {
                "id": "5",
                "full_name": "John Doe"
            },
            "Task": {
                "id": "1",
                "short_name": "Manager"
            }
        },
        {
            "Person": {
                "id": "2",
                "full_name": "Stagger Lee"
            },
            "Task": {
                "id": "9",
                "short_name": "Bad person"
            }
        }
    ]
}
This Json is prepared by the following model method:

public function getOfficesByRegion($region){
        $this->Behaviors->attach('Containable');
        
        return $this->find('all', array(
            'contain' => array(
                'Task' => array('fields' => array('id', 'short_name'),
                               //'conditions' => array('Task.short_name LIKE' => 'Bad Person')
                               ),
                'Person' => array('fields' => array('id', 'full_name')),
                'Office' => array('fields' => array('id', 'city', 'address', 'legal_office', 'region'), 
                                  'Company' => array('fields' => array('id', 'name'))), 
            )));
    }

If I want to remove all the info about a "Bad person" what can I do? If I remove comment from conditions row all that I get is:
{
    "Offices": [
        {
            "Person": {
                "id": "5",
                "full_name": "John Doe"
            },
            "Task": {
                "id": "1",
                "short_name": "Manager"
            }
        },
        {
            "Person": {
                "id": "2",
                "full_name": "Stagger Lee"
            },
            "Task": {
                "id": null,
                "short_name": null
            }
        }
    ]
}
Il giorno martedì 31 luglio 2012 12:16:38 UTC+2, Mariano C. ha scritto:
In CakePHP 2.1 I have prepared a Json webservice. This is an excerpt of what I get:

    {
       Offices:[
          {
             Office:{
                id:"1",
                Company:{
                   id:"1",
                   name:"ABC Software"
                }
             },
             Person:{
                mail:"sASa@sAAsS.it",
                OfficePersonTask:[
                   {
                      office_id:"1",
                      person_id:"1",
                      task_id:"1"
                   }
                ]
             }
          }
       ]
    } 

What I want is the possibility to show only some field from relative database, so the method that query the database have `$options['fields']` properly setting, so if I want to show person's mail I write:

$options['fields'] = array("Person.mail");

but what if I want to show company's id or company's name whice is one level deeper?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: