Quantcast

Choose what field show when depth level is 2

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Choose what field show when depth level is 2

Mariano C.
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:"[hidden email]",
                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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Choose what field show when depth level is 2

jodator
You can use Containable behavior and write something like this:

$Office->find('all', array(
   ...
   'contain' => array(//or 'contains' -> check manual ;)
    'Company' => array('fields' => array('name')),
    'Person' => array(
        'fields' => array('mail'),
        'OfficePersonTask' => array( )
             'Office' => array('fields' => 'name'),
             'Task' => array('fields' => 'name'),
        ),
   ),
   ...
));

Or try setting the fields names in $hasMany, $hasOne, etc array http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasone

On Tuesday, July 31, 2012 12:16:38 PM UTC+2, Mariano C. wrote:
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:"[hidden email]",
                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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Choose what field show when depth level is 2

Mariano C.
In reply to this post by Mariano C.
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:"[hidden email]",
                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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Loading...