Ref: https://auth0.com/blog/symfony-tutorial-building-a-blog-part-1/
https://symfony.com/doc/current/setup.html
Install the latest Symfony 4 via Composer with the following command:
|
1 |
composer create-project symfony/website-skeleton blog |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
teddy@teddy:~/Documents/works/symfony4$ composer create-project symfony/website-skeleton blog Installing symfony/website-skeleton (v4.1.0) - Installing symfony/website-skeleton (v4.1.0): Downloading (100%) Created project in blog Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 92 installs, 0 updates, 0 removals - Installing ocramius/package-versions (1.3.0): Downloading (100%) - Installing symfony/flex (v1.0.80): Downloading (100%) Prefetching 59 packages 🎶 - Downloading (100%) - Installing doctrine/lexer (v1.0.1): Loading from cache - Installing doctrine/inflector (v1.3.0): Loading from cache - Installing doctrine/collections (v1.5.0): Loading from cache - Installing doctrine/cache (v1.7.1): Loading from cache - Installing doctrine/annotations (v1.6.0): Loading from cache - Installing doctrine/common (v2.8.1): Loading from cache - Installing symfony/polyfill-ctype (v1.8.0): Loading from cache - Installing symfony/polyfill-mbstring (v1.8.0): Loading from cache - Installing symfony/http-foundation (v4.1.1): Loading from cache - Installing symfony/event-dispatcher (v4.1.1): Loading from cache - Installing psr/log (1.0.2): Loading from cache - Installing symfony/debug (v4.1.1): Loading from cache - Installing symfony/http-kernel (v4.1.1): Loading from cache - Installing symfony/yaml (v4.1.1): Loading from cache - Installing symfony/webpack-encore-pack (v1.0.2): Loading from cache - Installing psr/link (1.0.0): Loading from cache - Installing fig/link-util (1.0.0): Loading from cache - Installing symfony/web-link (v4.1.1): Loading from cache - Installing symfony/translation (v4.1.1): Loading from cache - Installing symfony/validator (v4.1.1): Loading from cache - Installing twig/twig (v2.4.8): Loading from cache - Installing symfony/twig-bridge (v4.1.1): Loading from cache - Installing symfony/filesystem (v4.1.1): Loading from cache - Installing symfony/config (v4.1.1): Loading from cache - Installing symfony/twig-bundle (v4.1.1): Loading from cache - Installing psr/container (1.0.0): Loading from cache - Installing symfony/dependency-injection (v4.1.1): Loading from cache - Installing egulias/email-validator (2.1.4): Loading from cache - Installing swiftmailer/swiftmailer (v6.0.2): Loading from cache - Installing symfony/swiftmailer-bundle (v3.2.2): Loading from cache - Installing symfony/serializer (v4.1.1): Loading from cache - Installing symfony/inflector (v4.1.1): Loading from cache - Installing symfony/property-info (v4.1.1): Loading from cache - Installing symfony/property-access (v4.1.1): Loading from cache - Installing psr/simple-cache (1.0.1): Loading from cache - Installing psr/cache (1.0.1): Loading from cache - Installing symfony/cache (v4.1.1): Loading from cache - Installing webmozart/assert (1.3.0): Loading from cache - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache - Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache - Installing symfony/serializer-pack (v1.0.1): Loading from cache - Installing symfony/security (v4.1.1): Loading from cache - Installing symfony/security-bundle (v4.1.1): Loading from cache - Installing symfony/process (v4.1.1): Loading from cache - Installing symfony/routing (v4.1.1): Loading from cache - Installing symfony/finder (v4.1.1): Loading from cache - Installing symfony/framework-bundle (v4.1.1): Loading from cache - Installing zendframework/zend-eventmanager (3.2.1): Loading from cache - Installing zendframework/zend-code (3.3.0): Loading from cache - Installing ocramius/proxy-manager (2.2.0): Loading from cache - Installing symfony/console (v4.1.1): Loading from cache - Installing doctrine/dbal (v2.7.1): Loading from cache - Installing doctrine/migrations (v1.8.1): Loading from cache - Installing symfony/stopwatch (v4.1.1): Loading from cache - Installing symfony/doctrine-bridge (v4.1.1): Loading from cache - Installing doctrine/doctrine-cache-bundle (1.3.3): Loading from cache - Installing jdorn/sql-formatter (v1.2.17): Loading from cache - Installing doctrine/doctrine-bundle (1.9.1): Loading from cache - Installing doctrine/doctrine-migrations-bundle (v1.3.1): Loading from cache - Installing doctrine/instantiator (1.1.0): Loading from cache - Installing doctrine/orm (v2.6.1): Loading from cache - Installing symfony/orm-pack (v1.0.5): Loading from cache - Installing monolog/monolog (1.23.0): Loading from cache - Installing symfony/monolog-bridge (v4.1.1): Loading from cache - Installing symfony/monolog-bundle (v3.3.0): Loading from cache - Installing symfony/options-resolver (v4.1.1): Loading from cache - Installing symfony/intl (v4.1.1): Loading from cache - Installing symfony/polyfill-intl-icu (v1.8.0): Loading from cache - Installing symfony/form (v4.1.1): Loading from cache - Installing symfony/expression-language (v4.1.1): Loading from cache - Installing symfony/asset (v4.1.1): Loading from cache - Installing sensio/framework-extra-bundle (v5.1.6): Loading from cache - Installing symfony/polyfill-php72 (v1.8.0): Loading from cache - Installing symfony/var-dumper (v4.1.1): Loading from cache - Installing symfony/web-profiler-bundle (v4.1.1): Loading from cache - Installing symfony/profiler-pack (v1.0.3): Loading from cache - Installing easycorp/easy-log-handler (v1.0.5): Loading from cache - Installing symfony/debug-bundle (v4.1.1): Loading from cache - Installing symfony/debug-pack (v1.0.5): Loading from cache - Installing symfony/dotenv (v4.1.1): Loading from cache - Installing nikic/php-parser (v4.0.2): Loading from cache - Installing symfony/maker-bundle (v1.5.0): Loading from cache - Installing symfony/phpunit-bridge (v4.1.1): Loading from cache - Installing symfony/css-selector (v4.1.1): Loading from cache - Installing symfony/dom-crawler (v4.1.1): Loading from cache - Installing symfony/browser-kit (v4.1.1): Loading from cache - Installing symfony/test-pack (v1.0.2): Loading from cache - Installing symfony/web-server-bundle (v4.1.1): Loading from cache Writing lock file Generating autoload files ocramius/package-versions: Generating version class... ocramius/package-versions: ...done generating version class Symfony operations: 21 recipes (2a2112973248c715b69c3d31ba4e5d87) - Configuring symfony/flex (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/framework-bundle (>=3.3): From github.com/symfony/recipes:master - Configuring doctrine/annotations (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/webpack-encore-pack (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/translation (>=3.3): From github.com/symfony/recipes:master - Configuring symfony/twig-bundle (>=3.3): From github.com/symfony/recipes:master - Configuring symfony/swiftmailer-bundle (>=2.5): From github.com/symfony/recipes:master - Configuring symfony/security-bundle (>=3.3): From github.com/symfony/recipes:master - Configuring symfony/routing (>=4.0): From github.com/symfony/recipes:master - Configuring symfony/console (>=3.3): From github.com/symfony/recipes:master - Configuring doctrine/doctrine-cache-bundle (>=1.3.3): From auto-generated recipe - Configuring doctrine/doctrine-bundle (>=1.6): From github.com/symfony/recipes:master - Configuring doctrine/doctrine-migrations-bundle (>=1.2): From github.com/symfony/recipes:master - Configuring symfony/monolog-bundle (>=3.1): From github.com/symfony/recipes:master - Configuring sensio/framework-extra-bundle (>=4.0): From github.com/symfony/recipes:master - Configuring symfony/web-profiler-bundle (>=3.3): From github.com/symfony/recipes:master - Configuring easycorp/easy-log-handler (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/debug-bundle (>=4.1): From github.com/symfony/recipes:master - Configuring symfony/maker-bundle (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/phpunit-bridge (>=4.1): From github.com/symfony/recipes:master - Configuring symfony/web-server-bundle (>=3.3): From github.com/symfony/recipes:master Executing script cache:clear [OK] Executing script assets:install public [OK] Some files may have been created or updated to configure your new packages. Please review, edit and commit them: these files are yours. What's next? * Run your application: 1. Change to the project directory 2. Create your code repository with the git init command 3. Execute the php -S 127.0.0.1:8000 -t public command 4. Browse to the http://localhost:8000/ URL. Quit the server with CTRL-C. Run composer require server --dev for a better web server. * Read the documentation at https://symfony.com/doc Database Configuration * Modify your DATABASE_URL config in .env * Configure the driver (mysql) and server_version (5.7) in config/packages/doctrine.yaml How to test? * Write test cases in the tests/ folder * Run php bin/phpunit |
Update DotEnv File
In your root directory, there is a file called .env (usually this file is hidden. so set your system to show the hidden files), you should see something similar to the following:
|
1 2 3 4 |
... # Configure your db driver and server_version in config/packages/doctrine.yaml DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name ###< doctrine/doctrine-bundle ### |
Change to this:
|
1 |
DATABASE_URL=mysql://root:teddy@127.0.0.1:3306/symfony4_blog |
run the following command
|
1 |
php bin/console doctrine:database:create |
which will create a database with the value of your database name.
<!– START SKIP –>
IF YOU GOT ERROR LIKE MINE. IT’S BECAUSE MY PHP SETTING & APACHE2/NGINX SERVER DIDN’T WORK WELL. SKIP IT IF YOU DONT HAVE ANY ERROR LIKE THIS :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
teddy@teddy:~/Documents/works/symfony4$ cd blog/ teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console doctrine:database:create In AbstractMySQLDriver.php line 125: An exception occurred in driver: could not find driver In PDOConnection.php line 50: could not find driver In PDOConnection.php line 46: could not find driver doctrine:database:create [--shard SHARD] [--connection [CONNECTION]] [--if-not-exists] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> |
I NEED TO CHECK MY PHP VERSION. CLI ALREADY 7.2.
|
1 2 3 4 5 6 |
teddy@teddy:~/Documents/works/symfony4/blog$ php -v PHP 7.2.7-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jun 22 2018 08:44:50) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.7-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethan |
BUT THE SERVER STILL 7.0 (http://localhost/phpinfo.php). I ALREADY HAS 7.2 BUT SOME EXTENSIONS ARE MISSING. SO INSTALL THEM
|
1 |
sudo apt-get install php7.2-mysql php7.2-curl php7.2-cgi php7.2-mbstring php-mcrypt php7.2-zip php7.2-fpm php7.2-xml php7.2-xmlrpc php-xdebug php7.2-intl |
TO ENABLE PHP 7.2 ON APACHE2 SERVER, PLS DO (MAKE SURE NGINX IS STOPPED IF IT’S RUNNING AND START APACHE2):
|
1 2 3 4 |
sudo a2dismod php7.0 sudo a2dismod php7.1 sudo a2enmod php7.2 sudo systemctl restart apache2 |
TEST: http://localhost/phpinfo.php
IF NEEDED TO CHANGE THE PHP SETTING, PLS EDIT:
|
1 |
sudo gedit /etc/php/7.2/apache2/php.ini |
|
1 2 3 4 5 6 |
upload_max_filesize = 2000M -> (default 2M) max_file_uploads = 2000 -> (default 20) post_max_size = 2000M -> (default 8M) max_execution_time = 600 -> (default 30) in second max_input_time = 600 -> (default 60) in second memory_limit = 2560M -> (default 128M) |
TO ENABLE PHP 7.2 ON NGINX SERVER, PLS DO (MAKE SURE APACHE2 IS STOPPED IF IT’S RUNNING AND START NGINX):
THE NGINX MAIN CONFIG IN /etc/nginx/nginx.conf
THE DEFAULT VIRTUALHOST IN /etc/nginx/sites-available/default
EDIT IT TO ENABLE PARSING PHP FILE:
|
1 |
sudo gedit /etc/nginx/sites-available/default |
CHANGE THE CONTENT TO ENABLE PHP 7.2 FPM:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
... server { ... # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.2-fpm.sock; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } ... |
THEN CHANGE PHP7.2-FPM SETTING:
|
1 |
sudo gedit /etc/php/7.2/fpm/php.ini |
CHANGE THE CONTENT:
|
1 2 3 4 5 6 7 |
cgi.fix_pathinfo=0 upload_max_filesize = 2000M -> (default 2M) max_file_uploads = 2000 -> (default 20) post_max_size = 2000M -> (default 8M) max_execution_time = 6000 -> (default 30) in second max_input_time = 600 -> (default 60) in second memory_limit = 2560M -> (default 128M) |
THEN RESTART PHP7.2-FPM SERVICE:
|
1 |
sudo systemctl restart php7.2-fpm |
CHECK NGINX SYNTAX: sudo nginx -t
RESTART NGINX: sudo systemctl restart nginx
OPEN IT: http://localhost/phpinfo.php (NEED TO CLEAR THE BROWSER CACHE FIRST)
<!– END SKIP –>
OK. I FIXED THE CREATION DATABASE ERROR. RUN AGAIN
|
1 |
php bin/console doctrine:database:create |
HERE IS THE WORKING RESULT:
|
1 2 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console doctrine:database:create Created database `symfony4_blog` for connection named default |
Create the Blog Controller
Create new BlogController by running the following command
|
1 |
php bin/console make:controller |
When it asks for The class name of the controller to create, type in: BlogController.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console make:controller Choose a name for your controller class (e.g. FierceKangarooController): > BlogController created: src/Controller/BlogController.php created: templates/blog/index.html.twig Success! Next: Open your new controller class and add some pages! |
THEN CHANGE THE ROUTE IN src/Controller/BlogController.php
FROM:
|
1 2 3 |
/** * @Route("/blog", name="blog") */ |
TO:
|
1 2 3 |
/** * @Route("/", name="homepage") */ |
THEN TEST IT VIA SYMFONY WEB SERVER. RUN:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console server:run [OK] Server listening on http://127.0.0.1:8000 // Quit the server with CONTROL-C. PHP 7.2.7-1+ubuntu16.04.1+deb.sury.org+1 Development Server started at Mon Jun 25 21:57:17 2018 Listening on http://127.0.0.1:8000 Document root is /home/teddy/Documents/works/symfony4/blog/public Press Ctrl-C to quit. [Mon Jun 25 21:57:32 2018] 127.0.0.1:39972 [200]: / [Mon Jun 25 21:57:33 2018] 127.0.0.1:39974 [200]: /_wdt/675573 [Mon Jun 25 21:57:33 2018] 127.0.0.1:39976 [404]: /favicon.ico |
OPEN IT ON YOUR BROWSER: http://127.0.0.1:8000
THEN you’ll be shown a Hello BlogController! page.
Creating a New Author Entity
Create new Author entity by running the following command:
|
1 |
php bin/console make:entity |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console make:entity Class name of the entity to create or update (e.g. DeliciousGnome): > Author created: src/Entity/Author.php created: src/Repository/AuthorRepository.php Entity generated! Now let's add some fields! You can always add more fields later manually or by re-running this command. New property name (press <return> to stop adding fields): > Success! Next: When you're ready, create a migration with make:migration |
OPEN A PHP FILE src/Entity/Author.php, THEN REPLACE IT.
FROM:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\AuthorRepository") */ class Author { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; public function getId() { return $this->id; } } |
TO:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * Author * * @ORM\Table(name="author") * @ORM\Entity(repositoryClass="App\Repository\AuthorRepository") */ class Author { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255, unique=true) */ private $name; /** * @var string * * @ORM\Column(name="title", type="string", length=255) */ private $title; /** * @var string * * @ORM\Column(name="username", type="string", length=255, unique=true) */ private $username; /** * @var string * * @ORM\Column(name="company", type="string", length=255) */ private $company; /** * @var string * * @ORM\Column(name="short_bio", type="string", length=500) */ private $shortBio; /** * @var string * * @ORM\Column(name="phone", type="string", length=255, nullable=true) */ private $phone; /** * @var string * * @ORM\Column(name="facebook", type="string", length=255, nullable=true) */ private $facebook; /** * @var string * * @ORM\Column(name="twitter", type="string", length=255, nullable=true) */ private $twitter; /** * @var string * * @ORM\Column(name="github", type="string", length=255, nullable=true) */ private $github; /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set name * * @param string $name * * @return Author */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set title * * @param string $title * * @return Author */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set username * * @param string $username * * @return Author */ public function setUsername($username) { $this->username = $username; return $this; } /** * Get username * * @return string */ public function getUsername() { return $this->username; } /** * Set company * * @param string $company * * @return Author */ public function setCompany($company) { $this->company = $company; return $this; } /** * Get company * * @return string */ public function getCompany() { return $this->company; } /** * Set shortBio * * @param string $shortBio * * @return Author */ public function setShortBio($shortBio) { $this->shortBio = $shortBio; return $this; } /** * Get shortBio * * @return string */ public function getShortBio() { return $this->shortBio; } /** * Set phone * * @param string $phone * * @return Author */ public function setPhone($phone) { $this->phone = $phone; return $this; } /** * Get phone * * @return string */ public function getPhone() { return $this->phone; } /** * Set facebook * * @param string $facebook * * @return Author */ public function setFacebook($facebook) { $this->facebook = $facebook; return $this; } /** * Get facebook * * @return string */ public function getFacebook() { return $this->facebook; } /** * Set twitter * * @param string $twitter * * @return Author */ public function setTwitter($twitter) { $this->twitter = $twitter; return $this; } /** * Get twitter * * @return string */ public function getTwitter() { return $this->twitter; } /** * Set github * * @param string $github * * @return Author */ public function setGithub($github) { $this->github = $github; return $this; } /** * Get github * * @return string */ public function getGithub() { return $this->github; } } |
Creating a New BlogPost Entity
Create new BlogPost entity by running the following command php
|
1 |
bin/console make:entity |
When it asks for The class name of the entity to create, type in: BlogPost.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console make:entity Class name of the entity to create or update (e.g. OrangePizza): > BlogPost created: src/Entity/BlogPost.php created: src/Repository/BlogPostRepository.php Entity generated! Now let's add some fields! You can always add more fields later manually or by re-running this command. New property name (press <return> to stop adding fields): > Success! Next: When you're ready, create a migration with make:migration |
Once the command has finished running, you’ll find a new file in src/Entity called BlogPost.php. Open this and configure it like so:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * BlogPost * * @ORM\Table(name="blog_post") * @ORM\Entity(repositoryClass="App\Repository\BlogPostRepository") * @ORM\HasLifecycleCallbacks */ class BlogPost { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="title", type="string", length=255) */ private $title; /** * @var string * * @ORM\Column(name="slug", type="string", length=255, unique=true) */ private $slug; /** * @var string * * @ORM\Column(name="description", type="string", length=2000) */ private $description; /** * @var string * * @ORM\Column(name="body", type="text") */ private $body; /** * @var Author * * @ORM\ManyToOne(targetEntity="Author") * @ORM\JoinColumn(name="author_id", referencedColumnName="id") */ private $author; /** * @var \DateTime * * @ORM\Column(name="created_at", type="datetimetz") */ private $createdAt; /** * @var \DateTime * * @ORM\Column(name="updated_at", type="datetime") */ private $updatedAt; /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set title * * @param string $title * * @return BlogPost */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set slug * * @param string $slug * * @return BlogPost */ public function setSlug($slug) { $this->slug = $slug; return $this; } /** * Get slug * * @return string */ public function getSlug() { return $this->slug; } /** * Set description * * @param string $description * * @return BlogPost */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set body * * @param string $body * * @return BlogPost */ public function setBody($body) { $this->body = $body; return $this; } /** * Get body * * @return string */ public function getBody() { return $this->body; } /** * Set author * * @param Author $author * * @return BlogPost */ public function setAuthor(Author $author) { $this->author = $author; return $this; } /** * Get author * * @return Author */ public function getAuthor() { return $this->author; } /** * Set createdAt * * @param \DateTime $createdAt * * @return BlogPost */ public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; return $this; } /** * Get createdAt * * @return \DateTime */ public function getCreatedAt() { return $this->createdAt; } /** * Set updatedAt * * @param \DateTime $updatedAt * * @return BlogPost */ public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; return $this; } /** * Get updatedAt * * @return \DateTime */ public function getUpdatedAt() { return $this->updatedAt; } /** * @ORM\PrePersist */ public function prePersist() { if (!$this->getCreatedAt()) { $this->setCreatedAt(new \DateTime()); } if (!$this->getUpdatedAt()) { $this->setUpdatedAt(new \DateTime()); } } /** * @ORM\PreUpdate */ public function preUpdate() { $this->setUpdatedAt(new \DateTime()); } } |
Although you have created these entities, your database still has no tables in there. Based off these entities, Doctrine can create the tables we’ve specified. In order to do this, all you have to do is run:
|
1 |
php bin/console doctrine:schema:update --force |
|
1 2 3 4 5 6 7 8 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console doctrine:schema:update --force Updating database schema... 3 queries were executed [OK] Database schema updated successfully! |
Install Doctrine-Fixtures
We want to just populate some data into the newly created tables as examples during the creation of the blog. So install doctrine-fixtures.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
teddy@teddy:~/Documents/works/symfony4/blog$ composer require --dev doctrine/doctrine-fixtures-bundle Using version ^3.0 for doctrine/doctrine-fixtures-bundle ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Prefetching 2 packages 🎶 - Downloading (100%) Package operations: 2 installs, 0 updates, 0 removals - Installing doctrine/data-fixtures (v1.3.1): Loading from cache - Installing doctrine/doctrine-fixtures-bundle (3.0.2): Loading from cache Writing lock file Generating autoload files ocramius/package-versions: Generating version class... ocramius/package-versions: ...done generating version class Symfony operations: 1 recipe (77fb7f8c687c54f4792ae78c50b1ea23) - Configuring doctrine/doctrine-fixtures-bundle (>=3.0): From github.com/symfony/recipes:master Executing script cache:clear [OK] Executing script assets:install public [OK] Some files may have been created or updated to configure your new packages. Please review, edit and commit them: these files are yours. |
Create Author and BlogPost Fixtures
Create a new file (and the directories the file is stored in) under src/DataFixtures/ORM/Fixtures.php and insert the following into the file:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\DataFixtures\ORM; use App\Entity\Author; use App\Entity\BlogPost; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\Persistence\ObjectManager; class Fixtures extends Fixture { public function load(ObjectManager $manager) { $author = new Author(); $author ->setName('Joe Bloggs') ->setTitle('Developer') ->setUsername('auth0-username') ->setCompany('The Writing Company') ->setShortBio('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages.') ->setPhone('070000000') ->setFacebook('joebloggs') ->setTwitter('joe.bloggs') ->setGithub('joe-bloggs'); $manager->persist($author); $blogPost = new BlogPost(); $blogPost ->setTitle('Your first blog post example') ->setSlug('first-post') ->setDescription('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.') ->setBody('Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.') ->setAuthor($author); $manager->persist($blogPost); $manager->flush(); } } |
Running Fixtures
Let’s run the fixtures! php bin/console doctrine:fixtures:load
|
1 2 3 4 |
teddy@teddy:~/Documents/works/symfony4/blog$ php bin/console doctrine:fixtures:load Careful, database will be purged. Do you want to continue y/N ?y > purging database > loading App\DataFixtures\ORM\Fixtures |