Answers to Get the related table values in cakephp3 ( 2 )

  1. 2017-01-07 06:01

    You can use contain to get associated table's data..

    If you want association of association then you can use recursive too. But if you want this only with one association then you can try this:

    $data = $this->PurchaseDetails->find('all')
                        ->contain(['MeasurementInventories' => ['MeasurementLables']])->toArray();
    
  2. 2017-01-09 07:01

    You have to use contain() to get associated table /model data like:-

    $data = $this->PurchaseDetails->find()->contain(['MeasurementInventories' => ['MeasurementLables']])->toArray();
    

    If your requirement is to show data only from measurement_lables then you can add select() to get your desired model data like:-

    $data = $this->PurchaseDetails->find()->contain(['MeasurementInventories' => ['MeasurementLables']])->select(['MeasurementLables.*'])->toArray();
    

    Note:- while using contain() you should be sure that the models are associated with each other. for example PurchaseDetails model should be associated with MeasurementInventories(belongsTo, hasMany, hasOne etc...)

    $this->belongsTo('PurchaseDetails', [
        'className' => 'MeasurementInventories',
        'foreignKey' => 'columnName'            
    ]);
    

    And MeasurementInventories is associated with MeasurementLables.

    $this->belongsTo('MeasurementInventories', [
        'className' => 'MeasurementLables',
        'foreignKey' => 'columnName'            
    ]);
    

Leave a reply to - Get the related table values in cakephp3

◀ Go back