我遇到一个unexpected '->' (T_OBJECT_OPERATOR)
错误,但无法解决:
$form = $this->createFormBuilder($item)
foreach ($classes->fieldMappings as $fieldMapping) {
->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control')))
}
->add('cancel', ButtonType::class, array('label' => 'Abbrechen','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar close_h')))
->add('save', SubmitType::class, array('label' => 'Speichern','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))
->getForm();
$form->handleRequest($request);
一个想法是更改此行:
$form = ($this->createFormBuilder($item))
但这无法解决,我不知道该怎么办
var_dump($classes)
输出以下内容:
object(Doctrine\ORM\Mapping\ClassMetadata)#2979 (40) {
["name"]=>
string(18) "App\Entity\Members"
["namespace"]=>
string(10) "App\Entity"
["rootEntityName"]=>
string(18) "App\Entity\Members"
["customGeneratorDefinition"]=>
NULL
["customRepositoryClassName"]=>
string(29) "App\Repository\UserRepository"
["isMappedSuperclass"]=>
bool(false)
["isEmbeddedClass"]=>
bool(false)
["parentClasses"]=>
array(0) {
}
["subClasses"]=>
array(0) {
}
["embeddedClasses"]=>
array(0) {
}
["namedQueries"]=>
array(0) {
}
["namedNativeQueries"]=>
array(0) {
}
["sqlResultSetMappings"]=>
array(0) {
}
["identifier"]=>
array(1) {
[0]=>
string(2) "id"
}
["inheritanceType"]=>
int(1)
["generatorType"]=>
int(4)
["fieldMappings"]=>
array(5) {
["id"]=>
array(9) {
["fieldName"]=>
string(2) "id"
["type"]=>
string(7) "integer"
["scale"]=>
int(0)
["length"]=>
NULL
["unique"]=>
bool(false)
["nullable"]=>
bool(false)
["precision"]=>
int(0)
["id"]=>
bool(true)
["columnName"]=>
string(2) "id"
}
["username"]=>
array(8) {
["fieldName"]=>
string(8) "username"
["type"]=>
string(6) "string"
["scale"]=>
int(0)
["length"]=>
int(25)
["unique"]=>
bool(true)
["nullable"]=>
bool(false)
["precision"]=>
int(0)
["columnName"]=>
string(8) "username"
}
["password"]=>
array(8) {
["fieldName"]=>
string(8) "password"
["type"]=>
string(6) "string"
["scale"]=>
int(0)
["length"]=>
int(64)
["unique"]=>
bool(false)
["nullable"]=>
bool(false)
["precision"]=>
int(0)
["columnName"]=>
string(8) "password"
}
["email"]=>
array(8) {
["fieldName"]=>
string(5) "email"
["type"]=>
string(6) "string"
["scale"]=>
int(0)
["length"]=>
int(191)
["unique"]=>
bool(true)
["nullable"]=>
bool(false)
["precision"]=>
int(0)
["columnName"]=>
string(5) "email"
}
["isActive"]=>
array(8) {
["fieldName"]=>
string(8) "isActive"
["type"]=>
string(7) "boolean"
["scale"]=>
int(0)
["length"]=>
NULL
["unique"]=>
bool(false)
["nullable"]=>
bool(false)
["precision"]=>
int(0)
["columnName"]=>
string(9) "is_active"
}
}
["fieldNames"]=>
array(5) {
["id"]=>
string(2) "id"
["username"]=>
string(8) "username"
["password"]=>
string(8) "password"
["email"]=>
string(5) "email"
["is_active"]=>
string(8) "isActive"
}
["columnNames"]=>
array(5) {
["id"]=>
string(2) "id"
["username"]=>
string(8) "username"
["password"]=>
string(8) "password"
["email"]=>
string(5) "email"
["isActive"]=>
string(9) "is_active"
}
["discriminatorValue"]=>
NULL
["discriminatorMap"]=>
array(0) {
}
["discriminatorColumn"]=>
NULL
["table"]=>
array(1) {
["name"]=>
string(7) "members"
}
["lifecycleCallbacks"]=>
array(0) {
}
["entityListeners"]=>
array(0) {
}
["associationMappings"]=>
array(0) {
}
["isIdentifierComposite"]=>
bool(false)
["containsForeignIdentifier"]=>
bool(false)
["idGenerator"]=>
object(Doctrine\ORM\Id\IdentityGenerator)#3894 (1) {
["sequenceName":"Doctrine\ORM\Id\IdentityGenerator":private]=>
NULL
}
["sequenceGeneratorDefinition"]=>
NULL
["tableGeneratorDefinition"]=>
NULL
["changeTrackingPolicy"]=>
int(1)
["isVersioned"]=>
NULL
["versionField"]=>
NULL
["cache"]=>
NULL
["reflClass"]=>
object(ReflectionClass)#3910 (1) {
["name"]=>
string(18) "App\Entity\Members"
}
["isReadOnly"]=>
bool(false)
["namingStrategy":protected]=>
object(Doctrine\ORM\Mapping\UnderscoreNamingStrategy)#2431 (1) {
["case":"Doctrine\ORM\Mapping\UnderscoreNamingStrategy":private]=>
int(0)
}
["reflFields"]=>
array(5) {
["id"]=>
object(ReflectionProperty)#3911 (2) {
["name"]=>
string(2) "id"
["class"]=>
string(18) "App\Entity\Members"
}
["username"]=>
object(ReflectionProperty)#3912 (2) {
["name"]=>
string(8) "username"
["class"]=>
string(18) "App\Entity\Members"
}
["password"]=>
object(ReflectionProperty)#3913 (2) {
["name"]=>
string(8) "password"
["class"]=>
string(18) "App\Entity\Members"
}
["email"]=>
object(ReflectionProperty)#3914 (2) {
["name"]=>
string(5) "email"
["class"]=>
string(18) "App\Entity\Members"
}
["isActive"]=>
object(ReflectionProperty)#3915 (2) {
["name"]=>
string(8) "isActive"
["class"]=>
string(18) "App\Entity\Members"
}
}
["instantiator":"Doctrine\ORM\Mapping\ClassMetadataInfo":private]=>
object(Doctrine\Instantiator\Instantiator)#2984 (0) {
}
}
deceze♦
:
我有一个由mySQL数据库创建的实体。因为我有100个不同的实体(即数据库表),它们具有不同的字段,所以我想创建一个函数,可以根据其内容使用该函数。这就是我想要达到的目标:
$form = $this->createFormBuilder($item)
->add('id', TextType::class, array('attr' => array('class' => 'form-control')))
->add('username', TextType::class, array('attr' => array('class' => 'form-control')))
->add('email', TextType::class, array('attr' => array('class' => 'form-control')))
->add('is_active', TextType::class, array('attr' => array('class' => 'form-control')))
->add('password', TextType::class, array('attr' => array('class' => 'form-control')))
->getForm();
根据我的输入$slug
,我想在表单构建器中创建字段:
$EntityName = 'App\\Entity\\' . ucwords($slug);
$em = $this->getDoctrine()->getManager();
$cmf = $em->getMetadataFactory();
$classes = $cmf->getMetadataFor($EntityName);
这就是为什么我要从$ classes对象获取元数据的原因。该对象为我提供了我需要的字段,并通过一个foreach循环来构建它们
答案 0 :(得分:1)
您要尝试的内容太元了。您无法使用foreach
创建PHP源代码,该代码随后将作为源代码执行。您想要的可能是这样的:
$form = $this->createFormBuilder($item);
foreach ($classes->fieldMappings as $fieldMapping) {
$form = $form->add($fieldMapping['fieldName'], TextType::class, array('attr' => array('class' => 'form-control')));
}
$form->add('cancel', ButtonType::class, array('label' => 'Abbrechen','attr' => array('class' => 'cancel form-btn btn btn-default pull-right close_sidebar close_h')))
->add('save', SubmitType::class, array('label' => 'Speichern','attr' => array('id' => 'submit-my-beautiful-form','class' => 'form-btn btn btn-info pull-right','style' => 'margin-right:5px')))
->getForm();
这是一个方法链:
$d = $a->foo()->foo()->foo();
每个方法调用都返回一个新对象,您可以在该对象上调用另一个方法。 “分解”的等效形式是:
$b = $a->foo();
$c = $b->foo();
$d = $c->foo();
现在,显然,您也可以循环执行此操作:
for ($i = 0; $i < 3; $i++) {
$a = $a->foo();
}