I have this database in which all tables have this format:
Somedata
and the names of the columns are like this:
UserInformation
Basically, the first letter is capitalized, and Doctrine cannot find the table/column
I tried adding a naming strategy:
naming_strategy: doctrine.orm.naming_strategy.underscore
But that didn't work.
I also tried to set MySQL lower case table names to 1
lower_case_table_names = 1
That didn't work as well. If I change all the names of the tables and columns to lowercase, works as expected. However, I cannot do that in the production database, so how can make Doctrine find the tables with that format?
By the way, the tables are already created, and I cannot modify them
You have 2 choices here:
Implement custom naming strategy where you generate table names as you wish. Check the docs Doctrine Naming Strategy
Set desired table name in every entity: #ORM\Table(name="SomeName")
UPDATE:
Found the solution. I had to write the table name in the Entity like this:
#ORM\Table(name="`Somename`")
Source:
Doctrine - PostgreSQL - Uppercase and Spaces in Table / Field
names
Related
Currently, while using doctrine migration, FK names are dechex(crc32()) encoded column names.
This is a problem when, I want to start using Doctrine migration for my existing Symfony + Doctrine application, which has really huge DB.
And Index names are also HEX-encoded names.
The problem is, we really don't want to rename all our current ones to hex'y names, because tables are huge.
Need suggestions.
Every time I run doctrine:migrations:diff to generate migration for my changes it always includes removal of a few tables that are not handled by doctrine eg.:
$this->addSql('DROP TABLE messenger_messages');
$this->addSql('DROP TABLE monitoring');
Is there a way to tell doctrine that specific tables do not belong to him so doctrine will stop trying to drop them every time?
You can find your answer in the docs : https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html#manual-tables
Short answer : add prefix to your custom tables, then configure this prefix (for instance if your custom tables start by 't_') :
doctrine:
dbal:
schema_filter: ~^(?!t_)~
You can use a regex to exclude tables from doctrine field of view.
To specify a list of tables that should not be touched by doctrine just add this to config:
doctrine:
dbal:
schema_filter: ~^(?!(messenger_messages|monitoring|foo|bar)$)~
This will prevent doctrine from manipulating those four tables:
messenger_messages
monitoring
foo
bar
Thanks #Diabetic Nephropathy for hinting the way with regex.
Can anyone explain what's the conceptual difference between #UniqueEntity validator, #UniqueConstraint table annotation and unique=true option of #Column annotation.
I understand that #UniqueConstraint adds UNIQUE index on database level and #UniqueEntity validates on ORM level. So what option shall I use, or do I use all of them?
#UniqueConstraint and unique=true are part of Doctrine and do similar thing.
When you set unique=true on a particular column, then Doctrine will create a unique key on this column physically in database.
#UniqueConstraint can be used to create a unique key in database on multiple columns (complex unique key). But if you pass a single column, then the result will be exactly the same as using unique=true on that field.
#UniqueEntity on the other hand is not a part of Doctrine, but it's a part of Symfony framework. While options above are used by Doctrine to generate proper schema, this one is just a validator used usually by Symfony Form Component at time of submitting the form.
So to answer your final question - yes, you usually should use both #UniqueEntity and one of #UniqueConstraint or unique=true.
As stated in documentation, #UniqueConstraint annotation is used for creation of unique constraint on multiple columns, when unique=true is used for unique constraint on one column.
UniqueEntityValidator exists to show friendly error message and unique database constraint's purpose is to make sure you don't store duplicate data.
So the answer to your question is like this - you should use both database constraint and #UniqueValidator.
I'm working on a symfony application, and i need to insert multiple raws at once, Doctrine ORM is not a good option because for each raw it will open a connection to execute the query, to avoid this and have one connection inserting all the raws i used prepared statement of doctrine dbal and it works fine, except i need to get the ids of the inserted raws, it seems the only available function is lastinsertedid which returns only the last id not all the last inserted ones, how can i achieve this?
any help would be appreciated!
This is actually not related to doctrine at all. If you want all inserted id's it must be possible in MySQL. "It's unlikely that if doctrine don't have batch insert it will support returning list of ids after batch insert :)"
Check answers related to MYSQL:
How can I Insert many rows into a MySQL table and return the new IDs?
MySQL LAST_INSERT_ID() used with multiple records INSERT statement
But it's possible in postgresql (since you didn't mention you DB):
Retrieving serial id from batch inserted rows in postgresql
You can actually generate IDs before inserting content into database. For example, using random UUIDs.
This library might be of use: https://github.com/ramsey/uuid
use Ramsey\Uuid\Uuid;
$uuid4 = Uuid::uuid4();
echo $uuid4->toString()
I have a table where I store relations between two other table with a float indicating relevance between the two rows. I have unique key on the two reference columns. The problem is, that I have to rebuild it regularly, which is kinda lengthy. So, I can't just truncate the table while I'm doing so, but I can't insert the relevance for the same row while they would be duplicates either. That's why I thought it would be great to save the current timestamp to a variable, persist the relations with REPLACE INTO instead of INSERT INTO and then remove everything created before the saved timestamp. However, I couldn't find out how. Any idea? I'm using Doctrine 2.2.0-DEV and Symfony 2.1.0-DEV.
Doctrine 2 doesn't support INSERT REPLACE or UPSERT, sorry.