Could not determine access type for property "category" in class "AppEntityAdminProducts" Error

Could not determine access type for property “category” in class “AppEntityAdminProducts”: The property “category” in class “AppEntityAdminProducts” can be defined with the methods “addCategory()”, “removeCategory()” but the new value must be an array or an instance of Traversable, “AppEntityAdminCategory” given.

I added some extra code new and edit functions of categorycontroller.php does the code effect product.php

AppEntityAdminProducts

<?php

namespace AppEntityAdmin;

use AppEntityAdmincategory;
use AppEntityAdminpublisher;
use AppEntityAdminusers;
use AppEntityAdminwriters;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass="AppRepositoryAdminProductsRepository")
 */
class Products
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=150, nullable=true)
     */
    private $title;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $keywords;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $description;

    /**
     * @ORMOneToMany(targetEntity="AppEntityAdminTypes", mappedBy="products")
     */
    private $type;

    /**
     * @ORMManyToMany(targetEntity="AppEntityAdminPublisher", inversedBy="products")
     */
    private $publisher;

    /**
     * @ORMColumn(type="integer", nullable=true)
     */
    private $year;

    /**
     * @ORMColumn(type="integer", nullable=true)
     */
    private $amount;

    /**
     * @ORMColumn(type="float", nullable=true)
     */
    private $pprice;

    /**
     * @ORMColumn(type="float", nullable=true)
     */
    private $sprice;

    /**
     * @ORMColumn(type="integer", nullable=true)
     */
    private $min;

    /**
     * @ORMColumn(type="text", nullable=true)
     */
    private $detail;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @ORMManyToOne(targetEntity="AppEntityAdminWriters", inversedBy="products")
     */
    private $writer;

    /**
     * @ORMManyToMany(targetEntity="AppEntityAdminCategory", inversedBy="products")
     */
    private $category;

    /**
     * @ORMManyToOne(targetEntity="AppEntityAdminUsers", inversedBy="products")
     */
    private $user;

    /**
     * @ORMColumn(type="string", length=10, nullable=true)
     */
    private $status;

    public function __construct()
    {
        $this->type = new ArrayCollection();
        $this->publisher = new ArrayCollection();
        $this->category = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getKeywords(): ?string
    {
        return $this->keywords;
    }

    public function setKeywords(?string $keywords): self
    {
        $this->keywords = $keywords;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): self
    {
        $this->description = $description;

        return $this;
    }

    /**
     * @return Collection|types[]
     */
    public function getType(): Collection
    {
        return $this->type;
    }

    public function addType(types $type): self
    {
        if (!$this->type->contains($type)) {
            $this->type[] = $type;
            $type->setProducts($this);
        }

        return $this;
    }

    public function removeType(types $type): self
    {
        if ($this->type->contains($type)) {
            $this->type->removeElement($type);
            // set the owning side to null (unless already changed)
            if ($type->getProducts() === $this) {
                $type->setProducts(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection|publisher[]
     */
    public function getPublisher(): Collection
    {
        return $this->publisher;
    }

    public function addPublisher(publisher $publisher): self
    {
        if (!$this->publisher->contains($publisher)) {
            $this->publisher[] = $publisher;
        }

        return $this;
    }

    public function removePublisher(publisher $publisher): self
    {
        if ($this->publisher->contains($publisher)) {
            $this->publisher->removeElement($publisher);
        }

        return $this;
    }

    public function getYear(): ?int
    {
        return $this->year;
    }

    public function setYear(?int $year): self
    {
        $this->year = $year;

        return $this;
    }

    public function getAmount(): ?int
    {
        return $this->amount;
    }

    public function setAmount(?int $amount): self
    {
        $this->amount = $amount;

        return $this;
    }

    public function getPprice(): ?float
    {
        return $this->pprice;
    }

    public function setPprice(?float $pprice): self
    {
        $this->pprice = $pprice;

        return $this;
    }

    public function getSprice(): ?float
    {
        return $this->sprice;
    }

    public function setSprice(?float $sprice): self
    {
        $this->sprice = $sprice;

        return $this;
    }

    public function getMin(): ?int
    {
        return $this->min;
    }

    public function setMin(?int $min): self
    {
        $this->min = $min;

        return $this;
    }

    public function getDetail(): ?string
    {
        return $this->detail;
    }

    public function setDetail(?string $detail): self
    {
        $this->detail = $detail;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(?string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getWriter(): ?writers
    {
        return $this->writer;
    }

    public function setWriter(?writers $writer): self
    {
        $this->writer = $writer;

        return $this;
    }

    /**
     * @return Collection|category[]
     */
    public function getCategory(): Collection
    {
        return $this->category;
    }

    public function addCategory(category $category): self
    {
        if (!$this->category->contains($category)) {
            $this->category[] = $category;
        }

        return $this;
    }

    public function removeCategory(category $category): self
    {
        if ($this->category->contains($category)) {
            $this->category->removeElement($category);
        }

        return $this;
    }

    public function getUser(): ?users
    {
        return $this->user;
    }

    public function setUser(?users $user): self
    {
        $this->user = $user;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(?string $status): self
    {
        $this->status = $status;

        return $this;
    }
    public function __toString() {
        return $this->title;
    }
}

AppFormAdminProductType

<?php

namespace AppFormAdmin;

use AppEntityAdminCategory;
use AppEntityAdminProducts;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeFileType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentValidatorConstraintsFile;

class ProductsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('keywords')
            ->add('description')
            ->add('year')
            ->add('amount')
            ->add('pprice')
            ->add('sprice')
            ->add('min')
            ->add('detail')
            ->add('image', FileType::class, [
                'label' => 'Product Image',
                'mapped' => false,
                'required' => false,

                'constraints' => [
                    new File([
                        'maxSize' => '1024k',
                        'mimeTypes' => ['image/*'
                        ],
                        'mimeTypesMessage' => 'Please upload a valid image file',
                    ])
                ],
            ])
            ->add('status')
            ->add('publisher')
            ->add('writer')
            ->add('category',EntityType::class,[
                'class'=> Category::class,
                'choice_label'=>'title'
            ])
            ->add('user')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Products::class,
        ]);
    }
}

AppEntityAdminCategory.php

<?php

namespace AppEntityAdmin;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass="AppRepositoryAdminCategoryRepository")
 */
class Category
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="integer", nullable=true)
     */
    private $parentid;

    /**
     * @ORMColumn(type="string", length=150, nullable=true)
     */
    private $title;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $keywords;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $decription;

    /**
     * @ORMColumn(type="string", length=80, nullable=true)
     */
    private $image;

    /**
     * @ORMColumn(type="string", length=10, nullable=true)
     */
    private $status;

    /**
     * @ORMColumn(type="datetime", nullable=true)
     */
    private $created_at;

    /**
     * @ORMColumn(type="datetime", nullable=true)
     */
    private $updated_at;

    /**
     * @ORMManyToMany(targetEntity="AppEntityAdminProducts", mappedBy="category")
     */
    private $products;

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

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getParentid(): ?int
    {
        return $this->parentid;
    }

    public function setParentid(?int $parentid): self
    {
        $this->parentid = $parentid;

        return $this;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(?string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getKeywords(): ?string
    {
        return $this->keywords;
    }

    public function setKeywords(?string $keywords): self
    {
        $this->keywords = $keywords;

        return $this;
    }

    public function getDecription(): ?string
    {
        return $this->decription;
    }

    public function setDecription(?string $decription): self
    {
        $this->decription = $decription;

        return $this;
    }

    public function getImage(): ?string
    {
        return $this->image;
    }

    public function setImage(?string $image): self
    {
        $this->image = $image;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(?string $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getCreatedAt(): ?DateTimeInterface
    {
        return $this->created_at;
    }

    public function setCreatedAt(?DateTimeInterface $created_at): self
    {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt(): ?DateTimeInterface
    {
        return $this->updated_at;
    }

    public function setUpdatedAt(?DateTimeInterface $updated_at): self
    {
        $this->updated_at = $updated_at;

        return $this;
    }

    /**
     * @return Collection|Products[]
     */
    public function getProducts(): Collection
    {
        return $this->products;
    }

    public function addProduct(Products $product): self
    {
        if (!$this->products->contains($product)) {
            $this->products[] = $product;
            $product->addCategory($this);
        }

        return $this;
    }

    public function removeProduct(Products $product): self
    {
        if ($this->products->contains($product)) {
            $this->products->removeElement($product);
            $product->removeCategory($this);
        }

        return $this;
    }
    public function __toString() {
        return $this->title;
    }
}

AppControllerAdminCategoryController.php

<?php

namespace AppControllerAdmin;

use AppEntityAdminCategory;
use AppFormAdminCategoryType;
use AppRepositoryAdminCategoryRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;

/**
 * @Route("/admin/category")
 */
class CategoryController extends AbstractController
{
    /**
     * @Route("/", name="admin_category_index", methods={"GET"})
     */
    public function index(CategoryRepository $categoryRepository): Response
    {
        return $this->render('admin/category/index.html.twig', [
            'categories' => $categoryRepository->findAll(),
        ]);
    }

    /**
     * @Route("/new", name="admin_category_new", methods={"GET","POST"})
     */
    public function new(Request $request, CategoryRepository $categoryRepository): Response
    {
        $category = new Category();
        $form = $this->createForm(CategoryType::class, $category);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($category);
            $entityManager->flush();

            return $this->redirectToRoute('admin_category_index');
        }
        $categories = $categoryRepository->findAll();
        return $this->render('admin/category/new.html.twig', [
            'categories'=>$categories,
            'category' => $category,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="admin_category_show", methods={"GET"})
     */
    public function show(Category $category): Response
    {
        return $this->render('admin/category/show.html.twig', [
            'category' => $category,
        ]);
    }

    /**
     * @Route("/{id}/edit", name="admin_category_edit", methods={"GET","POST"})
     */
    public function edit(Request $request, Category $category,CategoryRepository $categoryRepository): Response
    {
        $form = $this->createForm(CategoryType::class, $category);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('admin_category_index');
        }
        $categories = $categoryRepository->findAll();
        return $this->render('admin/category/edit.html.twig', [
            'categories'=>$categories,
            'category' => $category,
            'form' => $form->createView(),
        ]);
    }

    /**
     * @Route("/{id}", name="admin_category_delete", methods={"DELETE"})
     */
    public function delete(Request $request, Category $category): Response
    {
        if ($this->isCsrfTokenValid('delete' . $category->getId(), $request->request->get('_token'))) {
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->remove($category);
            $entityManager->flush();
        }

        return $this->redirectToRoute('admin_category_index');
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *