我有两张桌子:
departamento(“地区”的西班牙语)
provincia(西班牙语“省”)
“departamento”有三个领域:
id_departamento
departamento(地区名称)
presidente(州长)
“provincia”有四个领域:
id_provincia
id_departamento(外键)
省(省名)
gobernador(州长)
我使用命令行(模型,控制器和模板)生成cakephp代码。
但是,在生成的视图中,显示插入输入ID,我不想要它 - >
SCREEN CAPTURE
当我要添加新省时,我想用区域名称显示和替换区域ID字段,如下拉列表
PROVINCIACONTROLLER.PHP
<?php namespace App\Controller; use App\Controller\AppController; /** * Provincia Controller * * @property \App\Model\Table\ProvinciaTable $Provincia */ class ProvinciaController extends AppController { /** * Index method * * @return \Cake\Network\Response|null */ public function index() { $provincia = $this->paginate($this->Provincia); $this->set(compact('provincia')); $this->set('_serialize', ['provincia']); } /** * View method * * @param string|null $id Provincium id. * @return \Cake\Network\Response|null * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $provincium = $this->Provincia->get($id, [ 'contain' => [] ]); $this->set('provincium', $provincium); $this->set('_serialize', ['provincium']); } /** * Add method * * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. */ public function add() { $provincium = $this->Provincia->newEntity(); if ($this->request->is('post')) { $provincium = $this->Provincia->patchEntity($provincium, $this->request->data); if ($this->Provincia->save($provincium)) { $this->Flash->success(__('The provincium has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The provincium could not be saved. Please, try again.')); } } $this->set(compact('provincium')); $this->set('_serialize', ['provincium']); } /** * Edit method * * @param string|null $id Provincium id. * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise. * @throws \Cake\Network\Exception\NotFoundException When record not found. */ public function edit($id = null) { $provincium = $this->Provincia->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $provincium = $this->Provincia->patchEntity($provincium, $this->request->data); if ($this->Provincia->save($provincium)) { $this->Flash->success(__('The provincium has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The provincium could not be saved. Please, try again.')); } } $this->set(compact('provincium')); $this->set('_serialize', ['provincium']); } /** * Delete method * * @param string|null $id Provincium id. * @return \Cake\Network\Response|null Redirects to index. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); $provincium = $this->Provincia->get($id); if ($this->Provincia->delete($provincium)) { $this->Flash->success(__('The provincium has been deleted.')); } else { $this->Flash->error(__('The provincium could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); } }
PROVINCIATABLE.PHP
<?php namespace App\Model\Table; use Cake\ORM\Query; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; /** * Provincia Model * * @method \App\Model\Entity\Provincium get($primaryKey, $options = []) * @method \App\Model\Entity\Provincium newEntity($data = null, array $options = []) * @method \App\Model\Entity\Provincium[] newEntities(array $data, array $options = []) * @method \App\Model\Entity\Provincium|bool save(\Cake\Datasource\EntityInterface $entity, $options = []) * @method \App\Model\Entity\Provincium patchEntity(\Cake\Datasource\EntityInterface $entity, array $data,
array $ options = []) * @method \ App \ Model \ Entity \ Provincium [] patchEntities($ entities,array $ data,array $ options = []) * @method \ App \ Model \ Entity \ Provincium findOrCreate($ search,callable $ callback = null) * / class ProvinciaTable扩展了Table {
/** * Initialize method * * @param array $config The configuration for the Table. * @return void */ public function initialize(array $config) { parent::initialize($config); $this->table('provincia'); $this->displayField('id_provincia'); $this->primaryKey('id_provincia'); } /** * Default validation rules. * * @param \Cake\Validation\Validator $validator Validator instance. * @return \Cake\Validation\Validator */ public function validationDefault(Validator $validator) { $validator ->integer('id_provincia') ->allowEmpty('id_provincia', 'create') ->add('id_provincia', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']); $validator ->integer('id_departamento') ->requirePresence('id_departamento', 'create') ->notEmpty('id_departamento'); $validator ->requirePresence('provincia', 'create') ->notEmpty('provincia'); $validator ->requirePresence('gobernador', 'create') ->notEmpty('gobernador'); return $validator; } /** * Returns a rules checker object that will be used for validating * application integrity. * * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @return \Cake\ORM\RulesChecker */ public function buildRules(RulesChecker $rules) { $rules->add($rules->isUnique(['id_provincia'])); return $rules; } }
PROVINCIA.PHP
<?php namespace App\Model\Entity; use Cake\ORM\Entity; /** * Provincium Entity * * @property int $id_provincia * @property int $id_departamento * @property string $provincia * @property string $gobernador */ class Provincium extends Entity { /** * Fields that can be mass assigned using newEntity() or patchEntity(). * * Note that when '*' is set to true, this allows all unspecified fields to * be mass assigned. For security purposes, it is advised to set '*' to false * (or remove it), and explicitly make individual fields accessible as needed. * * @var array */ protected $_accessible = [ '*' => true, 'id_provincia' => false ]; }
ADD.CTP(ADD PROVINCE)
<nav class="large-3 medium-4 columns" id="actions-sidebar"> <ul class="side-nav"> <li class="heading"><?= __('Actions') ?></li> <li><?= $this->Html->link(__('List Provincia'), ['action' => 'index']) ?></li> </ul> </nav> <div class="provincia form large-9 medium-8 columns content"> <?= $this->Form->create($provincium) ?> <fieldset> <legend><?= __('Add Provincium') ?></legend> <?php echo $this->Form->input('id_departamento'); echo $this->Form->input('provincia'); echo $this->Form->input('gobernador'); ?> </fieldset> <?= $this->Form->button(__('Submit')) ?> <?= $this->Form->end() ?> </div>
感谢您的帮助
答案 0 :(得分:0)
如果您需要 departmento 的下拉列表,则需要使用find('list')并设置将其提取到模板中。
考虑到您的表名是Departmento,并且您希望填充值id_departamento和text departamento,请尝试以下操作:
/* Controller Code */
public function add()
{
$provincium = $this->Provincia->newEntity();
if ($this->request->is('post')) {
/* Other code */
}
/* Modified code starts */
$departmento = $this->Departmento->find('list', [
'keyField' => 'id_departamento',
'valueField' => 'departmento'
]);
$this->set(compact('departmento'));
/* Modified code ends*/
$this->set(compact('provincium'));
$this->set('_serialize', ['provincium']);
}
/* Template code */
echo $this->Form->select('id_departamento', $departmento, [
'empty' => 'Departmento'
]);