我试图为我的条令实体定义生成输入表单。
为了为数组字段生成一个输入字段,并且与特定实体字段的类型无关,我需要能够获得数组应该的长度。
根据文档中的信息,我似乎无法做到这一点(在sql中存储为序列化字符串)。是否可以添加长度约束的定义,我可以通过返回 [classMetaData]->fieldMappings['fieldName']
?
<?php
declare(strict_types=1);
namespace WebApp\Views;
abstract class ProductView implements IProductView
{
/**
* This property must be initialised for each concrete instance of this base class
* @var ProductController
*/
protected ProductController $controller;
protected array $formTypeMappings = [
"string" => "type='text'",
"integer" => "type='number' step='1'",
"float" => "type='number' step='0.01'"
];
public function displayForm(): string
{
$formHeader = "<form action='TODO' method='#'>";
$formFields = "";
$formFooter = "</form>";
foreach (array_values($this->controller->getFieldMap()) as $fieldConfig) {
var_dump($fieldConfig);
$formFields .= $this->makeFormField($fieldConfig);
}
return $formHeader . $formFields . $formFooter;
}
protected function makeFormField(array $fieldConfig)
{
if ($fieldConfig['type'] === "array") {
$fieldValue =
for($i = 0; $i < count())
}
$inputName = $fieldConfig['fieldName'] . "Input";
return "<div class='form-group'>
<label for='$inputName'>{$fieldConfig['fieldName']}</label>
<input {$this->formTypeMappings[$fieldConfig]} class='form-control id=$inputName>
</div>";
}
}
所需实体定义
<?php
declare(strict_types=1);
namespace WebApp\Models;
/**
* @Entity
*
*/
class Furniture extends Product
{
/**
* @column(type="array")
*
* @var array
*/
protected $dimensions;
/**
* Hard-coded product type declaration to ensure correct mapping for single-table inheritance
* @column(type='string')
*
* @var string
*/
public const PRODUCT_TYPE = 'furniture';
public function __construct(string $name, float $price, array $dimensions)
{
parent::__construct($name, $price);
//sanity check on dimensions argument
if (count($dimensions) !== 3) {
throw new \LengthException('\'$dimensions\' array passed to ' . __CLASS__
. '\'s constructor is ' . count($dimensions) . ' items not 3.');
}
$this->dimensions = $dimensions;
}
public function getDimensions(): array
{
return $this->dimensions;
}
}
n、 b:这是mysql数据库连接
是否可以通过扩展 ArrayType
定义
暂无答案!
目前还没有任何答案,快来回答吧!