// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.1.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.0.4
"symfony/console": "^4.0", // v4.0.14
"symfony/flex": "^1.0", // v1.21.6
"symfony/framework-bundle": "^4.0", // v4.0.14
"symfony/lts": "^4@dev", // dev-master
"symfony/twig-bundle": "^4.0", // v4.0.4
"symfony/web-server-bundle": "^4.0", // v4.0.4
"symfony/yaml": "^4.0" // v4.0.14
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.4
"fzaninotto/faker": "^1.7", // v1.7.1
"symfony/debug-bundle": "^3.3|^4.0", // v4.0.4
"symfony/dotenv": "^4.0", // v4.0.14
"symfony/maker-bundle": "^1.0", // v1.4.0
"symfony/monolog-bundle": "^3.0", // v3.1.2
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.0.4
"symfony/stopwatch": "^3.3|^4.0", // v4.0.4
"symfony/var-dumper": "^3.3|^4.0", // v4.0.4
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.0.4
}
}
42 Comments
Hi,
In the Article Entity I see a Column named "publishedAt" but in SQL DB is renamed to "published_at"
Is this because of datetime type?
Hey Teo
Nope, that's because of the convention being followed by Doctrine, you can choose the "underscore" or "camelcase" strategy, or create your own strategy. You can check the full config list here: https://symfony.com/doc/cur...
Cheers!
Hello,
We know with "php bin/console make:entity" we can add a new property to an existing Entity, so my question is how can we remove a property, change/update/add a relation of an property using "php bin/console make:entity".
I know we can edit directly and manually from the Entity.php file but I prefer using the make command. Please give example if it is possible to do so with make command.
Thanks very much!
Dung.
Hey Dung L. !
> so my question is how can we remove a property, change/update/add a relation of an property using "php bin/console make:entity".
> I know we can edit directly and manually from the Entity.php file but I prefer using the make command.
As you guessed, it's not possible with the make:entity command. I created this command... and it was a TON of work. What you're asking about... *might* be possible... but it also might be unreliable - e.g. what if you tell the command to remove a field called "name" but you've also added a custom method beyond just getName() and setName() like getFullName() which uses this property. Should the command remove that custom method? It's tricky... which is a big reason why it's not implemented.
So, sorry I can't give you the answer you want - but I hope this is a small detail ultimately :).
Cheers!
Thanks for sharing your insight, I do not want to miss the goodness offered by Symfony, at the very least I feel good that something is not only tricky for me but for you too :) - JK!
Good Morning,
When I update an entity with a new field this is what I get
Then I must manually typed and changed to the below to point this entity property to the corresponding database field. Should this syntax be auto generated? if so how do I make sure it happens every time I add new field?
Thank you!
Hey Dung L.
You only have to tweak your ORM config. It should use the "underscore" naming strategy instead of camel case.
Cheers!
Hi MolloKhan ,
what is the annotation
can I remove it completely or it needs to be there to reference field "total_point" in the database?
Best regards,
I believe you don't need to specify the table name anymore but you can leave it as a future reference
Got that, Merry Christmas MolloKhan !
Merry Christmas to you too! (A bit late :p)
Hello there,
How do I use bin/console command line related doctrine & make bundle to set primary key property on a an existing db field/entity property in symfony. Or this has to be done using annotation?
Can you please instruct or point me to some sort of documents to learn from? I have tried googling but found no resources for this question.
Thank you very much for any help!
Thank you anyways, I found the answer to my question here https://www.doctrine-projec... so it is within doctrine document.
Hey Dung,
Glad you figured it out yourself! And thanks for sharing the link with others, might be useful
Cheers!
Hi, i don't have autocomplete for variables in twig template... for example i write article and nothing... Last phpstorm and other things are up-to-date...
Hey Stranger
Do you have Symfony plugin installed and configured?
Cheers!
Hmm created new project from that files... and now it suggests.... strange bug... thanks anyway!
Stranger
Great that it's solved now, There are sometimes issues with suggestions, most of time it's because of phpstorm caches, if you face with this problem again, try to invalidate caches and restart PHPStorm
Cheers!
Yes of cause... everywhere it works... for example it suggests |ago , but it doesn't suggest article object :(
Hey Stranger
I'm so sorry for so late answer, but honestly I can't say exactly why it's so. Probably it's related to PHPStorm configuration, the best I can advice for you is to re-check every option from PHPStorm preferences > Languages & Frameworks > PHP > Symfony and after it Invalidate caches and restart
Cheers!
Stranger
-try to debug it here is the code taken from https://symfony.com/doc/4.1...
{# templates/article/recent_list.html.twig #}
{# the contents of this variable are sent to the Web Debug Toolbar #}
{% dump articles %}
{% for article in articles %}
{# the contents of this variable are displayed on the web page #}
{{ dump(article) }}
{{ article.title }}
{% endfor %}
-make sure you see what/data you expect to be suggested!
-if you do not see what/data you expected check your Entity/Article.php and make sure they are properly generated!
Hello there,
I added a property to existing entity the lazy way by ... make:entity, then make:migration, then doctrine:migrations;migrate. Everything went ok/green. But I have a question to ask: Why in my Migrations version I have so many queries when I only added 1 property namely "image_1o". I pasted the file in here please help me understand.
Thanks so much as usual!
`
/**
Auto-generated Migration: Please modify to your needs!
*/
final class Version20191017191622 extends AbstractMigration
{
public function getDescription() : string
{
}
public function up(Schema $schema) : void
{
...
...
...
d_fk FOREIGN KEY (remark_id) REFERENCES remark (id)');
}
}
`
Hey Dung L.
Whenever you create a migration via doctrine or a maker command it will synchronize your current database schema to your application (entities metadata). I believe your database schema was just not in sync. There is a command for checking it
php bin/console doctrine:schema:validateCheers!
Hi MolloKhan , sorry I just figured out how to find this conversation using Disqus, never knew that before. After I ran the command:
Can you please suggest me what I need to do properly, I would like to correct this error before my project goes too far.
Thanks Diego!
Hey Dung! If you feel lost among all the changes that you have made to your schema. What I usually do is to commit everything, go to the master branch, re-create the database (drop, create, create schema), then go back to my deving-branch and just run
bin/console doctrine:migration:diff. By doing so, you end up with one migration file containing all the difference between master and your branchI hope this helps. Cheers!
Hi MolloKhan , I understand your suggestion, its clean and straight forward. Unfortunately, my story is a bit different. I started out the project with database creation first! I built the database populate with data then create and connect an application to it. Now, I know I should have populated the db tables in schema using Doctrine:schema:update within Symfony.
So now I am well into the project, and trying to make:entity, then make:migration, then doctrine:migrations;migrate - this is where I am standing, i got me to this trouble.
I do not know if you can still give me advice to get me back on track so that I can migrate from Symfony's doctrine. But I will try to back up my database then drop/create and re-generate the database with my current Entities and doctrine annotation to see if that will reset me to a clean working and proper doctrine/entity migration capability.
I will update this thread after I try. Thanks Diego!
Hello Dung L. , Good news! Thinking of your suggestion, I thought I could apply it in my circumstance, so i backed up code as well database just in case things go wrong. Then I create a new empty schema (no tables yet) pointed my application to the new schema, removed all Versions.php under Migrations directory, cleaned out cache, then php bin/console doctrine:schema:validate, sure it is not in sync ([ERROR] The database schema is not in sync with the current mapping file.) Now I ran "php bin/console make:entity --regenerate" then " php bin/console doctrine:schema:update --dump-sql" to review only, then "php bin/console doctrine:schema:update --force" then "php bin/console make:migration" then "php bin/console doctrine:migrations:migrate" --- Surprisingly things went with out error :). And now I have a clean and tight app entities and dbase. Gotta love Symfony and Doctrine. Thank you as usual!
The database tables were created, in mysql the keys, indices, and foreign keys were not named the same as I had before because they are auto-generated by doctrine in Symfony, but that s not issue rather nicety.
That's great! If you have differences in your table and column names, you can always specify their names manually. e.g.
Thank you MolloKhan , got that!
it very wrong to get image path straight from the Article entity (getImagePath()), because the entity is only about storing its DB-related data, and must <b>never</b> be aware in what folder the images are actually stored;
instead, you should've added a Twig filter, e.g.
new TwigFilter('image_path', [$this, 'getImagePath']),with actual implementation like:
`public function getImagePath($value)
{
}`
Hey Yaroslav Y.
You're right but it was done in this way for simplicity/teaching reasons. The bundle VichUploaderBundle is very good at it, if you ever have to upload files, you may want to give it a try
Cheers!
Hi Ryan
I've just come back to this video from the next in the series as I have an issue with the make:entity command. When I now use make:entity I am prompted for the name but after typing the name "Comment" it doesn't give me the option to add any fields, it just creates the class with the id field. I can't use the make:entity command again to add fields (as it describes here although it did work when doing this tutorial, I just get an error, [ERROR] The file "src/Entity/Comment.php" can't be generated because it already exists.
Am I missing something?
Cheers
Steve
I've just run composer:update and all is now good.
Hey Steve,
Glad you got it working yourself! It was an easy fix ;)
Cheers!
When I use src="{{ asset(article.imagePath) }}" it renders a beginning slash <img class="article-img" src="/images/asteroid.jpg"> that goes nowhere. I figured out that I mispelled jpeg and put jpg instead. Why did it put a slash when it could not find the file?
Hey Skylar!
Ah, great question :). So, it is less magic than you might be thinking. First, here is some FULL information about what the asset function does in Symfony: https://knpuniversity.com/s...
Now, here is the shorter explanation: the asset() function ALWAYS adds a slash at the beginning. It doesn't know/care if the file exists - it doesn't do any checks for that. It just adds the slash in all cases. Actually, one of the reasons it exists is that, if you deploy your application under a subdirectory - e.g. example.com/my-app, then it will prefix the path with /my-app/. So, it *always* adds either an opening slash or the app's subdirectory, automatically. If you check out the HTML of the working version, you should see a slash there too :).
I hope that helps! Cheers!
Hello,
Just a minor confusion... you said here, that we should give the property a value so we can have a default value for the column, but i found a topic on SO saying that @ORM\Column(type="bigint", options={"default": 1}) is a better practice . Do you have any thoughts on which one is better and why? Should i do both?
Hey Petru L.!
Ah, very good catch :). So, it depends. Personally, setting it in PHP is much more important to me: I want my PHP object to have the value that will eventually be stored in the database. To use your example, if you ONLY have the "default": 1, then your object might have a null value for this field in PHP, but suddenly it changes to 1 in the database. That's strange to me.
However, I think the
options={"default": 1}can be a useful "extra" thing you can add... if you want to (I don't use this, but I can see why some people do). With this config (in addition to defaulting the value in PHP), you make your database a bit more flexible. For example, if you ever need to manually hack a new row manually via an INSERT query, this default value will be reflected. So, it's up to you - but I don't think options= is a better practice, it's just an extra practice, which can be good, depending on how much you care about the underlying database :).Cheers!
Hi Petru L.,
just wanted to add some thoughts:
* Doctrine FAQs recommend setting default values using properties: https://www.doctrine-projec...
* Doctrine will not go back to the database after persisting a new object to get the default values
* if you rely on your DB to set default values you cannot create easier, faster tests for your entities (without using the DB)
* there could be problems when default values for e.g. date types are different per DBMS (when your live DBMS is MySQL but your tests run on SQLite for example)
* double check on Doctrine Forms overwriting your default values
Because of these reasons we almost always stick to solely setting defaults in the entities themselves.
EDIT: as this is about adding fields to entities, you might have problems with existing rows for a new column which should be not null and but has no default value...
"as this is about adding fields to entities, you might have problems with existing rows for a new column which should be not null and but has no default value"
Well, for me that's a HUGE problem.
Imagine when you have a project in production and need to add NOT NULL (and no default) field. You cannot just truncate database table. Doctrine migrations become somewhat unusable in this case.
And it's not something special or rare: these kind of fields are actually the most prevalent ones.
Unfortunately I cannot see any reasonable solution rather than manually edit migration files to enter proper default values.
Hey Ivan!
This is precisely what I do in these situations: if your database column is NOT NULL and has no default, then you absolutely need to add some custom code in your migration to handle it - I totally agree this is the correct approach. I usually: create the column but allow NULL, set value on that column for all rows with the next query, and finally use one more ALTER TABLE to make it NOT NULL. Doctrine migrations are great for structure changes - but it can't correctly determine how to handle data in these situations.
Cheers!
Great answer! :)
"Houston: no signs of life"
Start the conversation!