Chapters
23 Chapters
|
2:22:24
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.4.1
Subscribe to download the code!Compatible PHP versions: ^7.4.1
-
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!
16.
Smarter Entity Methods
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.
This tutorial also works great for Symfony 6!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.4.1",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^2.1", // 2.1.1
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.7", // 2.8.2
"knplabs/knp-markdown-bundle": "^1.8", // 1.9.0
"knplabs/knp-time-bundle": "^1.11", // v1.16.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.1
"sentry/sentry-symfony": "^4.0", // 4.0.3
"stof/doctrine-extensions-bundle": "^1.4", // v1.5.0
"symfony/asset": "5.1.*", // v5.1.2
"symfony/console": "5.1.*", // v5.1.2
"symfony/dotenv": "5.1.*", // v5.1.2
"symfony/flex": "^1.3.1", // v1.21.6
"symfony/framework-bundle": "5.1.*", // v5.1.2
"symfony/monolog-bundle": "^3.0", // v3.5.0
"symfony/stopwatch": "5.1.*", // v5.1.2
"symfony/twig-bundle": "5.1.*", // v5.1.2
"symfony/webpack-encore-bundle": "^1.7", // v1.8.0
"symfony/yaml": "5.1.*", // v5.1.2
"twig/extra-bundle": "^2.12|^3.0", // v3.0.4
"twig/twig": "^2.12|^3.0" // v3.0.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.0
"symfony/debug-bundle": "5.1.*", // v5.1.2
"symfony/maker-bundle": "^1.15", // v1.23.0
"symfony/var-dumper": "5.1.*", // v5.1.2
"symfony/web-profiler-bundle": "5.1.*", // v5.1.2
"zenstruck/foundry": "^1.1" // v1.5.0
}
}
24 Comments
minor issue here: I don't have any DATABASE_URL environment variable explicitly defined in .env or anywhere else. I've been letting the symfony binary figure it out from docker-compose.yml and set it for me, as we learned a few chapters ago. Consequently, when I run composer commands and it tries to run the auto-scripts'
cache:clear, I get a fatal 'Environment variable not found: "DATABASE_URL".' If I prefix the composer command with DATABASE_URL=whatever it works -- literally, any old string works. So, not a show-stopper, but I do wonder what the recommended practice is.btw these tutorials are marvelous. really enjoyable and extremely clear.
Hey Elliott,
First of all, thank you for kind words about this tutorials! :)
What exactly command did you run? If you want to base on Docker configuration, you should execute "symfony composer install" instead of just "composer install", otherwise the variable won't be found. I guess the problem was in the command you ran :)
Cheers!
of course! I didn't realize you could run both
composeras well asbin/consolethrough thesymfonycommand. it almost -- but not quite -- occurred to me that I might try that (-: thanks.Hey Elliott,
Yeah, that's a little know fact probably, and in most cases there's literally no difference unless you start using Docker ;)
Cheers!
Hello,
First of all, I want to say that I love the way you explain things Ryan, you make them easy to understand, so big thanks to u and to the Team.
I was surprised when I noticed that it's not mandatory to have a "get" word at the beginning of your method to be able to use it in the TWIG (ex : getVotesString can be votesString ) , which means that when creating custom methods it's better to make them protected if we don"t want them to be accessible outside (twig).
I tried to use the doctrine annotation to set a default value, but still I get the error saying that this field (votes) can't be null, I had to set the property to Zero to make it work. Here is the syntax
* @ORM\Column(type="integer",options={"default":0})Am I missing smtgThanks in advance for your response.
Hey Hanane K.!
Thanks for for the nice message ❤️❤️❤️
Definitely. Better, make them private. If you don't need to access a method from outside of the class (via Twig or any other way), then make it private. I always start with private... then only make something public when I NEED to. Though, I'm a bit liberal with my getter/setter methods in my entities (I usually start with a public getter/setter for every property... and sometimes I remove methods that I don't use... but not always - I'm not strict about it).
I always set my properties to their default value anyways - I've never actually used the
options={"default":0}way of doing things. There's nothing wrong it it (well, except that it's not working for you for some reason!), it's just not really needed. If you only used theoptionsway of doing this, then it's a bit weird because your property might benullin php... but then when you save it, it will save asa 0. It's just a bit cleaner and more consistent (in my opinion) to set the property to 0... and then naturally when you save, it will save 0 to the database. So I don't know why the "options" isn't working, but I don't recommend bothering with it anyways :).Cheers!
Small note, getVotesString should be something like this:
public function getVotesString(): string
{
$prefix = ($this->votes == 0) ? '' : (($this->votes >=0) ? '+' : '-');
return sprintf('%s %d', $prefix, abs($this->votes));
}
If not you will get +0 :)
Hey Tomasz P.
Good catch =) Yeah of course that part of code can be much better and that's all is up to you to make it perfect!
Cheers!
Why do you use $this->getVotes() and not just $this->votes in the getVotesString() function?
Hey Name->Nick,
Fairly speaking, it's not that much important, and probably just matter of habit, but yeah, you're right, calling the getter is redundant in this case. Moreover, sometimes you even don't need a getter, so it's pointless to have it only to reference from within the class where the property is defined. Though sometimes you may have a bit extra logic in the method, and you do want to reuse it. But most of the cases, I'm for calling properties directly instead via getters within the class :)
Cheers!
Cool just making sure there wasn't some more complicated purpose behind it like doctrine having to query the database or entity persistence or something
Hey Name->Nick,
Good thing to think about, yeah :) But in this case it's much simpler, the value is just an int type ;)
Cheers!
Heads up for those using PostgreSQL - the migration execution isn't going to work based on the auto-generated migration PHP files alone, assuming you already have some existing Questions saved in your database.
When executing the migration, it will attempt to add the 'votes' field to your existing Questions with the default value 'null'. But you specified that 'votes' cannot be null, so the migration fails.
It's an easy fix - update the migration file's "up" function to use 0 as the default value instead.
Before:
$this->addSql('ALTER TABLE question ADD votes INT NOT NULL');After:
$this->addSql('ALTER TABLE question ADD votes INT NOT NULL DEFAULT 0');In your migration:
`
public function up(Schema $schema): void
`
Hey Piotyras,
Yes, you're right, migrations generated for MySQL won't work on PostgreSQL. We usually use MySQL DB in our screencasts, so consider to generate a different migration that will work for your DB porting our migration generated for MySQL, or if you don't care about the data lose - you can execute bin/console doctrine:schema:update --force, but this trick won't fit production of course, as you do not want to lose prod data.
Thanks for sharing your solution with others!
Cheers!
In AbstractMySQLDriver.php line 115:
An exception occurred in driver: SQLSTATE[HY000] [2006] MySQL server has go
ne away
In Exception.php line 18:
SQLSTATE[HY000] [2006] MySQL server has gone away
In PDOConnection.php line 39:
SQLSTATE[HY000] [2006] MySQL server has gone away
In PDOConnection.php line 39:
PDO::__construct(): MySQL server has gone away
I added the votes property then I wanted to make migration but It states this error? any help
Hey Shubham,
It sounds like you have a connection problem to the DB server. I'd suggest you to restart your MySQL server, and try to stop and start Symfony server as well just in case. Does it help?
Also, double check your DB schema, make sure it's valid and in sync. To check it, you can run:
$ bin/console doctrine:schema:validate
Both Mapping and Database sections are green? If it's a dev project with no real data, and you don't care about any potential data lose locally - you can try to execute:
$ bin/console doctrine:schema:update --force
To sync the DB schema and try again to see if it solves the issue. But you probably may want to write a correct migration for those executed queries to avoid data losing on production.
If nothing above helps, what exactly are you doing when see this error? Just refresh the page? What's page URL exactly?
Cheers!
Yeah it worked I restarted docker!!
Hey Shubham,
I'm happy to hear it, thanks for confirming that helps!
Cheers!
Hello,
I was surprised to see that the migration did not break when adding a not nullable field to our data. It appears it's thanks to MySQL, but I didn't find informations about this behavior :
After a few tests, I noticed that when adding a int or double not null without providing a default value, mysql puts the value 0 in this field for existing datas.
On the other hand, I was confused by symfony console dbal:run-sql "SELECT * FROM question" showing string(1) "0" for my new INT fields. But it's just a weird display from the command. In fact it is indeed an int. For example, a double 1.1 is displayed string(3) "1.1".
Hey Antoine R.!
> I was surprised to see that the migration did not break when adding a not nullable field to our data. It appears it's thanks to MySQL, but I didn't find informations about this behavior
Yup, you're correct, I also don't know the specifics. But I do know, for example, if you added a new "not null datetime" field, that WOULD cause a problem with MySQL.
> On the other hand, I was confused by symfony console dbal:run-sql "SELECT * FROM question" showing string(1) "0" for my new INT fields. But it's just a weird display from the command. In fact it is indeed an int. For example, a double 1.1 is displayed string(3) "1.1".
Good debugging. As i was reading this, I was also thinking "I wonder if that's just due to how it's displayed" :).
Cheers!
"Houston: no signs of life"
Start the conversation!