gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
Well hey friends! And bienvenidos to our tutorial about learning Spanish! What? That's next week? Doctrine?
Ah: welcome to our tutorial all about making Symfony talk to a database... in English.
We learned a ton in the first two courses of this series, especially the last tutorial where we demystified services, autowiring and configuration. That hard work is about to pay off as we take our app to the next level by adding a database. That's going to make things way, way more interesting.
In truth, Symfony has no database layer at all. Instead, it leverages another library called Doctrine, which has been around for a long time and is incredible. Symfony and Doctrine are, sort of, the BFF's of programming, the Batman and Robin of web development, the Bert & Ernie of HTTP! They're both powerful, but they have such a strong integration that it feels like you're using one library.
And not only is Doctrine powerful, but it's also easy to use. I'll admit that this was not always the case. But Doctrine is now more accessible and fun to use than ever before. I think you're going to love it.
To learn the most about Doctrine - and to become the third amigo - you should definitely code along with me by downloading the course code from this page. After you unzip the file, you'll find a start/
directory with the same code that you see here. Check out this README.md
file for all the setup fun!
The last step will be to open a terminal and use the Symfony binary to start a local web server - you can download the binary at https://symfony.com/download. Run:
symfony serve -d
This starts a web server in the background on port 8000. I'll copy the URL, spin over to my browser and say hello to... Cauldron Overflow! Our question and answer site for witches and wizards: a place to debug what went wrong when you tried to make your cat invisible and instead made your car invisible.
So far, we have a homepage that lists questions and you can view each individual question and its answers. But... this is all hardcoded! None of this is coming from a database... yet. That is our job.
Now, remember: Symfony starts small: it does not come with every feature and library that you might ever need. And so, Doctrine is not installed yet.
To get it, find your terminal and run:
composer require orm
Let's... "unpack" this command!
First, orm
is one of those Symfony Flex aliases. We only need to say composer require orm
but, in reality, this is a shortcut for a library called symfony/orm-pack
.
Also, we talked about "packs" in a previous course. A pack is a, sort of, fake package that exists simply to help you install several other packages.
Let me show you: copy the name of the package, and go open it in GitHub: https://github.com/symfony/orm-pack. Yep! It's nothing more than a single composer.json
file! The whole point of this library is that it requires a few other packages. That means that we can composer require
this one package, but in reality, we will get all four of these libraries.
Now, one of the other packages that we have in our project is symfony/flex
, which is what powers the alias and recipe systems. Starting in symfony/flex
version 1.9 - which I am using in this project - when you install a pack, Flex does something special.
Go and look at your composer.json
file. What you would expect to see is one new line for symfony/orm-pack
: the one library that we just required. In reality, Composer would also download its 4 dependencies... but only the pack would show up here. But... surprise! Instead of symfony/orm-pack
, the 4 packages it requires are here instead!
{ | |
... lines 2 - 3 | |
"require": { | |
... lines 5 - 7 | |
"composer/package-versions-deprecated": "^1.8", | |
"doctrine/doctrine-bundle": "^2.1", | |
"doctrine/doctrine-migrations-bundle": "^3.0", | |
"doctrine/orm": "^2.7", | |
... lines 12 - 26 | |
}, | |
... lines 28 - 82 | |
} |
Here's the deal: before symfony/flex
1.9, when you required a pack, nothing special happened: Composer added the one new package to composer.json
. But starting in symfony/flex
1.9, instead of adding the pack, it adds the individual libraries that the pack requires: these 4 lines. It does this because it makes it much easier for us to manage the versions of each package independently.
The point is: a pack is nothing more than a shortcut to install several packages. And in the latest version of Flex, it adds those "several" packages to your composer.json
file automatically to make life easier.
Anyways, if we scroll down... you can ignore this zend-framework
abandoned warning. That's a distant dependency and it won't cause us problems. And... ah! It looks like this installed two recipes... and one of those gives us a nice set of instructions at the bottom. We'll learn all about this.
Tip
If you're using the latest version of Symfony Flex, this installation command will ask you if you want to also include some Docker configuration. Feel free to choose whatever you want, but we will use Docker to help connect to the database in this tutorial.
To see what the recipes did, I'll clear my screen and say:
git status
Ok: in addition to the normal files that we expect to be modified, the recipe also modified .env
and created some new files.
Go check out .env
. At the bottom... here it is: it added a new DATABASE_URL
. This is the environment variable that Doctrine uses to connect to the database.
Tip
The default DATABASE_URL
now uses PostgreSQL, but there is a commented-out
MySQL example above if you prefer that. But in both cases, if you use our Docker
integration (keep watching!) then you won't need to configure DATABASE_URL
manually.
... lines 1 - 22 | |
###> doctrine/doctrine-bundle ### | |
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url | |
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" | |
# For a PostgreSQL database, use: "postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=utf8" | |
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml | |
DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7 | |
### |
And... we can see this! The recipe also added another file called config/packages/doctrine.yaml
This file is responsible for configuring DoctrineBundle. And you can actually see that this doctrine.dbal.url
key points to the environment variable! We won't need to do much work in this file, but I wanted you to see that the environment variable is passed to the bundle.
doctrine: | |
dbal: | |
url: '%env(resolve:DATABASE_URL)%' | |
# IMPORTANT: You MUST configure your server version, | |
# either here or in the DATABASE_URL env var (see .env file) | |
#server_version: '5.7' | |
orm: | |
auto_generate_proxy_classes: true | |
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware | |
auto_mapping: true | |
mappings: | |
App: | |
is_bundle: false | |
type: annotation | |
dir: '%kernel.project_dir%/src/Entity' | |
prefix: 'App\Entity' | |
alias: App |
The recipe also added a few directories src/Entity/
, src/Repository/
, and migrations/
, which we'll talk about soon.
So all we need to do to start working with Doctrine is configure this DATABASE_URL
environment variable to point to a database that we have running somewhere.
To do that, we're going to do something special in this tutorial. Instead of telling you to install MySQL locally, we're going to use Docker. If you already use Docker, great! But if you haven't used Docker... or you tried it and didn't like it, give me just a few minutes to convince you - I think you're going to love how Symfony integrates with Docker. That's next!
Hey Marnix,
It sounds like you forgot to run composer install, no? Did you follow instructions from the README? Please, download the course code again, go to the start/ directory and follow all the instructions from the README.md file there. Also, please, keep in mind that you need to run "composer install" instead of "composer update" - this is important to avid such kind errors. Let me know if you still have this issue! I would like to know what PHP version do you have.
Cheers!
Well, that worked for using the code of the fundamentals course. Still have the same problem. Working on mac and php version is 7.4.26
Hi,
I just copied the files to my XAMPP on Windows 10 (PHP 7.4.29). Then I installed the CLI from Symfony and executed symfony composer install
. But at the end of it I am getting a "Environment variable not found: "SENTRY_DSN"." How do I get rid of this one?? I couldn't find anything helpful on the net.
Thx
Oliver
finally I found it - further down on this page. And adding the SENTRY_DSN= to my .env did it for me.
Hey Oliver,
I'm glad you could solve your problem. I recommend you create a local version of the .env
file to override any credentials you want without committing those changes to the repository. You can read more about it here https://symfony.com/doc/current/configuration.html#overriding-environment-values-via-env-local
Cheers!
In ExceptionConverter.php line 103:
An exception occurred in the driver: SQLSTATE[HY000] [2002] Connection refused
In Exception.php line 30:
SQLSTATE[HY000] [2002] Connection refused
In Driver.php line 28:
SQLSTATE[HY000] [2002] Connection refused
please tell me what to do?
Hey Dec1derr,
Do you use MySQL database in your project config? Please, double check that your MySQL server is working... if it's not - please start it first and try again. If it works, well, I think you might have to restart it and try again.
I hope this helps! Otherwise, please, double check your DB configuration in your project, like DB host, port, username/password, etc.
Cheers!
Hello, guys. Great course, but I'm having a problem.
After extracting "start" folder from archive, installing all bundles with "composer install" and running local server with "symfony serve -d" I'm able to see working site. But then following tutorial I'm trying to install doctrine with "composer require orm". And after that site is crashing with this error:
Argument 1 passed to
Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter::__construct()
must be an instance of Doctrine\Common\Persistence\ManagerRegistry or
null, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called
in
/home/iscander/Desktop/start/var/cache/dev/ContainerTj0Nn3G/App_KernelDevDebugContainer.php
on line 1212
That's not the only problem I faced (previous were solved by googling).
It's like most of the time watching these tuts I'm stucking with this kind of compatibility errors.
I installed php 7.4 (7.4.28 to be exact) and downgraded my composer to version 1 (1.10.14 to be exact). Working on Ubuntu.
Can you please make a full list of all requirements to watch these tutorials and to be able to reproduce all the steps on my computer?
Tried it on Windows 10 with php 7.2.5 (as it requires in composer.json) and composer 1.10.25. After "composer install" it says:
Compile Error: Cannot declare interface Stringable, because the name is already in use
It's really funny. Will I ever start coding instead of fixing errors? :C
I'm not blaming you. I really like your platform, courses and the way you teach us.
Hey Ruslan I.
As I see the only you should run this code with php 7.4 without any issues, and it's better to use latest composer version, just allow plugins to run when it asks it. So try again with latest composer and php7.4 and get back with results
Cheers!
Okey, I did what you said, Vladimir. php 7.4, composer latest. Recorded whole process. I could upload it on youtube if you want.
Now after "composer require orm" it said:
The "symfony/flex" plugin was skipped because it is not compatible with Composer 2+. Make sure to update it to version 1.9.8 or greater.
It suggested "symfony/orm-pack". I typed "composer require symfony/orm-pack". Site's working fine, composer.json has symfony/orm-pack line. Am I doing right?
But how am I gonna work without symfony/flex? Don't I need recipes?
That is the real issue, why you do have outdated symfony/flex package, try to re-download course code from this page, the symfony/flex should be 1.17.5 and it should work with latest compser
probably you got more outdated packages, that's why your installation is broken so downloading course code should fix it
Cheers!
Argument 1 passed to
Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter::__construct()
must be an instance of Doctrine\Common\Persistence\ManagerRegistry or
null, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called
in
C:\Users\test\Desktop\start\var\cache\dev\ContainerKMxEU8Z\App_KernelDevDebugContainer.php
on line 1212
new error after updating symfony/flex
yep that did the trick, however you may face some more issue in future so be careful with updating packages on course code =)
Cheers
Hey Mepcuk
Doctrine does not impose you any style. If you want your entities to behave as anemic or rich domain model it's up to you and the needs of your application.
Cheers!
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires php ^7.4.1 but your php version (8.0.14) does not satisfy that requirement.
this appears when 'composer install' after downolading code, I hit 'composer install --ignore-platform-reqs' but appears again when 'composer require orm'
should i add this „ignore” flag every time i 'composer something'?
Hey Jan
My apologies but this course do not support PHP 8. In the versions tab you can see the full list of depedencies for this course. What you can do is to install PHP 7 in your local machine, Sorry for the troubles
When I want to download a code from the course I get this error:
Woops, wrong turn!
It seems our GPS malfunctioned. No worries, search or navigate to safety below!
Hey Huerto,
It works for me too. Please, try to refresh the page and try again. If still the same error - could you try to open the website in Chrome Incognito mode, login into your account and try again? Still does not work for you? Please, let us know if you still have that issue, we would like to know more about your browser, and the URL that you have in your address bar when you see this page. Any screenshots are welcome too!
Cheers!
Hey @Huerto!
Ah, sorry about that! It works for me... but I believe you that something is (or at least was) going wrong. Do you still get the error? What you're seeing is our 404 error... and (looking at our code) I can't see anything that would trigger that.
Cheers!
Hi everyone,
I'm currently trying to install doctrine with the command composer require orm
.
But my terminal (DOS command prompt on Windows 10) crashes a few seconds after I launch the command.
I'm juste getting :
Using version ^2.1 for symfony/orm-pack<br />./composer.json has been updated<br />Running composer update symfony/orm-pack<br />Loading composer repositories with package information
... and then crash !
I have downloaded the Course code and installed the dependencies with composer install (got an error at the end about sentry_dsn not existing, I guess I should recreate this value in vault as seen in last tutorial).
I tried to install another package, and have no problem.
I even reinstalled composer.
Could someone please help me ?
Thank you a lot.
It's @Ariel again, replying to myself :)
Looks like it finally worked.
I found that my composer uninstallation was still running in the back (waiting for a reply on which files to delete).
So I installed composer again, and updated my composer.json with the same dependencies as in the course "versions" (see the tab next to conversations).
With a composer install an composer update, it seems to work.
Have a nice day.
Hey @Ariel
That IS nice that you got everything fixed! Keep learning and feel free to ask questions!
Cheers!
i installed it from start, yarn install, and also install composer, that time it's working fine.
but when i install orm then i it show me this error
An exception occurred in driver: could not find driver
please help me.
Hi Covi A.!
Hi! Sorry about the troubles - but I know this issue, and the error is very unclear :). The short answer is: your PHP is missing the PDO extension. How you install it depends on your operating system. For example, on Ubuntu, you would run something like sudo apt-get install php-pdo php-mysql
... but again, it varies based on your operating system.
What operating system are you using and how is PHP installed?
Cheers!
thank you very much for your answer. but i can't solve yet the problem
i am using ubuntu 20.04
and php version is 7.3
i have tired install php-pdo etc command. could you please tell me exactly right command
Hi Covi A.!
Perfect - Ubuntu is one of the easier to get this working :). It "should" just be this command:
sudo apt install php-mysql
After, be sure to restart your web server (i.e. the Symfony web server if using that). If that doesn't work, let me know if you get an error when running the above code, or if it says "this is already installed".
Cheers!
I installed sudo apt install php-mysql
and restart my web server
An exception occurred in driver: could not find driver
But it still shows me this error. No changes.
Hey Covi A.
My guess here is you may have installed the php-mysql module for php 7.4 or other version than the one you're currently using. Inspect your directory where you have php installed (usually, /etc/php) and look inside the mods-available
folder, in there you should see the mysql.ini and pdo.ini files
Or, you can run php -m
to see all installed modules.
Cheers!
Hello. after clear project start folder copy and composer install
get error <blockquote>Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for jean85/pretty-package-versions 2.0.3 -> satisfiable by jean85/pretty-package-versions[2.0.3].
- jean85/pretty-package-versions 2.0.3 requires composer-runtime-api ^2.0.0 -> no matching package found.
Problem 2
- jean85/pretty-package-versions 2.0.3 requires composer-runtime-api ^2.0.0 -> no matching package found.
- sentry/sentry-symfony 4.0.3 requires jean85/pretty-package-versions ^1.5 || ^2.0 -> satisfiable by jean85/pretty-package-versions[2.0.3].
- Installation request for sentry/sentry-symfony 4.0.3 -> satisfiable by sentry/sentry-symfony[4.0.3].
</blockquote>
Something i do wrong?
Thanks for any advise
Solved - Update composer from 1 to version 2
In Ubuntu few steps<br />sudo apt-get remove composer<br />curl -sS https://getcomposer.org/installer -o composer-setup.php<br />sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer<br />
I am getting the following error clearing the cache after composer install on the start files, any ideas?
Symfony\Component\Config\Exception\LoaderLoadException:
Class "1\CommentController" does not exist in /........../projects/learn_symfony/symfony-doctrine/config/routes/../../src/Controller/ (which is being imported from " /........../projects/learn_symfony/symfony-doctrine/config/routes/annotations.yaml"). Make sure annotations are installed and enabled.
Hey Fraser P.!
Hmm. I did make a few tweaks to the code a few days ago... so it's very possible I messed something up. However, I just tried to download the course code from this page and couldn't repeat. I unzipped, went into the start directory, ran composer install
and also tried php bin/console cache:clear
. All work fine.
What version of PHP are you using? And are you doing anything different than what I'm doing above to trigger the problem?
Let me know!
Cheers!
right after composer require orm, when i reload the page in browser, i getting the following in the error log
php.CRITICAL: Uncaught Error: Argument 1 passed to Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter::__construct() must be an instance of Doctrine\Common\Persistence\ManagerRegistry or null, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called in start/var/cache/dev/ContainerQkMdM9p/App_KernelDevDebugContainer.php on line 1151
Hey Robin,
Did you download the course code and start from start/ directory? And if so, did you run the "composer update" command on this project? Or did you start from scratch with a new Symfony project?
Could you clear the cache and try again?
Cheers!
started from the freshly extracted start directory. page shows up fine after composer install. composer require orm broke the page. running composer update fixed the issue, thanks!
Hey Robin,
Thank you for more info on this! I confirm this bug, was able to reproduce. Thank you for the tip with "composer up"! Yes, this should help. Another workaround would be to update the sensio/framework-extra-bundle, we will try to fix this in our code. Thanks for letting us know about this problem!
Cheers!
downloaded 'Course Code'
open in PhpStorm
run composer install
8 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 1
!! In EnvVarProcessor.php line 171:
!!
!! Environment variable not found: "SENTRY_DSN".
!!
!!
!! cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
!!
!!
Script @auto-scripts was called via post-install-cmd
Hey Kc,
Looks like you need to declare an env var called SENTRY_DSN in .env - it should fix the problem. Try to add just "SENTRY_DSN=", it should be enough. Or you can set your real SENTRY_DSN value, but to avoid committing it - add it into .env.local file instead. Btw, do you start from finish/ or start/ directory of downloaded course code?
Cheers!
Hello Victor
After using the above step when i tried again with Composer install, I got the following error -
[Seld\JsonLint\ParsingException]
"./composer.json" does not contain valid JSON
Parse error on line 10:
...bundle" : "^6.0", /*"sentry/sentry-sym
---------------------^
Expected: 'STRING' - It appears you have an extra trailing comma
how to resolve this issue?
Hey Jayant,
It looks like you have invalid syntax in your composer.json. What exactly wrong - difficult to say without seeing the actual content :) From the error, I'd suggest to take a look at some extra commas. Also, I noticed you tried to comment out the "sentry/sentry-symfony" line with /**/ comment? If so - it won't work, you can't use comments in compsoer.json file. If you don't want to have sentry in your composer.json - remove it instead of comment it out.
Also, I'd recommend you to open composer.json file in PhpStorm and it will highlight the problem spot for you I think :)
I hope this helps!
Cheers!
Hello Victor
Thanks for the quick response!
Yes, when i was having error with SENTRY_DSN then i tried to comment that line but after that i removed it, but for some reasons system was still referring to /**/ . It was cache. I am using Eclipse so i navigated between the tabs of composer.json - Overview, dependency Graph and Autoload and click save again on Composer.json and tried again and it worked.
Thank you so much for looking into this.
Appreciated!
// 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.17.5
"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
}
}
I'm running into problems just using the source code for this course. The source code doesn't contain the var en vendor directories. I copied those from a previous course and that magically worked. But now it says:
Fatal error: Uncaught Error: Call to undefined method Symfony\Component\Dotenv\Dotenv::bootEnv() in /somedir/Symfony/code-symfony-doctrine/start/public/index.php:10 Stack trace: #0 {main} thrown in /somedir/Symfony/code-symfony-doctrine/start/public/index.php on line 10
What can i do?