简体   繁体   中英

Adding items under a specific category using Sonata Admin

Using Symfony2, i followed a tuto how to make sonata admin bundle to manage products in a shop, it's working fine, i can add, delete, modify each product (each product contain these colums: id, name, price, quantity) Now my question how can i add categories for these products? In sonata admin I want to be able to select a defined category before adding a product into it (kind of drop-down-menu, is it possible?) so i can easly manage products. Also I want to be able to manage categories from the same admin dashboard (add/remove/modify).

(I just started using Symfony2, I'm a beginner I need a clear explanation & thank you!)

You will need a Category entity, and a CategoryAdmin class. Then in your product Entity, you will be able to add a OneToMany link , something like :

class Project
{
    /**
    * @ORM\OneToMany(targetEntity="Category", mappedBy="categories")
    * @ORM\OrderBy({"id" = "ASC"})
    */
    private $categories;
}

Then in your admin, you'll need to have a look on the sonata_type_collection , which allow one-to-many connection :

The Collection Type is meant to handle creation and editing of model collections. Rows can be added and deleted, and your model abstraction layer may allow you to edit fields inline. You can use type_options to pass values to the underlying forms.

Good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM