我有2个表(drugs_and_medications
和drug_images
),它们与belongsTo
链接在一起,如下所示:
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('drugs_and_medications');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('DrugImages', [
'foreignKey' => 'drug_image_id',
]);
}
在我的Drugs_And_Medications
表的编辑功能中,我有一个表格,可让我从Drug_Image
表的图片中获取ID。
Drugs_and_medications
表的编辑功能:
public function edit($id = null)
{
$drugsAndMedication = $this->DrugsAndMedications->get($id, [
'contain' => ['Prescriptions'],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$drugsAndMedication = $this->DrugsAndMedications->patchEntity($drugsAndMedication, $this->request->getData());
if ($this->DrugsAndMedications->save($drugsAndMedication)) {
$this->Flash->success(__('The drugs and medication has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The drugs and medication could not be saved. Please, try again.'));
}
$drugImages = $this->DrugsAndMedications->DrugImages->find('list', ['limit' => 200]);
//$prescriptions = $this->DrugsAndMedications->Prescriptions->find('list', ['limit' => 200]);
$this->set(compact('drugsAndMedication', 'drugImages'));
}
这是编辑功能的模板:
<div class="drugsAndMedications form large-9 medium-8 columns content">
<?= $this->Form->create($drugsAndMedication) ?>
<fieldset>
<legend><?= __('Edit Drugs And Medication') ?></legend>
<?php
echo $this->Form->control('drug_name');
echo $this->Form->control('drug_cost');
echo $this->Form->control('drug_available_date');
echo $this->Form->control('drug_withdrawn_date');
echo $this->Form->control('drug_description');
echo $this->Form->control('drug_image_id', ['option' => $drugImages]);
//echo $this->Form->control('prescriptions._ids', ['options' => $prescriptions]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
这是我的drugs_and_medications
和drug_images
表:
CREATE TABLE `drugs_and_medications` (
`id` int(10) NOT NULL,
`drug_name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`drug_cost` int(3) NOT NULL,
`drug_available_date` date NOT NULL,
`drug_withdrawn_date` date NOT NULL,
`drug_description` text COLLATE utf8mb4_general_ci,
`drug_image_id` int(11) DEFAULT NULL,
`created` date NOT NULL,
`modified` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
ALTER TABLE `drugs_and_medications`
ADD CONSTRAINT `drugs_and_medications_ibfk_1` FOREIGN KEY (`drug_image_id`) REFERENCES `drug_images` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
CREATE TABLE `drug_images` (
`id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`path` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`created` date NOT NULL,
`modified` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
我应该从drug_images
表中获取图像ID,并将其放在drugs_and_medications
字段的drug_image_id
表中,这样我就可以在索引中显示图片,但是无论何时我尝试获取image_id
,即使我可以在列表中看到它,它也返回null
,甚至可以单击并选择它。所以我无法在应用程序中显示图像。
如果需要,您总是可以要求我显示更多代码。