Giter Site home page Giter Site logo

Using the Resource Attributes leads to error '"App\Entity\ModelClass" is not a valid entity or mapped super class. about syliusresourcebundle HOT 9 CLOSED

nkamuo avatar nkamuo commented on May 25, 2024
Using the Resource Attributes leads to error '"App\Entity\ModelClass" is not a valid entity or mapped super class.

from syliusresourcebundle.

Comments (9)

loic425 avatar loic425 commented on May 25, 2024

Thx for reporting the issue, could you share us your supplier repository?

from syliusresourcebundle.

nkamuo avatar nkamuo commented on May 25, 2024

Hello @loic425 , Thanks for your reply.

This is how the SupplyRepository looks

`

* * @method Supplier|null find($id, $lockMode = null, $lockVersion = null) * @method Supplier|null findOneBy(array $criteria, array $orderBy = null) * @method Supplier[] findAll() * @method Supplier[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class SupplierRepository extends ServiceEntityRepository implements RepositoryInterface { use ResourceRepositoryTrait; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Supplier::class); } // /** // * @return Supplier[] Returns an array of Supplier objects // */ // public function findByExampleField($value): array // { // return $this->createQueryBuilder('s') // ->andWhere('s.exampleField = :val') // ->setParameter('val', $value) // ->orderBy('s.id', 'ASC') // ->setMaxResults(10) // ->getQuery() // ->getResult() // ; // } // public function findOneBySomeField($value): ?Supplier // { // return $this->createQueryBuilder('s') // ->andWhere('s.exampleField = :val') // ->setParameter('val', $value) // ->getQuery() // ->getOneOrNullResult() // ; // } } `

from syliusresourcebundle.

nkamuo avatar nkamuo commented on May 25, 2024
namespace App\Repository\Inventory;

use App\Entity\Inventory\Supplier;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;
use Sylius\Component\Resource\Repository\RepositoryInterface;

/**
 * @extends ServiceEntityRepository<Supplier>
 *
 * @method Supplier|null find($id, $lockMode = null, $lockVersion = null)
 * @method Supplier|null findOneBy(array $criteria, array $orderBy = null)
 * @method Supplier[]    findAll()
 * @method Supplier[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class SupplierRepository extends ServiceEntityRepository implements RepositoryInterface
{
    use ResourceRepositoryTrait;
    
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Supplier::class);
    }

//    /**
//     * @return Supplier[] Returns an array of Supplier objects
//     */
//    public function findByExampleField($value): array
//    {
//        return $this->createQueryBuilder('s')
//            ->andWhere('s.exampleField = :val')
//            ->setParameter('val', $value)
//            ->orderBy('s.id', 'ASC')
//            ->setMaxResults(10)
//            ->getQuery()
//            ->getResult()
//        ;
//    }

//    public function findOneBySomeField($value): ?Supplier
//    {
//        return $this->createQueryBuilder('s')
//            ->andWhere('s.exampleField = :val')
//            ->setParameter('val', $value)
//            ->getQuery()
//            ->getOneOrNullResult()
//        ;
//    }
}

from syliusresourcebundle.

loic425 avatar loic425 commented on May 25, 2024

@nkamuo
I did'nt reproduce your issue in an empty symfony 6.3 project.
Do you have a routing with this entity?

For information:

# config/packages/sylius_resource.yaml
sylius_resource:
    mapping:
        paths:
            - '%kernel.project_dir%/src/Entity'
            
    resources:
        # You can remove this configuration, this is auto-registered now.
        #app.supplier:
        #    driver: doctrine/orm
        #    classes:
        #        model: App\Entity\Inventory\Supplier
#[Resource(alias: 'app.supplier')]

This alias is the default value.

from syliusresourcebundle.

nkamuo avatar nkamuo commented on May 25, 2024

Yes - I had the configuration and also specified the Resource entity explicitly.

