PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 30, 2022

[FIXED] how do I get all associated data for a given ID in CakePhp 3 using get()?

 August 30, 2022     cakephp, cakephp-3.0, mysql, php     No comments   

Issue

Here is my situation....

  1. I can view item details based on item ID passed from GUI

    public function view($id = null){
    
    $id = $this->request->getData('assetsource_id');
    $assetSource = $this->AssetSources->get($id, [
        'contain' => ['Assets']
    ]);
    
    debug($assetSource);
    die();
    
    $this->set('assetSource', $assetSource);
    $this->set('_serialize', ['assetSource']);
    
  2. When I debug, I get the following except...

    /src/Controller/AssetSourcesController.php (line 64)
    
      object(App\Model\Entity\AssetSource) {
    
      'id' => (int) 18,
      'name' => 'Donated',
      'created_by' => '',
      'assets' => [
    (int) 0 => object(App\Model\Entity\Asset) {
    
        'id' => (int) 1,
        'school_unit_id' => (int) 33,
        'asset_source_id' => (int) 18,
        'asset_description' => 'TOYOTA HILUX',
        'date_of_entry' => object(Cake\I18n\FrozenDate) {
    
            'time' => '2021-05-31T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false
    
        },
        'date_of_purchase' => object(Cake\I18n\FrozenDate) {
    
            'time' => '2021-05-31T00:00:00+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false
    
        },
        'grn_number' => 'KBHBBH92',
        'name_of_supplier' => 'TOYOTA ZAMBIA',
        'serial_number' => 'YTDIYTFYUFOGOOHH',
        'location' => 'BURSAR',
        'asset_category_id' => (int) 24,
        'asset_group_class_id' => (int) 65,
        'full_asset_number' => 'GGFUYG88',
        'condition_id' => (int) 12,
        'asset_status_id' => (int) 14,
        'value' => '400,000',
        'custodian_name' => 'JOE BANDA',
        'custodian_phone' => '0966010101',
        'custodian_email' => 'bursar@unza.zm',
        'created' => object(Cake\I18n\FrozenTime) {
    
            'time' => '2021-05-31T07:26:31+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false
    
        },
        'modified' => object(Cake\I18n\FrozenTime) {
    
            'time' => '2021-05-31T07:26:31+00:00',
            'timezone' => 'UTC',
            'fixedNowTime' => false
    
        },
        'created_by' => 'admin',
        '[new]' => false,
        '[accessible]' => [
            '*' => true,
            'id' => false
        ],
        '[dirty]' => [],
        '[original]' => [],
        '[virtual]' => [],
        '[errors]' => [],
        '[invalid]' => [],
        '[repository]' => 'Assets'
    
    }
       ],
         '[new]' => false,
         '[accessible]' => [
         '*' => true,
         'id' => false
       ],
         '[dirty]' => [],
         '[original]' => [],
         '[virtual]' => [],
         '[errors]' => [],
         '[invalid]' => [],
         '[repository]' => 'AssetSources'
       }
    
  3. Except when I pass the result to the view.ctp, school_unit_id, asset_source_id, asset_category_id, asset_group_class_id, condition_id, asset_status_id are showing the corresponding IDs as saved in Assets table.

                                    <?php foreach ($assetSource->assets as $assets) : ?>
                                        <tr>
                                            <td><?php echo $i++ ?></td>
                                            <td><?= h($assets->school_unit_id) ?></td>
                                            <td><?= h($assets->asset_description) ?></td>
                                            <td><?= h($assets->date_of_purchase) ?></td>
                                            <td><?= h($assets->name_of_supplier) ?></td>
                                            <td><?= h($assets->location) ?></td>
                                            <td><?= h($assets->condition_id) ?></td>
                                            <td><?= h($assets->value) ?></td>
                                            <td><?= h($assets->custodian_name) ?></td>
    
                                        </tr>
                                    <?php endforeach; ?>
    
  4. How do I show the respective display fields instead of IDs? Notice asset_sources table is only associated to assets table. Then assets table is associated to school_units, asset_sources, asset_categories, asset_group_classes, conditions, asset_status tables. In my view. I want to see school_unit_name not school_unit_id. NOTE: I used bake commands to create the application.

Thanks


Solution

Read up on containment. It will let you include whatever you want. 'contain' => ['Assets' => ['SchoolUnits']] should work here, and then you can use $assets->school_unit->name or something like that in your view.



Answered By - Greg Schmidt
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing