如何使用 get() 在 CakePhp 3 中获取给定 ID 的所有关联数据?
how do I get all associated data for a given ID in CakePhp 3 using get()?
这是我的情况....
我可以根据从 GUI 传递的项目 ID 查看项目详细信息
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']);
当我调试时,除了...
/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'
}
除非我将结果传递给 view.ctp、school_unit_id
、asset_source_id
、asset_category_id
、asset_group_class_id
、condition_id
、asset_status_id
正在显示保存在资产中的相应 ID 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; ?>
如何显示相应的显示字段而不是 ID?注意 asset_sources
table 仅关联到 assets
table。然后 assets
table 关联到 school_units
, asset_sources
, asset_categories
, asset_group_classes
, conditions
, asset_status
tables。在我看来。我想看到 school_unit_name
而不是 school_unit_id
。注意:我使用烘焙命令来创建应用程序。
谢谢
阅读收容措施。它会让你包含任何你想要的。 'contain' => ['Assets' => ['SchoolUnits']]
应该在这里工作,然后你可以在你的视图中使用 $assets->school_unit->name
或类似的东西。
这是我的情况....
我可以根据从 GUI 传递的项目 ID 查看项目详细信息
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']);
当我调试时,除了...
/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' }
除非我将结果传递给 view.ctp、
school_unit_id
、asset_source_id
、asset_category_id
、asset_group_class_id
、condition_id
、asset_status_id
正在显示保存在资产中的相应 ID 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; ?>
如何显示相应的显示字段而不是 ID?注意
asset_sources
table 仅关联到assets
table。然后assets
table 关联到school_units
,asset_sources
,asset_categories
,asset_group_classes
,conditions
,asset_status
tables。在我看来。我想看到school_unit_name
而不是school_unit_id
。注意:我使用烘焙命令来创建应用程序。
谢谢
阅读收容措施。它会让你包含任何你想要的。 'contain' => ['Assets' => ['SchoolUnits']]
应该在这里工作,然后你可以在你的视图中使用 $assets->school_unit->name
或类似的东西。