Issues with EasyAdmin and Symfony relations One-to-Many

1.5k Views Asked by At

everyone.

I was having issues with Easy Admin and the one-to-many relationship handled by Symfony. The three entities that I currently have in place are User, Products, and Category.

When I place the relationship on Product and Category, I get this error when I try to add a new Category in the admin section of Easy Bundle:

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

Here are what my associates look like int the Product and Category classes, they are basically copied straight out of the Symfony docs.

The Product Category

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

And here is the association on the Category end:

...
use Doctrine\Common\Collections\ArrayCollection;

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

public function __construct()
{
    $this->products = new ArrayCollection();
}

The main issues that I believe is going on is that their is no field for category in the products table and this is leaving the Easy Bundle confused, since it seems to be relying on the class properties.

If anyone can make a suggestion on how to fix this, that would be great. Or if you know of a butter admin bundle to work with that might have this issues, that would also be great.

Also, if you have any experience with Easy Admin Bundle, do you suggest just developer just make their own. Because I do see other issue with this Bundle, such as being able to list category names on the Products new form, since Symfony only seems to log the category_id and not the name. I wouldn't be able to list the categories by name, just id numbers. And I would like to list of the different category names.

Any suggestions or help with this would be great.

1

There are 1 best solutions below

0
On

I just got this error as well. Just add a __toString() magic method to your Product entity:

class Product {
    ...

    public function __toString()
    {
        return $this->title; // <-- add here a real property which
    }                        //     describes your product