178 search results

…read this and didn't get a new `make:factory` command. I think the new recipes (v1.10+) are not compatible with Symfony 5.1.x. Make sure you register the bundle manually. `# config/bundles.php return [ // ... Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true…
julien_bonnier
julien_bonnier
Read Full Comment
Hello. In some previous tutorials, you presented us other library for the fixture called Alice Bundle. I wonder beacause this tuto is newer (Symfony 5) so this bundle zenstruck/foundry is better and I should use this instead for new project? Thanks
…symfony 5.0 and php 7.3. I've worked through some issues with Foundry (had to update to php74 to get a version that actually has the make:factory command) and at the point where AsciiSlugger() was used, I had to `composer require symfony…
61 lines | src/Factory/UserFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\User;
use App\Repository\UserRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
// ... lines 10 - 28
final class UserFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
// TODO inject services if required (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services)
}
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories)
'email' => self::faker()->text(),
'roles' => [],
'firstName' => self::faker()->text(),
];
}
protected function initialize(): self
{
// see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
return $this
// ->afterInstantiate(function(User $user) {})
;
}
protected static function getClass(): string
{
return User::class;
}
}
See Code Block in Script
59 lines | src/Factory/QuestionTagFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\QuestionTag;
use App\Repository\QuestionTagRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
// ... lines 11 - 28
final class QuestionTagFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
// TODO inject services if required (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services)
}
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories)
'taggedAt' => self::faker()->datetime(),
];
}
protected function initialize(): self
{
// see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
return $this
// ->afterInstantiate(function(QuestionTag $questionTag) {})
;
}
protected static function getClass(): string
{
return QuestionTag::class;
}
}
See Code Block in Script
61 lines | src/Factory/TagFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\Tag;
use App\Repository\TagRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
// ... lines 11 - 28
final class TagFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
// TODO inject services if required (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services)
}
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories)
'name' => self::faker()->text(),
'createdAt' => null, // TODO add DATETIME ORM type manually
'updatedAt' => null, // TODO add DATETIME ORM type manually
];
}
protected function initialize(): self
{
// see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
return $this
// ->afterInstantiate(function(Tag $tag) {})
;
}
protected static function getClass(): string
{
return Tag::class;
}
}
See Code Block in Script
…and Foundry make it easy, and here is how you use Foundry with Doctrine relations". This chapter really just focuses on covering one edge-case when people use Foundry with relations (that's this part here: https://symfonycasts.com/screencast/doctrine-relations/foundry-factory-relation…
weaverryan
weaverryan
Read Full Comment
62 lines | src/Factory/AnswerFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\Answer;
use App\Repository\AnswerRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
/**
* @extends ModelFactory<Answer>
*
// ... lines 14 - 27
*/
final class AnswerFactory extends ModelFactory
{
public function __construct()
{
parent::__construct();
// TODO inject services if required (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services)
}
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories)
'content' => self::faker()->text(),
'username' => self::faker()->text(),
'createdAt' => null, // TODO add DATETIME ORM type manually
'updatedAt' => null, // TODO add DATETIME ORM type manually
];
}
protected function initialize(): self
{
// see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
return $this
// ->afterInstantiate(function(Answer $answer) {})
;
}
protected static function getClass(): string
{
return Answer::class;
}
}
See Code Block in Script
Is there a mistake in the course description then? One of the topics is "Handling relationships in fixtures with Foundry"
…forward to it! These doctrine courses are the reason I got a subscription Sweet! > my implementation of foundry for testing is incredibly hacky We won't be talking about Foundry and testing in this tutorial. But if you have any specific questions, I'd…
weaverryan
weaverryan
Read Full Comment
Looking forward to it! These doctrine courses are the reason I got a subscription. I'm currently working on a massive warehouse management database and my implementation of foundry for testing is incredibly hacky
Thanks for all your help so far. Do you maybe know on which course / chapter I can learn about Foundry? I can not remember if it was already in one of the Symfony 5 courses. Foundry sounds fimiliar though.
… Excellent questions! > First one: I'd like to create dummy users and I've got a password property I had a long conversation about this with Foundry's creator Kevin. The problem (and it's kind of by design) is that the factories are…
weaverryan
weaverryan
Read Full Comment
Thanks Ryan! I guess we can say that it is better to use one or the other based on your own implementation or use case, which speaks really well about Foundry.
Ismael A.
Ismael A.
Read Full Comment
… Actually, I would say that *your* way probably has more benefits in general, because you're using more of Foundry's features - I just hadn't thought of it! Using afterInstantiate() gives you more power (e.g. you could call any method on your entity…
weaverryan
weaverryan
Read Full Comment
Hey Sean, I just double-checked it: download the course code, goes to the start/ directory, require orm and zenstruck/foundry packs and it worked for me. So, not sure why exactly you hit that error, probably a new version was released that conflicted with…
Hello again. I swear Ill make it through one of these without running into an issue. Tonight suddenly started getting an error running "composer require zenstruck/foundry --dev", which is weird because I installed it a few days ago.....I dunno. If anyone else is…
54 lines | src/Factory/QuestionFactory.php
// ... lines 1 - 6
use Symfony\Component\String\Slugger\AsciiSlugger;
// ... lines 8 - 20
final class QuestionFactory extends ModelFactory
{
// ... lines 23 - 35
protected function initialize(): self
{
// see https://github.com/zenstruck/foundry#initialization
return $this
->afterInstantiate(function(Question $question) {
if (!$question->getSlug()) {
$slugger = new AsciiSlugger();
// ... line 43
}
})
;
}
// ... lines 48 - 52
}
See Code Block in Script
54 lines | src/Factory/QuestionFactory.php
// ... lines 1 - 6
use Symfony\Component\String\Slugger\AsciiSlugger;
// ... lines 8 - 20
final class QuestionFactory extends ModelFactory
{
// ... lines 23 - 35
protected function initialize(): self
{
// see https://github.com/zenstruck/foundry#initialization
return $this
->afterInstantiate(function(Question $question) {
if (!$question->getSlug()) {
$slugger = new AsciiSlugger();
$question->setSlug($slugger->slug($question->getName()));
}
})
;
}
// ... lines 48 - 52
}
See Code Block in Script
42 lines | src/Factory/QuestionFactory.php
// ... lines 1 - 2
namespace App\Factory;
use App\Entity\Question;
use App\Repository\QuestionRepository;
use Zenstruck\Foundry\RepositoryProxy;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
/**
* @method static Question|Proxy findOrCreate(array $attributes)
* @method static Question|Proxy random()
* @method static Question[]|Proxy[] randomSet(int $number)
* @method static Question[]|Proxy[] randomRange(int $min, int $max)
* @method static QuestionRepository|RepositoryProxy repository()
* @method Question|Proxy create($attributes = [])
* @method Question[]|Proxy[] createMany(int $number, $attributes = [])
*/
final class QuestionFactory extends ModelFactory
{
protected function getDefaults(): array
{
return [
// TODO add your default values here (https://github.com/zenstruck/foundry#model-factories)
];
}
protected function initialize(): self
{
// see https://github.com/zenstruck/foundry#initialization
return $this
// ->beforeInstantiate(function(Question $question) {})
;
}
protected static function getClass(): string
{
return Question::class;
}
}
See Code Block in Script