Reputation: 362
CakePHP 3.10 adding a Form field with '_id' in name, automatically makes it a select field. I want to know if this is something that is done in our App specifically by previous developer and if so, how/where would I look?
Alternatively, if it's a CakePHP thing, where can I get documentation on it?
Controller:
public function add()
{
$building = $this->Buildings->newEntity();
if ($this->request->is('post')) {
//code for after form submission
}
$this->set(compact('building'));
}
View:
<div>
<?= $this->Form->create($building) ?>
<fieldset>
<legend><?= __('Add Building') ?></legend>
<?php
echo $this->Form->control('test_id'); <!-- this field type will be a select field -->
echo $this->Form->control('name');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
Upvotes: 1
Views: 108
Reputation: 184
If you read this V3 documentation you can see:
https://book.cakephp.org/3/en/views/helpers/form.html#namespace-Cake\View\Helper
So, the cause of this is because cakephp understand you have a belongsTo or hasOne association between Buildings
model and Test
model.
If you have a test_id column in buildings table, you are saying to cakephp that tests table has one or has many building/s.
you can force the type of Form Helper using the condition:
$this->Form->control('test_id', ['type' => 'text']);
Extra note: when you have this case you could use virtual fields to show what is the test_id that the Building belongs. For example: you could show the concatenation of the test name and author name (assuming these two fields exist in the test table) like:
Into TestEntity.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
class Test extends Entity
{
protected function _getTestIndicator()
{
return $this->name . ' ' . $this->author_name;
}
}
Into the view:
echo $user->test_indicator;
Upvotes: 2