0

My question extends one posted previously CakePHP: Limit Fields associated with a model. I used this solution effectively for limiting the returned fields for the parent table with this call

$data = $this->SOP10100->find('all', 
        array('fields' => $this->SOP10100->defaultFields));

However, this method returns the filtered parent and unfiltered child fields. I have 131 child fields of which I only need 7. I have the same defaultFields array construct in the child table. How do I modify this call ( or create a new one) that will return the filtered fields for both parent and child models in the same array?

Here is the structure for the array for the parent table:

    public $defaultFields = array(
    'SOP10100.SOPNUMBE',
    'SOP10100.INVODATE',
    'SOP10100.DOCDATE',
    'SOP10100.DOCAMNT',
    'SOP10100.SUBTOTAL');

Your help is appreciated.

Community
  • 1
  • 1
Mike S.
  • 2,048
  • 1
  • 32
  • 55
  • are you using `belongTo` or `hasA` for defining parent-children relationships? if so, are you setting the filter in there for the joining tables? – pollirrata Apr 25 '12 at 20:57

1 Answers1

1

SCORE! Wow, solved two big problems in one day. I finally figured it out with loads of help from many resources:

    $this->InvoiceHeader->Behaviors->attach('Containable');
    $data = $this->InvoiceHeader->find('all', array(
        'fields' => $this->InvoiceHeader->defaultFields,
        'contain' => array(
            'InvoiceDetail' => array(
               'fields' => $this->InvoiceDetail->defaultFields))
            )
    );

returns my array data just like I want it:

array(
(int) 0 => array(
    'InvoiceHeader' => array(
        'SOPNUMBE' => 'SVC0202088           ',
        'INVODATE' => '2012-04-17 00:00:00',
        'DOCDATE' => '2012-04-17 00:00:00',
        'DOCAMNT' => '.00000',
        'SUBTOTAL' => '.00000'
    ),
    'InvoiceDetail' => array(
        (int) 0 => array(
            'ITEMNMBR' => 'SERVICE                        ',
            'QUANTITY' => '1.00000',
            'UOFM' => 'EA       ',
            'UNITPRCE' => '.00000',
            'TAXAMNT' => '.00000',
            'CONTSTARTDTE' => '2012-04-17 00:00:00',
            'CONTENDDTE' => '2012-04-30 00:00:00',
            'SOPNUMBE' => 'SVC0202088           '
        ),
Mike S.
  • 2,048
  • 1
  • 32
  • 55