2015-12-09 13 views
10

A causa dei problemi che avevo con la versione 2.7 di symfony (errore di 404 pagine subito dopo l'installazione di un progetto) ho iniziato ad usare Symfony versione 3.0 . Dopo alcuni problemi minori ho capito che "app/console" è stata sostituita da "bin/console". Così sto lavorando ora su un nuovo progetto e ho già costruire un nuovo bundle con 1 soggetto chiamatoArgomento previsto di tipo "string", "Vendor NameBundle Form EntitynameType" dato Symfony 3.0

Codeit/RestaurantBundle && CodeitRestaurantBundle:Reserveren

Format è annotazioni, e l'entità ha un ID e 1 campo chiamato "naam "(stringa, 255). Ho aggiornato lo schema, ho generato le entità di Codeit e dopo che è stato fatto con successo ho generato un crud con azioni di scrittura. Il formato era nuovamente annotazione e il prefisso è/reserveren.

Quindi se visito la pagina web/reserveren sto ricevendo una pagina di presentazione della mia entità. Purtroppo se provo ad aggiungere una nuova voce che sto ottenendo il seguente errore:

Expected argument of type "string", "Codeit\RestaurantBundle\Form\ReserverenType" given

mio Bundle/Form/ReserverenType.php

<?php 

namespace Codeit\RestaurantBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class ReserverenType extends AbstractType 
{ 
/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('naam') 
    ; 
} 

/** 
* @param OptionsResolver $resolver 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Codeit\RestaurantBundle\Entity\Reserveren' 
    )); 
} 
} 

Il mio codice entità

<?php 

namespace Codeit\RestaurantBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Reserveren 
* 
* @ORM\Table(name="reserveren") 
* @ORM\Entity(repositoryClass="Codeit\RestaurantBundle\Repository\ReserverenRepository") 
*/ 
class Reserveren 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="Naam", type="string", length=255) 
    */ 
    private $naam; 


    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set naam 
    * 
    * @param string $naam 
    * 
    * @return Reserveren 
    */ 
    public function setNaam($naam) 
    { 
     $this->naam = $naam; 

     return $this; 
    } 

    /** 
    * Get naam 
    * 
    * @return string 
    */ 
    public function getNaam() 
    { 
     return $this->naam; 
    } 


} 

risposta

24

forme sono cambiate un po 'in 3.0. Per ora potresti stare meglio con 2.8.

Non lo diede a vedere, ma ho il sospetto che, in base al messaggio di errore, che il codice di controllo si presenta come:

$form = $this->createForm(new ReservernType(), $item); 

Questo è il modo 2.x di fare le cose. Per uso 3.x:

$form = $this->createForm(ReservernType::class, $item); 

http://symfony.com/doc/current/book/forms.html#creating-form-classes

+0

Grazie Cerad! Ha funzionato bene .. Riconsidererò di fare un nuovo progetto con una versione più conosciuta .. Grazie comunque! – Gijsberts

+4

Non c'è niente di sbagliato nell'usare 3.x per un nuovo progetto. Basta essere consapevoli del fatto che un bel po 'delle informazioni S2 là fuori non si applica più. – Cerad

+1

molte grazie, stava impazzendo sul perché il modulo non funzionava e ora lo so. – Baig

1

Prova come this:

/.../ 

use Symfony\Component\Form\Extension\Core\Type\TextType; 

/.../ 

class ReserverenType extends AbstractType 
{ 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
      $builder->add('naam', TextType::class); 
    } 
} 
-1

aggiungi questo alla classe della tua entità sostituisci il nome con un attributo che ti si addice.

public function __toString() { 
    return $this->name; 
} 
+0

Quindi, a mio Bundle/Entità/Come prenotare.php Devo aggiungere il codice in fondo alla classe, e devo sostituire 'return $ this-> name;' in 'return $ this-> naam'? Perché non funziona sfortunatamente .. :( – Gijsberts

+0

Puoi pubblicare il codice della tua entità? –

+0

Sì, è ora nella mia domanda di cui sopra! – Gijsberts

1

provare con:

$builder 
     ->add('naam', TextType::class); 
     // If you use PHP 5.3 or 5.4 you must use 
     // ->add('naam','Symfony\Component\Form\Extension\Core\Type\TextType') 

invece di questo

$builder 
    ->add('naam'); 

e aggiungere la use dichiarazione:

use Symfony\Component\Form\Extension\Core\Type\TextType; 

motivazione: da il upgrade guide:

Type names were deprecated and will be removed in Symfony 3.0. Instead of referencing types by name, you should reference them by their fully-qualified class name (FQCN) instead. With PHP 5.5 or later, you can use the "class" constant for that:

Spero che questo aiuto

+0

Sfortunatamente non .. – Gijsberts

+0

Ora capisco con la risposta di @cerad che il campo che inserisco è Field Type Guessing (non è necessario specificare il tipo) ma il modulo principale è necessario dichiarare il (classe) tipo – Matteo

+0

Sì, immagino di sì, comunque, grazie per la tua risposta! :) – Gijsberts