-3

I have a unexpected '->' (T_OBJECT_OPERATOR) error and I am not able to solve it:

 $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);

One idea was to change this line:

$form = ($this->createFormBuilder($item))

But this did not work out and I don't know what I could do

var_dump($classes) outputs this:

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) {
  }
}

for deceze♦:

I have an entity that is created by the mySQL database. Because I have 100 different entities (this means database tables) with different fields I want to create one function, that I can use depending on their content. So this is what I am trying to achieve:

  $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();

Depending on my input $slug, I want to create the fields in the form builder:

$EntityName = 'App\\Entity\\' . ucwords($slug);
$em = $this->getDoctrine()->getManager();
$cmf = $em->getMetadataFactory();
$classes = $cmf->getMetadataFor($EntityName);

This is why I want to get the metadata from the $classes object. The object offers me the fields that I need and with a foreach loop I want to build them

peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

1

What you're trying is too meta. You can't create PHP source code with a foreach that will then be executed as source code. What you want is probably something like this:

$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();

This is a method chain:

$d = $a->foo()->foo()->foo();

Each method call returns a new object, on which you call another method. The "decomposed", equivalent form of that is:

$b = $a->foo();
$c = $b->foo();
$d = $c->foo();

Now, obviously, you can do that in a loop too:

for ($i = 0; $i < 3; $i++) {
    $a = $a->foo();
}
deceze
  • 510,633
  • 85
  • 743
  • 889