zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
607 stars 62 forks source link

Zenstruck\Foundry\Factory::$class must not be accessed before initialization #637

Closed dwcs-2324 closed 1 week ago

dwcs-2324 commented 1 week ago

Hi, I'm having this issue with name": "zenstruck/foundry" "version": "v1.38.0": Typed property Zenstruck\Foundry\Factory::$class must not be accessed before initialization

Could anyone help me?

Thank you in advance

nikophil commented 1 week ago

Hi, could you please share more context? at least the stack trace would be helpful

dwcs-2324 commented 1 week ago

Do you mean this?

C:\Users\project> php bin/console doctrine:fixtures:load

Careful, database "api_presupuesto_db/" will be purged. Do you want to continue? (yes/no) [no]:

yes

purging database loading App\DataFixtures\AppFixtures loading App\DataFixtures\BudgetFixtures string(4412) "#0 C:\Users\project\vendor\zenstruck\foundry\src\Persistence\PersistentProxyObjectFactory.php(77): Zenstruck\Foundry\Factory->create(Array, false)

1 C:\Users\project\vendor\zenstruck\foundry\src\FactoryCollection.php(94): Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory->create(Array, false)

2 C:\Users\project\vendor\zenstruck\foundry\src\Persistence\PersistentProxyObjectFactory.php(45): Zenstruck\Foundry\FactoryCollection->create(Array, false)

3 C:\Users\project\src\DataFixtures\BudgetFixtures.php(17): Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory::__callStatic('createMany', Array)

4 C:\Users\project\vendor\doctrine\data-fixtures\src\Executor\AbstractExecutor.php(122): App\DataFixtures\BudgetFixtures->load(Object(ContainerUSbqKi5\EntityManagerGhost51e8656))

5 C:\Users\project\vendor\doctrine\data-fixtures\src\Executor\ORMExecutor.php(26): Doctrine\Common\DataFixtures\Executor\AbstractExecutor->load(Object(ContainerUSbqKi5\EntityManagerGhost51e8656), Object(App\DataFixtures\BudgetFixtures))

6 C:\Users\project\vendor\doctrine\orm\src\EntityManager.php(182): Doctrine\Common\DataFixtures\Executor\ORMExecutor::Doctrine\Common\DataFixtures\Executor{closure}(Object(ContainerUSbqKi5\EntityManagerGhost51e8656))

7 C:\Users\project\vendor\doctrine\data-fixtures\src\Executor\ORMExecutor.php(20): Doctrine\ORM\EntityManager->wrapInTransaction(Object(Closure))

8 C:\Users\project\vendor\doctrine\doctrine-fixtures-bundle\src\Command\LoadDataFixturesDoctrineCommand.php(140): Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute(Array, false)

9 C:\Users\project\vendor\doctrine\doctrine-fixtures-bundle\src\Command\CommandCompatibility.php(32): Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->doExecute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

10 C:\Users\project\vendor\symfony\console\Command\Command.php(312): Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

11 C:\Users\project\vendor\symfony\console\Application.php(1040): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

12 C:\Users\project\vendor\symfony\framework-bundle\Console\Application.php(88): Symfony\Component\Console\Application->doRunCommand(Object(Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

13 C:\Users\project\vendor\symfony\console\Application.php(314): Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand(Object(Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

15 C:\Users\project\vendor\symfony\console\Application.php(168): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

16 C:\Users\project\vendor\symfony\runtime\Runner\Symfony\ConsoleApplicationRunner.php(54): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

17 C:\Users\project\vendor\autoload_runtime.php(29): Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run()

18 C:\Users\project\bin\console(15): require_once('C:\Users\...')

This same procedure used to work a month ago, but now it doesn't...

By the way if I modify Vendor > zenstruck > foundry > src> Factory.php and initialize the $class attribute to the exact name of the Entity I want to create, it works...

Any help will be appreciated.

nikophil commented 1 week ago

I can't understand how this could be possible.

could you share BudgetFixtures as well as your factory's code please?

dwcs-2324 commented 1 week ago

BudgetFixtures.php:

<?php

    namespace App\DataFixtures;  

    use Doctrine\Bundle\FixturesBundle\Fixture;  
    use Doctrine\Persistence\ObjectManager;  
        use App\Factory\BudgetFactory;

        class BudgetFixtures extends Fixture
        {
            public function load(ObjectManager $manager): void
            {

                BudgetFactory::createMany(5);
              //  $manager->flush();
            }
        }

BudgetFactory.php:

 <?php

namespace App\Factory; 

use App\Entity\Budget;
use App\Repository\BudgetRepository;
use Doctrine\ORM\EntityRepository;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Persistence\ProxyRepositoryDecorator;

/**
 * @extends PersistentProxyObjectFactory<Budget>
 *
 * @method        Budget|Proxy create(array|callable $attributes = [])
 * @method static Budget|Proxy createOne(array $attributes = [])
 * @method static Budget|Proxy find(object|array|mixed $criteria)
 * @method static Budget|Proxy findOrCreate(array $attributes)
 * @method static Budget|Proxy first(string $sortedField = 'id')
 * @method static Budget|Proxy last(string $sortedField = 'id')
 * @method static Budget|Proxy random(array $attributes = [])
 * @method static Budget|Proxy randomOrCreate(array $attributes = [])
 * @method static Budget|ProxyRepositoryDecorator repository()
 * @method static Budget[]|Proxy[] all()
 * @method static Budget[]|Proxy[] createMany(int $number, array|callable $attributes = [])
 * @method static Budget[]|Proxy[] createSequence(iterable|callable $sequence)
 * @method static Budget[]|Proxy[] findBy(array $attributes)
 * @method static Budget[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
 * @method static Budget[]|Proxy[] randomSet(int $number, array $attributes = [])
 */  
class BudgetFactory extends PersistentProxyObjectFactory{
    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
     *
     * @todo inject services if required
     */
    public function __construct()
    {
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
     */
    protected function initialize(): static
    {
        return $this
            // ->afterInstantiate(function(Budget $presupuesto): void {})
        ;
    }

    public static function class(): string
    {
        return Budget::class;
    }

    /**
     * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
     *
     * @todo add your default values here
     */
    protected function defaults(): array|callable
    {
        return [
            'Nombre' => self::faker()->text(255),
            'Total' => self::faker()->randomFloat(),

        ];
    }
}

Thank you, again.

nikophil commented 1 week ago

hey @dwcs-2324

this is fixed and released in v1.38.1. could you confirm?

nikophil commented 1 week ago

I'm closing this, feel free to reopen it if you still have a problem around this