In case someone runs into the same problem... Since I'm taking this screencast after 2 years from its release, I had a lot of dependencies errors while loading composer repositories with
composer require hautelook/alice-bundle
I updated it with the particular version that was valid at that time (0.1.5), and it worked:
hello ? I think I've been using a different thing ? Didn't have a problem with it yet , the nelmio/Alice one ? Are they related ?? Another question I'm having , was looking through the documentation and not sure is it possible to select a random relationship like this ?
hautelook/alice-bundle is a bundle for standalone nelmio/alice library, which ports this lib and another one (fzaninotto/Faker) for the Symfony applications. If you see its dependencies - you will find nelmio/alice. So if you're on a Symfony app, you probably want to use this bundle instead of standalone library.
And yes, it's still possible to use random relationships (using wildcard ) in new version 2.x. Do you have any errors using it? Try to wrap it with quotes like `place: '@place_'`.
Hello , yes it works with and without quotes but noticed something strange. While running the fixtures multiple times, only the id of the place is regenerated everything else stays the same? Only way to create something new after the first fixture load is to drop the database and recreate it. Edit: Fixed , it seems it's running the fixtures in the order defined in the YML , so you'd have to put the relationship childrens before the parent.
yes but it doesn't make sense, example I want to make 10 User, 4 of them are Engineer and 6 are Client so when using current() client_1 and engineer_1 will have the same user
Hi guys, I know it's an old tutorial but when I click on the new version of tutorial there is nothing ... link is broken https://symfonycasts.com/sc...
Good question! If you want, you can add it to the `require-dev` part of your `composer.json`: https://getcomposer.org/doc..., though at best, that simply means that it won't be downloaded to your disk in an environment, and disk space is cheap.
What you *really* want is to add it to AppKernel in the spot where it's only loaded in the `dev` environment: https://github.com/symfony/.... There's no downside to this, and it'll give you a very minor performance boost in the `prod` environment.
I *think* I know what you're asking, and I'm planning on covering it in one upcoming screencast. If I have a field that normally holds a value like "foo.png", and that's populated by uploading a file, then I would use processors: https://github.com/nelmio/a... to look for that file in some pre-configured directory and move it into some "uploads" directory (wherever your files normally upload to - you could even move to S3).
As far as the URL to the image, now that you have "foo.png" stored in your database and the file actually lives in the correct upload spot, it's up to you in your template to link to it correctly. Btw, I recommend using VichUploaderBundle to handle uploads and link to them.
I have only one question: is there any way to make a relation with data which already exist in the database?
E.g. I want to generate fixtures for the entity Conversation which as 3 attributes: - name - creationDate - creator -> this is ManyToOne with the entity User
Is it possible to say to the 'creator' attribute to take a random User from the database and not from another .yml file?
UPDATE: I solved creating a custom function for Alice (like the one created in the screencast for super heroes names) which return a casual user id from the ones present in the database.
I don't have other information for debugging. I tried to remove some fields in my fixture yml file and when I run the doctrine:fixtures:load I have a SQL error like "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'registration_details' cannot be null". It means that Alice is trying to insert data right ?
Ok coo. So, I *probably* know what your issue is :). Somehow, you probably have some Alice code that is setting an array of data on a field that is mapped as a string on the database. I could be wrong, but Doctrine is probably trying to convert an array (that was set on an entity by Alice) into a string while saving. I would remove the alice lines one-by-one until this error goes away (like you were doing). When you finally don't have an error - or have the above error about a null constraint - it means the saving is happening, and probably the last line you removed is the problem. You could also open the core Doctrine code where the error is being thrown, and do a little var_dump() of the value right before the error. Then you can see what array value is trying to be turned into a string, which might help debugging.
Hi, thanks for this screencast! I also found typo in the text. I think you wanted to write "characters.yml" instead of "test.yml" in second code block (after sentence "Let’s call ours characters.yml and then go ahead and create that file:").
Does this work for Symfony 3.2.8? it seems to be for symfony2 only? When I look at the packagist.org, the description of hautelook bundle is "Symfony2 Bundle to manage fixtures with Alice and Faker.".
I tried following instruction multiple times, from here or from their respective github documentation, and I can't make it work with my symfony installation. Always having error when using date() faker.
This bundle works for Symfony 3 also, but *this* specific tutorial uses an old version of the bundle. But if you're following their GitHub documentation, yep, it should work! What's the date you have with the date() faker function?
It would be nice to update it with the latest changes to Alice (at least in the text below): 1. Using Hautelook\AliceBundle\Doctrine\DataFixtures\AbstractLoader instead of DataFixtureLoader 2. Launching update with hautelook_alice:doctrine:fixtures:load (or h:d:f:l) instead of doctrine:fixtures:load.
Yea, I've been thinking about this! I haven't used it much, but the new version of the bundle looks harder to use. Recently, I used Alice + DoctrineFixturesBundle directly (with no other bundle) and was very happy.
Ah, thank you! It'll be covered in the new Doctrine tutorial (near the end) which will start coming out this week: http://knpuniversity.com/sc.... Here's a preview on the script (https://github.com/knpunive... (if that helps) - the actual code blocks will be at the first link soon. Cheers!
[InvalidArgumentException] Could not find any fixtures to load in: - /Applications/MAMP/htdocs/starwarsevent/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBund .... and more
The bundle looks in the DataFixtures/ORM directory of each of your bundles for fixture classes. If it doesn't find *any*, you'll get this error. So double check that you have the fixture class in DataFixtures/ORM. But also, this tutorial covers an older version of the bundle - so this could be from a difference with the new version! In the next few days, we'll have an updated section about using Alice for fixtures in our Doctrine tutorial: http://knpuniversity.com/sc....
Basically, you want to set an array onto roles (since roles is an array field). To do that in YAML, use this syntax:
users_{1..10}:
username: # ...
# ...
roles: [ROLE_USER, ROLE_ADMIN]
# ... or, if you want something random, it should be something like this
roles: <randomElements(['ROLE_USER', 'ROLE_ADIMN', 'ROLE_CLIENT'])>
Hello , seems it doesn't like it if i define it as an array not sure why. If i put [] in the fixture for the role it will generate an array of array a:1:{i:0;a:1:{i:0;s:10:"ROLE_ADMIN";}} instead of a:1:{i:0;s:10:"ROLE_ADMIN";} If I leave it as a string like roles : ROLE_ADMIN it will generate the good array. Maybe it had something to do with the setRoles() annotation in the entity. Cheers
Hmm - yea it definitely looks like we're ending up with an array of arrays - array(array('ROLE_ADMIN')) instead of just array('ROLE_ADMIN').
I would double-check your setRoles() method - make sure you're not accidentally wrapping the array in another array before setting it. For example, this would be a problem:
public function setRoles($roles)
{
$this->roles = array($roles); // this is bad, you'd end up with 2 arrays
}
If this isn't the problem, then I would dump the $roles argument in setRoles() to see what is being passed to you:
public function setRoles($roles)
{
dump($roles);die;
}
$this->roles = array($roles); Indeed this was the case all good. Don't remember why I changed this was testing something and forgot to change it back >.< Thanks
Hello Ryan! I just installed the bundle latest version and found out the class you extend doesn't exist any longer, nonetheless in the documentation they extend the class AbstractLoader. I ran the usual command fixutures:load and my class is there but no data added to the data base. How can I debug a command? Because I thing the problem is the route where I placed the yaml file. Tried var_dump to no avail. Thanks
Found the solution. I was previously using regular Doctrine Fixtures, so having a look at this excerpt https://github.com/hauteloo... found that running php app/console h:d:f:l or hautelook_alice:doctrine:fixtures:load will load only alice fixtures. The confusing part is that when running the regular doctrine:fixtures:load I see that the alice fixtures are alse executed. Thanks any way
Yeah, it is more in line with symfony's way of doing things. The processors need to be defined as services with arguments and stuff. One question, how would I load alice's fixtures on my tests? I tried this $fixtures = $container->get('hautelook_alice.doctrine.command.load_command'); but didn't work. Thanks
71 Comments
DONT PANIC for old version
and see https://knpuniversity.com/s...
have fun!
Hey Tael,
Good cross reference, thanks!
Awesome!
In case someone runs into the same problem...
Since I'm taking this screencast after 2 years from its release, I had a lot of dependencies errors while loading composer repositories with
I updated it with the particular version that was valid at that time (0.1.5), and it worked:
Yep, good call! There is a new version of the library - and it's great - but it's *totally* different now :). This tutorial covers 0.1.5.
hello ? I think I've been using a different thing ? Didn't have a problem with it yet , the nelmio/Alice one ?
Are they related ??
Another question I'm having , was looking through the documentation and not sure is it possible to select a random relationship like this ?
AppBundle\Entity\Conference:
conference{1..5}:
startat: <datetimethisdecade()>
comment: <realtext(420)>
place: @place_*
AppBundle\Entity\Place:
place_{1..10}
name: <city()>
address: <streetaddress()>
website: <domainname()>
Hey, rddro !
hautelook/alice-bundleis a bundle for standalonenelmio/alicelibrary, which ports this lib and another one (fzaninotto/Faker) for the Symfony applications. If you see its dependencies - you will findnelmio/alice. So if you're on a Symfony app, you probably want to use this bundle instead of standalone library.And yes, it's still possible to use random relationships (using wildcard ) in new version 2.x. Do you have any errors using it? Try to wrap it with quotes like `place: '@place_'`.
Cheers!
Hello , yes it works with and without quotes but noticed something strange.
While running the fixtures multiple times, only the id of the place is regenerated everything else stays the same?
Only way to create something new after the first fixture load is to drop the database and recreate it.
Edit: Fixed , it seems it's running the fixtures in the order defined in the YML , so you'd have to put the relationship childrens before the parent.
Your "Edit" sounds right - iirc, the authors of the AliceBundle are working to address some "ordering" problems.
good day everyone
is there option for OneToOne persist, I tried this but doesn't work:
`
App\Entity\User:
App\Entity\Engineer:
<br />the error is: <br />duplicate key value violates unique constraint Key (email)=(heath58@harris.org) already exists.
`
Hey Marwen,
Have you tried this?
you can find more information about working with relationships here https://github.com/nelmio/alice/blob/master/doc/relations-handling.md
Cheers!
yes but it doesn't make sense, example I want to make 10 User, 4 of them are Engineer and 6 are Client
so when using current() client_1 and engineer_1 will have the same user
Perhaps you'll have to create your users in two sections. One for creating users with the role engineer, and another one with the role client, then you can reference them as mentioned here in the docs https://github.com/nelmio/alice/blob/master/doc/complete-reference.md#handling-unique-constraints
If that doesn't help, I think you'll have to create a custom factory
Note: I tried also
userRef (unique): '@user_*'but no chanceHi guys, I know it's an old tutorial but when I click on the new version of tutorial there is nothing ... link is broken https://symfonycasts.com/sc...
Hey Lionel F.
Thanks for pointing it out, the problem is we do not have a newer version of this course yet. My apologies for any inconveniences
Hello, great tutorial!
Is there any way to put this dependency (hautelook/alice-bundle) in my composer.json project file and only load it for development environment ?
Hi Sergio!
Good question! If you want, you can add it to the `require-dev` part of your `composer.json`: https://getcomposer.org/doc..., though at best, that simply means that it won't be downloaded to your disk in an environment, and disk space is cheap.
What you *really* want is to add it to AppKernel in the spot where it's only loaded in the `dev` environment: https://github.com/symfony/.... There's no downside to this, and it'll give you a very minor performance boost in the `prod` environment.
Cheers!
11:40... If you want to import your fixtures from scratch, you must first create the test universe.
Is there a way to add image url(s) as well via this bundle? Maybe a field in db points to an image URL.
Hey Nick!
I *think* I know what you're asking, and I'm planning on covering it in one upcoming screencast. If I have a field that normally holds a value like "foo.png", and that's populated by uploading a file, then I would use processors: https://github.com/nelmio/a... to look for that file in some pre-configured directory and move it into some "uploads" directory (wherever your files normally upload to - you could even move to S3).
As far as the URL to the image, now that you have "foo.png" stored in your database and the file actually lives in the correct upload spot, it's up to you in your template to link to it correctly. Btw, I recommend using VichUploaderBundle to handle uploads and link to them.
Let me know if I hit on your question!
Thanks for reply! Yes VichUploaderBundle is very good for Uploads. Also that's exactly what I mean!
Hi! Great tutorial!
I have only one question: is there any way to make a relation with data which already exist in the database?
E.g.
I want to generate fixtures for the entity Conversation which as 3 attributes:
- name
- creationDate
- creator -> this is ManyToOne with the entity User
Is it possible to say to the 'creator' attribute to take a random User from the database and not from another .yml file?
UPDATE:
I solved creating a custom function for Alice (like the one created in the screencast for super heroes names) which return a casual user id from the ones present in the database.
Great solution! Nothing is built in to do this, so I think you solved this perfectly!
Hello, thanks for this tutorial I didn't know this cool library !
I tried to use it in my project but I always have this error in the console :
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Array to string conversion
I don't have other information for debugging. I tried to remove some fields in my fixture yml file and when I run the doctrine:fixtures:load I have a SQL error like "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'registration_details' cannot be null". It means that Alice is trying to insert data right ?
Hey there!
Ok coo. So, I *probably* know what your issue is :). Somehow, you probably have some Alice code that is setting an array of data on a field that is mapped as a string on the database. I could be wrong, but Doctrine is probably trying to convert an array (that was set on an entity by Alice) into a string while saving. I would remove the alice lines one-by-one until this error goes away (like you were doing). When you finally don't have an error - or have the above error about a null constraint - it means the saving is happening, and probably the last line you removed is the problem. You could also open the core Doctrine code where the error is being thrown, and do a little var_dump() of the value right before the error. Then you can see what array value is trying to be turned into a string, which might help debugging.
I hope that helps!
Thanks a lot Ryan you were right. In my last field (a string one) I used a function that returns an array (paragraphs()) Shame on me !
I'll no longer make this error ^^ Thank you for your advices
:thumbsup: nice work!
Hi, thanks for this screencast!
I also found typo in the text. I think you wanted to write "characters.yml" instead of "test.yml" in second code block (after sentence "Let’s call ours characters.yml and then go ahead and create that file:").
You're right - good catch! I just updated it :) https://github.com/knpunive...
Tanks!
Very quickly! :)
I didn't know that code was available on GitHub, it would be better to create a pull request.
Thanks for quick fix
It's a poorly-publicized fact - I hope to make that more obvious soon :). Thanks again!
Does this work for Symfony 3.2.8? it seems to be for symfony2 only?
When I look at the packagist.org, the description of hautelook bundle is "Symfony2 Bundle to manage fixtures with Alice and Faker.".
I tried following instruction multiple times, from here or from their respective github documentation, and I can't make it work with my symfony installation. Always having error when using date() faker.
Hey Martin Bittner!
This bundle works for Symfony 3 also, but *this* specific tutorial uses an old version of the bundle. But if you're following their GitHub documentation, yep, it should work! What's the date you have with the date() faker function?
Cheers!
Great tut!
It would be nice to update it with the latest changes to Alice (at least in the text below):
1. Using Hautelook\AliceBundle\Doctrine\DataFixtures\AbstractLoader instead of DataFixtureLoader
2. Launching update with hautelook_alice:doctrine:fixtures:load (or h:d:f:l) instead of doctrine:fixtures:load.
Yea, I've been thinking about this! I haven't used it much, but the new version of the bundle looks harder to use. Recently, I used Alice + DoctrineFixturesBundle directly (with no other bundle) and was very happy.
Thanks for a quick reply. Can you please drop a few lines how you use it?
BTW brilliant tutorials :)
Ah, thank you! It'll be covered in the new Doctrine tutorial (near the end) which will start coming out this week: http://knpuniversity.com/sc.... Here's a preview on the script (https://github.com/knpunive... (if that helps) - the actual code blocks will be at the first link soon. Cheers!
When i try to load the fixtures i get this error:
[InvalidArgumentException]
Could not find any fixtures to load in:
- /Applications/MAMP/htdocs/starwarsevent/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBund
.... and more
Help please
Hey Oscar!
The bundle looks in the DataFixtures/ORM directory of each of your bundles for fixture classes. If it doesn't find *any*, you'll get this error. So double check that you have the fixture class in DataFixtures/ORM. But also, this tutorial covers an older version of the bundle - so this could be from a difference with the new version! In the next few days, we'll have an updated section about using Alice for fixtures in our Doctrine tutorial: http://knpuniversity.com/sc....
Cheers!
Hello o/ Anyone have some clue how to add the user role ? Tried to set it like roles: 'a:1:{i:0;s:10:"ROLE_ADMIN";}' but it didn't work properly.
Update : it would seem you can echo them like so
roles:
but I think there might be a better way
Hey rddro!
Basically, you want to set an array onto roles (since roles is an array field). To do that in YAML, use this syntax:
Cheers!
Hello , seems it doesn't like it if i define it as an array not sure why.
If i put [] in the fixture for the role it will generate an array of array
a:1:{i:0;a:1:{i:0;s:10:"ROLE_ADMIN";}} instead of a:1:{i:0;s:10:"ROLE_ADMIN";}
If I leave it as a string like roles : ROLE_ADMIN it will generate the good array.
Maybe it had something to do with the setRoles() annotation in the entity. Cheers
Hmm - yea it definitely looks like we're ending up with an array of arrays - array(array('ROLE_ADMIN')) instead of just array('ROLE_ADMIN').
I would double-check your setRoles() method - make sure you're not accidentally wrapping the array in another array before setting it. For example, this would be a problem:
If this isn't the problem, then I would dump the $roles argument in setRoles() to see what is being passed to you:
Cheers!
😂😂😂
$this->roles = array($roles); Indeed this was the case all good. Don't remember why I changed this was testing something and forgot to change it back >.<
Thanks
Hello Ryan! I just installed the bundle latest version and found out the class you extend doesn't exist any longer, nonetheless in the documentation they extend the class AbstractLoader. I ran the usual command fixutures:load and my class is there but no data added to the data base. How can I debug a command? Because I thing the problem is the route where I placed the yaml file. Tried var_dump to no avail. Thanks
Found the solution. I was previously using regular Doctrine Fixtures, so having a look at this excerpt https://github.com/hauteloo... found that running php app/console h:d:f:l or hautelook_alice:doctrine:fixtures:load will load only alice fixtures. The confusing part is that when running the regular doctrine:fixtures:load I see that the alice fixtures are alse executed. Thanks any way
It looks like the bundle got a nice new facelift - I'll take a look at what's been updated :).
Yeah, it is more in line with symfony's way of doing things. The processors need to be defined as services with arguments and stuff. One question, how would I load alice's fixtures on my tests? I tried this $fixtures = $container->get('hautelook_alice.doctrine.command.load_command'); but didn't work. Thanks
"Houston: no signs of life"
Start the conversation!