Chapters
47 Chapters
|
4:41:51
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
¡Este tutorial también funciona muy bien para Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.3", // v3.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"doctrine/annotations": "^1.0", // 1.13.2
"doctrine/doctrine-bundle": "^2.1", // 2.6.3
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.1.1
"doctrine/orm": "^2.7", // 2.10.1
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.1
"pagerfanta/doctrine-orm-adapter": "^3.3", // v3.3.0
"pagerfanta/twig": "^3.3", // v3.3.0
"phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
"scheb/2fa-bundle": "^5.12", // v5.12.1
"scheb/2fa-qr-code": "^5.12", // v5.12.1
"scheb/2fa-totp": "^5.12", // v5.12.1
"sensio/framework-extra-bundle": "^6.0", // v6.2.0
"stof/doctrine-extensions-bundle": "^1.4", // v1.6.0
"symfony/asset": "5.3.*", // v5.3.4
"symfony/console": "5.3.*", // v5.3.7
"symfony/dotenv": "5.3.*", // v5.3.8
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/form": "5.3.*", // v5.3.8
"symfony/framework-bundle": "5.3.*", // v5.3.8
"symfony/monolog-bundle": "^3.0", // v3.7.0
"symfony/property-access": "5.3.*", // v5.3.8
"symfony/property-info": "5.3.*", // v5.3.8
"symfony/rate-limiter": "5.3.*", // v5.3.4
"symfony/runtime": "5.3.*", // v5.3.4
"symfony/security-bundle": "5.3.*", // v5.3.8
"symfony/serializer": "5.3.*", // v5.3.8
"symfony/stopwatch": "5.3.*", // v5.3.4
"symfony/twig-bundle": "5.3.*", // v5.3.4
"symfony/ux-chartjs": "^1.3", // v1.3.0
"symfony/validator": "5.3.*", // v5.3.8
"symfony/webpack-encore-bundle": "^1.7", // v1.12.0
"symfony/yaml": "5.3.*", // v5.3.6
"symfonycasts/verify-email-bundle": "^1.5", // v1.5.0
"twig/extra-bundle": "^2.12|^3.0", // v3.3.3
"twig/string-extra": "^3.3", // v3.3.3
"twig/twig": "^2.12|^3.0" // v3.3.3
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.3.*", // v5.3.4
"symfony/maker-bundle": "^1.15", // v1.34.0
"symfony/var-dumper": "5.3.*", // v5.3.8
"symfony/web-profiler-bundle": "5.3.*", // v5.3.8
"zenstruck/foundry": "^1.1" // v1.13.3
}
}
18 Comments
Is anyone else confused about the "product" table mentioned in the tip of the migration file for PostgreSQL user?
Hey @JDCrain ,
Ah, I see now :) Yeah, seems someone was testing that query on different DB structure and didn't tweak it to match the tutorial code, I fixed it :)
Thanks for the head ups!
Cheers!
Hey @JDCrain ,
What exactly confuses you in that? :) Could you share more info?
Cheers!
Dear SymfonyCasts,
I created a migration to add
passwordcolumn containing$this->addSql('ALTER TABLE user ADD password VARCHAR(255) NOT NULL');.Since the
usertable already has users in it, I thought the migration would fail because it should not know what value to use for the existing rows in the table, resulting in an error. However, the migration surprisingly executed without any error.I inserted a new user manually using MySQL's console, and it warned me:
Field 'password' doesn't have a default value.I ran
set SQL_MODE='STRICT_ALL_TABLES';referring to this Stack Exchange post.As expected, the MySQL console throws an error when attempting to insert a new user without setting the password field.
However, when using
symfony console doctrine:query:sqlto insert a new user, despite the NOT NULL constraint, the following command completed successfully, introducing a new row with a missing password value:Symfony: 7.0.9
doctrine/doctrine-bundle: "^2.12"
MariaDB: 10.4.32-MariaDB
How to enforce the
NOT NULLconstraint?Thank you in advance.
Best regards,
Easwaran Chinraj
Hey Easwaran,
Hm, that should just work out of the box, not sure what exactly goes wrong in your case. First of all, please double-check the server version specified in your DB connection, either in the DATABASE_URL or in Doctrine config files, see "server_version" in https://symfony.com/doc/current/reference/configuration/doctrine.html . Also, double check your Doctrine config files that you don't have any custom options that may override that behavior.
But in your application, you can easily fix it with the validation constraints, then when you save the form without the required fields the validation will fail and the form will render an error for you avoiding saving the invalid data. That mostly solves your issue. About the "doctrine:query:SQL" - that's more complex, it just should work the same way your SQL console works, not sure. But that sounds like an edge case for admins anyway, it should not affect your users because users' input may be validated with validation constraints as I said above.
I hope that helps!
Cheers!
Hello,
the auth system requires a password field with the name
passwordfor the query in the corresponding table. How do I proceed if the password field should be called e.g.usr_password.thx!
HI @Rufnex,
Eeasy-peasy, you can configure everything in
security.yamlhere the docs https://symfony.com/doc/current/reference/configuration/security.html#reference-security-firewall-form-loginCheers!
Hi sadikoff,
thank you .. thats clear. What I meant I, in the database table the password field is called usr_password and not password. Is it possible to configure that too?
Thank you again.
Of course, you have total control over your Entity. You can use
nameproperty on#[ORM\Column()]attribute, and many overs BTW :-)Cheers
OMG . thats fu*** easy! Thank you ;o)
After changing nullable field to "not null", probably needs to add DEFAULT value in the migrations
$this->addSql('ALTER TABLE users ADD password VARCHAR(255) NOT NULL DEFAULT \'\'');
Hey triemli,
And probably not, did you get any issues with that? If yes, can you please share you DB version so we can find why that's not work for you?
If you create a not null field without default data mysql should just add this field with empty data, but probably that is something depending on configuration or server version.
Cheers
Maybe because I used Postgres.
First migration:
` $this->addSql('CREATE SEQUENCE users_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
Second
$this->addSql("ALTER TABLE users ADD password VARCHAR(255) NOT NULL DEFAULT ''");Yeah that's the key, we are using mysql for tutorial, sounds like we should write a note about it, thanks for tip!
Cheers!
On Postgres I had to add some extra escaped quotes round user to get the migration to complete:
$this->addSql("ALTER TABLE \"user\" ADD password VARCHAR(255) NOT NULL DEFAULT''");Hey gazzatav
I bet that is because you are using "user" as table name, and this word is reserved. but tutorial uses "users" and that can be ok
Cheers
Hi @Vladimir, you are right that 'user' is a reserved word in postgresql and several other sql standards but I have used the class User exactly like the tutorials (there is no use of 'users' in the class). Doctrine is choosing table names not me! It may be that Doctrine chose 'users' for your dbms and version. Maybe Doctrine's knowledge of the dbms is so good it knows how closely it can keep to entity names. Out of interest, the postgresql documentation does say that it's possible to use a reserved word as a bare table name without creating an 'as' alias but anyway Doctrine always seems to create aliases. If I use psql to check my database, when I use the '\d user' command to describe the table, it describes the one Doctrine made. However, if I run 'select * from user;' the result comes from the admin table which postgres made! If I change the query to use 'public.user' I get the results from the fixtures.
Whoops, I missed somehow that tutorial uses 'user'. You can always force doctrine to use any table name you need with
@ORM\Table()annotation =)Cheers
"Houston: no signs of life"
Start the conversation!