2017-11-12 3 views
0

Ich verfolge die symfony documentation for associations und ich erstellt Produkt- und Kategorieentitäten.Symfony3 Eins-zu-viele-Beziehung __toString-Fehler

In meinem Artikel Einheit habe ich:

/** 
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 
* @ORM\JoinColumn(name="category_id", referencedColumnName="id") 
*/ 
private $category; 

In meiner Kategorie Einheit habe ich:

/** 
* @ORM\OneToMany(targetEntity="Product", mappedBy="category") 
*/ 
private $products; 

Hier ist, wie die generierten Tabellen wie folgt aussehen:

mysql> show create table product \G 
*************************** 1. row *************************** 
     Table: product 
Create Table: CREATE TABLE `product` (
    `id` bigint(20) unsigned NOT NULL, 
    `category_id` int(10) unsigned DEFAULT NULL, 
    `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `details` text COLLATE utf8_unicode_ci COMMENT '(DC2Type:json_array)', 
    PRIMARY KEY (`id`), 
    KEY `IDX_D34A04AD12469DE2` (`category_id`), 
    CONSTRAINT `FK_D34A04AD12469DE2` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 
1 row in set (0.00 sec) 

mysql> show create table category \G 
*************************** 1. row *************************** 
     Table: category 
Create Table: CREATE TABLE `category` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `parent_id` int(10) unsigned DEFAULT NULL, 
    `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 
1 row in set (0.00 sec) 

mysql> 

ich auch das EasyAdmin-Paket installiert. Jetzt gehe ich zu EasyAdmin und öffne 'Category' Tabelle und füge eine Zeile ein. Dann habe ich auf ‚Produkt‘ Tisch gehen und wenn ich klicken Sie auf ‚Produkt hinzufügen‘ klicken, erhalte ich die Fehlermeldung:

Catchable Fatal Error: Object of class AppBundle\Entity\Category could not be converted to string 

Es ist möglich, das Problem zu beheben, indem Sie eine magische Methode __toString Zugabe, den $ this- zurück> Name in Kategorieentität.

Frage:

1- Mache ich etwas falsch? Warum muss ich der Kategorieentität die __toString-Methode hinzufügen?

2- Wenn das normal ist, warum wird das im Symfony Handbuch nicht erwähnt?

Antwort

1

Kein Zweifel, dass dies das normale Verhalten ist. Ich habe es schon oft mit EasyAdminbundle gemacht.

Sie müssen die __toString() Methode implementieren.

Fields that represent an association with another entity are displayed as lists. For that reason, you must define the __toString() PHP method in any entity which is used in a Doctrine relation. Otherwise you'll see an error message because the backend cannot represent the related object as a string.

Es ist im doc hier: https://symfony.com/doc/current/bundles/EasyAdminBundle/book/edit-new-configuration.html

+0

Ja, das habe ich nicht sehen. Aber jetzt denke ich, dass der bessere Weg für mich darin besteht, Autocomplete für dieses Feld zu deaktivieren. Weil die Anzahl der Datensätze in der Beziehung möglicherweise vielleicht überwältigend ist. –

+0

Wenn schon für Projekte gearbeitet, in denen die