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!
08.
Persisting to the Database
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
}
}
19 Comments
I am editing an entity, and upon some manual validation error I want to write a log message to the DB, but when I flush that message, the entity under editing is also flushed to DB. I have tried entityManager->clear() but that throws errors about entity configuration (seems like I need additional cascade configuration on my entities in order to use that).
Is there no simple way of just reversing the entityManager->persist()?
What I want is to be able to NOT persist some of the entities previously called ->persist() on (or fetched from the DB thus automatically being persisted).
Code:
Hey Matt!
Yea, this is a weird thing about Doctrine... it doesn't "come up" much, but it is a bit inflexible :/. You could, at one point, call
$em->flush($entity), but that was deprecated and apparently was never guaranteed to work.What I sometimes do in this situation is opt for a quick, manual, DQL insert query for the log entry, instead of creating and persisting an entity. I don't do this frequently, but it's an option.
I have very little experience using this, but I believe this is what
detach()is for:Let me know if that works :).
Cheers!
Hi.
Sorry I bother you, but I'm stuck on this chapter. When I try to run app with "new" method I got an error:
PDOException>Exception>DriverException
An exception occurred in the driver: could not find driver
in C:\xampp\htdocs\symfonycast\cauldron_overflow\vendor\doctrine\dbal\src\Driver\API\PostgreSQL\ExceptionConverter.php (line 87)
}
Sorry I don't know what cause an error?
I would be grateful for your help.
Howdy!
No bother at all! This sounds like your PHP instance does not have the required PDO drivers to interact with a database. A quick way to check, in a terminal run
php -i | grep "pdo"& you should see something similar to:Couple of questions to get those installed if they're missing:
1) What version of PHP are you using?
php -vWill show you.2) What Operating System are you using
3) Do you remember how you installed PHP onto your computer? e.g. using
apt-get install phpor compiled PHP source code, etc..Thank You!
Hi. Thanks for quick response.
Answers:
ad1.
c:\xampp\htdocs\symfonycast\cauldron_overflow>php -v<br />PHP 7.4.24 (cli) (built: Sep 21 2021 13:38:25) ( ZTS Visual C++ 2017 x64 )<br />Copyright (c) The PHP Group<br />Zend Engine v3.4.0, Copyright (c) Zend Technologiesad2. Windows 11
ad3. I have installed PHP with XAMPP
And maybe some useful info regarding PDO:
PDO
PDO support => enabled
PDO drivers => mysql, sqlite
pdo_mysql
PDO Driver for MySQL => enabled
Client API version => mysqlnd 7.4.24
pdo_sqlite
PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.31.1
thanks a million
Awesome! That's very helpful. I think instead of trying to install the Postgres PDO driver for PHP, you might be better off using MySQL/MariaDB seeing as XAMPP already has that configured for you.
In your
.envfile, you should have aDATABASE_URL="postgresql........."line and right above/below you'll see another that is commented out:# DATABASE_URL="mysql.........". Remove the comment from themysqlline and add it to thepostgresqlline. This tells Symfony to use MySQL/MariaDB instead of Postgres. You'll need to set the correct username, password, host, etc to match XAMPP's configuration. But this should get you on the right track.If you really want to stick with Postgres, and that's not a bad thing, you'll need to install postgres for windows if it's not already installed: https://www.postgresql.org/download/windows/ I'd recommend either version 13 or 14 for now. Then uncomment the following lines in your
php.inifile:And, if I'm not mistaken, add
LoadFile "C:\xampp\php\libpq.dll"to yourhttpd.confensuring that the path is correct tolibpq.dll.Restarting XAMPP and running
php -i | grep "pdo"will tell you if you have thePDO_PGSQLconfigured correctly.I hope this helps, but do let use know if you have any trouble!
So, I'm trying to run all this stuff on a separate CENTOS box than my local machine, and for the most part everything is working as intended as I go through the tutorials. I have a tomcat running that I use as a URL instead of localhost.
But I can't for the life of me get the persist to the DB working.
I have the docker running.
I can connect to the docker shell and run MYSQL and everything as expected.
The problem comes when I try to persist to the DB. I get a connection refused error, which I am assuming is because it can't find the DB.
I've tried replacing the URL (@127.0.0.1:3306) with the URL I am running via apache (@http://cauldron.server.local:3306) but that hasn't worked.
I could use some help. Running using localhost just isn't an option for me.
Hey Tim,
Please, double-check the DB credentials you're using, i.e. check DATABASE_URL in your .env and .env.local files. Make sure that you entered the valid credentials - not only DB host and port, but also username and password. something like this:
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7&charset=utf8mb4"
Try to use the correct IP instead of that cauldron.server.local, it may help as well. Btw, do you use MySQL as your DB provider? I'd also recommend you to double-check your DB credentials with a MySQL GUI client - fill the credentials you're using there and try to connect. Most probably your credentials are just invalid. But if you can connect there - just use the same credentials for DATABASE_URL (don't forget to make changes in all .env files where you set DATABASE_URL just in case).
There's another option to use Docker locally for running this project. Probably take a look?
Cheers!
No, I know the credentials are correct. I was just giving a shortened database_url above to show the area that I think needed to be looked at. As I said above I can connect through the docker shell using the same credentials that I have in the database_url. Truth is I don't even think it is getting far enough to worry about the credentials yet, it isn't even getting as far as finding the DB to ask for credentials.
I guess what I am asking is if I run the docker with the MySQL (Yes it is MySQL) is it going to use the same apache/tomcat and use the same URL as the project itself or do I need to set up a conf.d file for the dockerised port, or something else entirely? I am not the most knowledgeable when it comes to tomcat/apache and know just enough to get things mostly working for a project and I can /usually/ figure it out, but this one has me stumped.
UPDATE:
It's moot now. I found an online MySQL host and am using that now without worrying about Docker.
Why symfony doesn't see Question.php as a part of a project? It doesn't want to autocomplete the use statement and it even don't see it at all
Hey HudyWeas,
I'm not sure, if you could provide a screenshot so I could better understand what you mean about "as a part of a project". But as I replied to another your comment - please, make sure you have PhpStorm's Symfony plugin installed and enabled for your project - it gives you most of the autocompletion in projects based on Symfony.
Cheers!
Hello
I'm new to the symfony world and I encountered a problem at
$entityManager->persist($question);
I did exactly as in the tutorial and I came across this error:
An exception occurred in driver: could not find driver
Doctrine\DBAL\Exception\
DriverException
Doctrine\DBAL\Driver\PDO\
Exception
could not find driver
PDOException
could not find driver
I have the database on the docker because I did not install mysql locally
but I can connect to it
I don't understand what the problem is
I tried to add the extension pdo_mysql in php.ini but without success I use ubuntu and I am new in this operating system :)
PS: sorry for my english
Hey Assist software
Which php version are you using? is it installed on the OS? if it's OS package, than you should install php-mysql extension and it will add PDO drive, you can do it with
sudo apt-get install php-mysqlCheers!
hi again,
I get this error when trying to flush:
An exception occurred in driver: SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'this is my databse url:
<br /> DATABASE_URL="mysql:/root:password@127.0.0.1:3306/db_name?serverVersion=5.7"<br />my password is correct.
any ideas?
Hey Payman,
Yes, you clearly does not have access with the given credentials. Are you sure that those credentials are used for the Symfony environment you're trying to load your application with? If so, please, make sure they are really valid, like check the host, port, database name, user and password. Also, the way you wrote it is incorrect, it should have 2 slashes after "mysql", i.e.:
I hope this helps! If not, probably you don't have needed permissions for root user, I mean it might not be allowed to access the DB from your PHP script. The easiest solution would be to create another user, e.g. name it "admin" and grant all the necessary permissions to it, then update the DATABASE_URL.
Cheers!
Using single quotes like in this video to query on the CLI resulted in the error:
Too many arguments, expected arguments "command" "sql".Switching to double quotes fixed it though.
Hey there,
That's weird, maybe it's related to your shell, or you may have had a non-closed quote?
I'm doing this on Windows 10 so I'm sure it's the shell.
I'd think the same. Windows just don't like to work as expected :D
"Houston: no signs of life"
Start the conversation!