# config/packages/sylius_resource.yaml
sylius_resource:
    mapping:
        paths:
            - "%kernel.project_dir%/src/Entity"

    resources:
        app.supplier:
            driver: doctrine/orm
            classes:
                model: App\Entity\Inventory\Supplier

        app.purchase_order:
            driver: doctrine/orm
            classes:
                model: App\Entity\Inventory\PurchaseOrder
                form: App\Form\Inventory\PurchaseOrderType

However, I was able to solve the issue by switching my entity metadata from PHP8 attributes to Doctrine annotations.
Now, everything works so well since switching to annotations.
Do you have a workaround for this.

Thanks for your help so far.

This is how my Supplier class looks now.

namespace App\Entity\Inventory;

use App\Repository\Inventory\SupplierRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Metadata\Resource;
use Sylius\Component\Resource\Model\ResourceInterface;// as ResourceMetadata;


// #[Resource(alias: 'app.supplier')]
/**
 * @ORM\Entity(repositoryClass=SupplierRepository::class)
 * @ORM\Table(name="app_inventory_supplier")
 */
class Supplier implements ResourceInterface
{
    
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column
     */
    private ?int $id = null;

    /**
     * @ORM\Column(length=32, unique=true)
     */
    private ?string $code = null;

    /**
     * @ORM\Column(length=64)
     */
    private ?string $name = null;

    /**
     * @ORM\Column(type=Types::TEXT,length=255, nullable=true)
     */
    // #[ORM\Column(type: Types::TEXT, length: 255, nullable: true)]
    private ?string $description = null;

    /**
     * @ORM\Column(type="boolean")
     */
    private ?bool $enabled = null;

    // #[ORM\OneToMany(mappedBy: 'supplier', targetEntity: PurchaseOrder::class)]
    /**
     * @ORM\OneToMany(targetEntity=PurchaseOrder::class, mappedBy="supplier")
     */
    private Collection $purchaseOrders;

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

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

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(?string $code): static
    {
        $this->code = $code;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }

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

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

        return $this;
    }

    public function isEnabled(): ?bool
    {
        return $this->enabled;
    }

    public function setEnabled(bool $enabled): static
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * @return Collection<int, PurchaseOrder>
     */
    public function getPurchaseOrders(): Collection
    {
        return $this->purchaseOrders;
    }

    public function addPurchaseOrder(PurchaseOrder $purchaseOrder): static
    {
        if (!$this->purchaseOrders->contains($purchaseOrder)) {
            $this->purchaseOrders->add($purchaseOrder);
            $purchaseOrder->setSupplier($this);
        }

        return $this;
    }

    public function removePurchaseOrder(PurchaseOrder $purchaseOrder): static
    {
        if ($this->purchaseOrders->removeElement($purchaseOrder)) {
            // set the owning side to null (unless already changed)
            if ($purchaseOrder->getSupplier() === $this) {
                $purchaseOrder->setSupplier(null);
            }
        }

        return $this;
    }
}

from syliusresourcebundle.

loic425 avatar loic425 commented on May 25, 2024

Have you configured Doctrine to work with attributes?

# config/packages/doctrine.yaml
doctrine:
    orm:
        auto_generate_proxy_classes: '%kernel.debug%'
        entity_managers:
            default:
                auto_mapping: true
                mappings:
                    App:
                        is_bundle: false
                        type: attribute
                        dir: '%kernel.project_dir%/src/Entity'
                        prefix: 'App\Entity'
                        alias: App

from syliusresourcebundle.

loic425 avatar loic425 commented on May 25, 2024

@nkamuo Is that fixed for you?

from syliusresourcebundle.

nkamuo avatar nkamuo commented on May 25, 2024

I was able to fix it by changing the type: attribute to type: annotation.

I think the default doctrine config on sylius does not specify the metadata type [attribute or annotation] but defaults to annotation.
So when I switched to attribute as you suggested, other Sylius entities[Product, Order, ProductVariant ...] become invisible to doctrine. I had to rewrite the generated entities to annotation and everything works so well now.

from syliusresourcebundle.

nkamuo avatar nkamuo commented on May 25, 2024

Thanks @loic425 for your support - You pointed me in the right direction.

from syliusresourcebundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.