PHP code example of marty / mcfly

1. Go to this page and download the library: Download marty/mcfly library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marty / mcfly example snippets



// src/DataFixtures/CompanyFixtures.php

namespace App\DataFixtures;

use Doctrine\Persistence\ObjectManager;
use Marty\McFly\Fixture;

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // $product = new Product();
        // $manager->persist($product);

        $manager->flush();
    }
}


// src/DataFixtures/CompanyFixtures.php

namespace App\DataFixtures;

use Doctrine\Persistence\ObjectManager;
use Marty\McFly\Fixture;

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // $this->generate();

        $manager->flush();
    }

    public function generate(?array $properties = null, array|string|null $references = null): object
    {
        // TODO: Implement generate() method.
        throw new \RuntimeException("The generate() method is not implemented.");
    }
}


// src/DataFixtures/CompanyFixtures.php

namespace App\DataFixtures;

use App\Entity\Company;
use Doctrine\Persistence\ObjectManager;
use Marty\McFly\Fixture;

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // Create a random Company, persist it (for database), and automatically add a reference to use it in other fixtures.
        $this->generate();

        // Create 10 randoms Companies to "Paris" (Since 2.0)
        $this->generateMany(10, [
            'city' => 'Paris',
            'postalCode' => '75000'
        ]);

        // Finally, flush to the database
        $manager->flush();
    }

    public function generate(?array $properties = null, array|string|null $references = null): object
    {
        return $this->createAndSave(Company::class, $properties,
            [
                'name'       => self::$faker->company(),
                'address'    => self::$faker->address(),
                'city'       => self::$faker->city(),
                'postalCode' => self::$faker->postcode(),
            ],
            $references
        );
    }

}


// src/DataFixtures/CompanyFixtures.php

namespace App\DataFixtures;

use App\Entity\Company;
use Doctrine\Persistence\ObjectManager;
use Marty\McFly\Fixture;

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // Create a random Company, persist it (for database), and automatically add a reference to use it in other fixtures.
        $this->generate();

        // Alternative : Create 10 randoms Companies to "Paris" with setter
        for ($i=0 ; $i<10 ; $i++) {
            $this->generate()
                ->setCity('Paris')
                ->setPostalCode('75000');
        }

        // Finally, flush to the database
        $manager->flush();
    }

    public function generate(?array $properties = null, array|string|null $references = null): object
    {
        $company = (new Company())
            ->setName(self::getFaker()->company())
            ->setAddress(self::getFaker()->address())
            ->setCity(self::getFaker()->city())
            ->setPostalCode(self::getFaker()->postcode());

        $this->save($company, $references);

        return $company;
    }
}



// ...

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // Create a random Company
        $this->generate();

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}




// ...

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // Create a random Company to "Paris"
        $this->generate([
            'city'       => 'Paris',
            'postalCode' => '75000',
        ]);

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}



// ...

class CompanyFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // Create a random Company to "Paris"
        $this->generate()
            ->setCity('Paris')
            ->setPostalCode('75000');

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}



// ...

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

        // Create 10 random Company to "Paris"
        $this->generateMany(10, [
            'city'       => 'Paris',
            'postalCode' => '75000',
        ]);

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}




// ...

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

        // Create 10 random Company to "Paris"
        $this->generateMany(10, function() {
            return [
                'city'       => 'Paris',
                'postalCode' => self::$faker->randomElement(['75000', '75100', '75200']),
            ];
        });

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}




// ...

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

        // Create 10 random Company to "Paris"
        for ($i=0 ; $i<10 ; $i++) {
            $this->generate()
                ->setCity('Hill Valley')
            ;
        }

        // Finally, flush to the database
        $manager->flush();
    }

    // ... generate() definition
}





namespace App\DataFixtures;

use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Marty\McFly\Fixture;
use App\Entity\Company;

class UserFixture extends Fixture implements DependentFixtureInterface
{

    public function load(ObjectManager $manager): void
    {
        // Use this function to loop through all companies and create users for them.
        /** @var array<Company> $companies */
        $companies = $this->getReferencesByClass(Company::class);

        // Need a random company? Use this method.
        /** @var Company $company */
        $company = $this->getRandomReferenceByClass(Company::class);

    }

    public function create(string|array $references = null): User
    {
        // You can also use it in the template to add a default random company.
        /** @var Company $company */
        $company = $this->getRandomReferenceByClass(Company::class);

        // ..
    }

    public function getDependencies(): array
    {
        return [
            CompanyFixture::class,
        ];
    }
}


// --

class InvoiceFixture extends Fixture implements DependentFixtureInterface
{

    // --

    public function generate(?array $properties = null, array|string|null $references = null): Invoice
    {
        $invoice = $this->createAndSave(Invoice::class, $properties,
            [
                'createdAt' => new DateTimeImmutable(),
                'number'    => InvoiceFixture::count(),                                 // the current number, auto-incrementation on save()
                'user'      => $this->getRandomReferenceByClass(User::class),           // a random User
                'status'    => self::randomValue(Status::cases()),                      // randomly a value of Status Enum,
                'confirmed' => self::randomValue([True, False]),                        // randomly True or False
                'comment'   => self::randomValue([null, self::getFaker()->sentence()]), // Add random sentence or randomly NULL
                'product'
            ],
            $references
        );

        // Add RandomProduct
        for ($i=0 ; $i<10 ; $i++) {
            /** @var Product $product */
            $randomProduct = $this->getRandomReferenceByClass(Product::class);
            $invoice->addProduct($randomProduct);
        }

        return $invoice;
    }

    // --
}