Doctrine is not retrieving new columns - doctrine-orm

I'm using the default find() method to get data, every thing was ok until I've added a new property to the entity : the same code is not retrieving the new column, I don't see it in the generated SQL query !
I've added other properties with different types and the problem remains : new properties aren't visible in the SQL query !
This is the result of doctrine:schema:validate :
Mapping
-------
[OK] The mapping files are correct.
Database
--------
[ERROR] The database schema is not in sync with the current mapping file.
So it seems the schema is not ok, how can I find the problem ?
UPDATE 1
I have another project with no database error. I've updated an existing entity :
bin/console make:entity
Then updated the schema :
bin/console doctrine:schema:update --force
But the new column is not retrieved !
UPDATE 2
I've generated a new entity with the same properties and Doctrine returns all the columns. I've made a diff between the two entities and the two repositories : they are identical !
I've cleared Doctrine's cache :
php bin/console doctrine:cache:clear-metadata
php bin/console doctrine:cache:clear-query
php bin/console doctrine:cache:clear-result
...but the problem persists

It was a Doctrine cache issue, here is a part on my doctrine.yaml :
orm:
metadata_cache_driver: apcu
result_cache_driver: apcu
query_cache_driver: apcu
I've tried to clear the cache php -r "apcu_clear_cache();" : no effect ! So I've changed the cache :
orm:
metadata_cache_driver: array
result_cache_driver: array
query_cache_driver: array
And now it works fine :)

Related

column "xx_xx" can only be updated to DEFAULT - DJANGO

Im trying to update some values (via django) at a table on Postgres that contains a Generated column.
This is the error im getting:
column "xx_xx" can only be updated to DEFAULT
See complete error here
You cannot edit generated column in a way other columns are saved/updated
As PostgreSQL documentation states
A generated column cannot be written to directly. In INSERT or UPDATE commands, a value cannot be specified for a generated column,
but the keyword DEFAULT may be specified.

Doctrine Migration from string to Entity

I've an apparently simple task to perform, i have to convert several tables column from a string to a new entity (integer FOREIGN KEY) value.
I have DB 10 tables with a column called "app_version" which atm are VARCHAR columns type. Since i'm going to have a little project refactor i'd like to convert those VARCHAR columns to a new column which contains an ID representing the newly mapped value so:
V1 -> ID: 1
V2 -> ID: 2
and so on
I've prepared a Doctrine Migration (i'm using symfony 3.4) which performs the conversion by DROPPING the old column and adding the new id column for the AppVersion table.
Of course i need to preserve my current existing data.
I know about preUp and postUp but i can't figure how to do it w/o hitting the DB performance too much. I can collect the data via SELECT in the preUp, store them in some PHP vars to use later on inside postUp to write new values to DB but since i have 10 tables with many rows this become a disaster real fast.
Do you guys have any suggestion i could apply to make this smooth and easy?
Please do not ask why i have to do this refactor now and i didn't setup the DB correctly in the first time. :D
Keywords for ideas: transaction? bulk query? avoid php vars storage? write sql file? everything can be good
I feel dumb but the solution was much more simple, i created a custom migration with all the "ALTER TABLE [table_name] DROP app_version" to be executed AFTER one that simply does:
UPDATE [table_name] SET app_version_id = 1 WHERE app_version = "V1"

Doctrine Nothing to update after adding onDelete="CASCADE"

I want to add the option onDelete="CASCADE" on one of my attributes via the #JoinColumn annotation:
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Product",mappedBy="category",fetch="EAGER")
* #ORM\JoinColumn(onDelete="CASCADE")
*/
private $products;
But when I try to update with php bin/console doctrine:schema:update --force , I always get:
nothing to uptade - database already sync.
I tried to add some other attributes and I got the same issue. However, if I intentionally add a mistake I get an error as expected.
How can I fix this?
The #OneToMany annotation is the one you use on the inverse side of your many-to-one association. The table storing the entities on this side of the association does not hold any foreign key pointing to the table storing your Product entities, thus there is no "join column" there.
The documentation states the following about #JoinColumn:
This annotation is used in the context of relations in #ManyToOne, #OneToOne fields and in the Context of #JoinTable nested inside a #ManyToMany.
In your case, the annotation does not apply to any column at all and consequently, your database does not need to be updated.
If you wish to have Product entities related to a given Category removed through cascade operations by your database, you have to add a #JoinColumn(onDelete="CASCADE") on the owning side of the association, next to the #ManyToOne annotation of the category attribute of Product.

Using C++ SQL to Add Relationship to Microsoft Access Table

I'm trying to add a CONSTRAINT/RELATIONSHIP between two Microsoft Access Tables using C++ (MFC-based) (through ODBC) with the following SQL Command:
ALTER TABLE [JobSettingsReporting]
ADD CONSTRAINT FK_PerEntry
FOREIGN KEY (JobSetID)
REFERENCES JobSettingsEntry(JobSetID)
ON UPDATE CASCADE
ON DELETE CASCADE
I tried to do it with this:
CDatabase db;
db.OpenEx(App().GetDBConnectString());
db.ExecuteSQL(strSQL);
db.Close();
where strSQL is the Query above. I'm getting the error:
Syntax error in CONSTRAINT clause.
It seems the problem is with the "ON UPDATE CASCADE" and "ON DELETE CASCADE" components. I get the same error when I use the Query Design tool (or in VB) within Microsoft Access.
However, when I do the following in VB in Microsoft Access, it works:
Dim cn As ADODB.Connection 'For action queries
Set cn = CurrentProject.Connection
cn.Execute "ALTER TABLE [JobSettingsReporting]
ADD CONSTRAINT FK_PerEntry
FOREIGN KEY (JobSetID)
REFERENCES JobSettingsEntry(JobSetID)
ON UPDATE CASCADE
ON DELETE CASCADE"
This command also seems to work in SQL Server Express when directly using the Query Window.
It is my understanding that I need to use Jet/ADO to issue this command in C++ for it to work but I'm having trouble finding the syntax. I need to add this relationship programmatically so that it will update the databases on all the deployed machines.
Any help would be appreciated.

Doctrine ORM error : Unknown column type varchar requested

Ok, so i just assigned a new project which uses Doctrine and Zend. This is my first time using Doctrine, and i came upon an error for which google didn't came up with any answer.
I added a new field (VARCHAR 17) to a table, added getter/setter functions in the Entity for that table, then ran orm:generate-proxies.
All good, except now i am getting this error when trying to save anything: Unknown column type varchar requested.
Any thoughts?
The problem is, that doctrine annotation doesn't know about the underlying database. So what you have to do is mark your field as type string with length of 17 in your case:
/**
* mySuperField
* \ORM\Column(name="mySuperField", type="string", length=17)
*/
$mySuperField;
For reference about how to map entity properties see also Doctrine Basic Mapping
At first learn about doctrine commands
orm:generate-entities to write getter/setter functions in the Entity files,
orm:schema-tool:update to update db tables,
you should not do this work manually, just write yaml/xml/php schema and run them.
If you use Bisna library to integrate doctrine2 with zf, there must be "adapterClass" and "mappingDirs[]" options in application.ini file to describe where schema files are.
Use type "string" instead of varchar in entity and schema files.
I prefer yaml schemas:
username:
type: string
length: 17