Updating fields doctrine symfony mysql - symfony

Here is the code on the console.
ubuntu1256:~/httpdocs/site$ php app/console doctrine:schema:update --dump-sql
ALTER TABLE campaign CHANGE adresse_email_business_partner adresse_email_business_partner VARCHAR(255) DEFAULT NULL
ubuntu1256:~/httpdocs/site$ php app/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "1" queries were executed
I continue to force but it doesn't updates correctly. Any ideas? I wanted to add a field in a class.

Related

Previously executed migration are not registered migrations

I'm trying to update my database with those commands
php bin/console make:migration
this return success
But when I try
php bin/console doctrine:migrations:migrate
I have this error:
WARNING! You have 5 previously executed migrations in the database >>that are not registered migrations.
>> 2018-12-17 10:42:04 (20181217104204)
>> 2018-12-17 13:19:24 (20181217131924)
>> 2018-12-17 13:40:58 (20181217134058)
>> 2018-12-18 10:41:38 (20181218104138)
>> 2018-12-18 13:15:49 (20181218131549)
Thing is, the database listed here are not in my migrations table from my database and they are not in my Migrations folder either.
How can I remove those wrong migrations ? Thanks.
This is a year old, but I've had problems a few times where I delete old migration files because they aren't relevant or whatever reason and had the same issue. I think the correct way to handle this is to delete references from the table directly.
php bin/console doctrine:query:sql "delete from migration_versions where version = '2020181217104204'";
EDIT - newer versions of Symfony are now using a "doctrine_migration_versions" table.
php bin/console doctrine:query:sql "delete from doctrine_migration_versions where version = '2020181217104204'";
Etc..
Encounterd the same probleme : I had previously copied already executed migration to the newly created migration table (due to doctrine update).
Renaming all version names as follow saved the day : 20190408092436 --> DoctrineMigrations\Version20190408092436

Cannot generate Entity from existing Oracle DB

Following symfony's documentation to create entities from exiting database,I get an error when running the first command:
php bin/console doctrine:mapping:import --force AppBundle yml
The error is:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: file_put_contents(C:\WebApp\src\AppBundle/Resources/config/doctrine/"user".orm.yml): failed to open stream: Invalid argument
How can I fix it?
Looks like User is a reserved word within Oracle databases, so when I created the table, Oracle created a table named "user" with double quotes. So when trying to generate the entity, doctrine cannot find the table user and found an invalid argument "user" with double quotes in it. The way I solved the problem is by using another name other than user.
Hope this helps someone on the future.
php bin/console doctrine:mapping:import --force AppBundle yml
change to
php bin/console doctrine:mapping:import AppBundle yml --force
more details:
The doctrine:mapping:import command imports mapping information
from an existing database:
php app/console doctrine:mapping:import "MyCustomBundle" xml
You can also optionally specify which entity manager to import from with the
--em option:
php app/console doctrine:mapping:import "MyCustomBundle" xml --em=default
If you don't want to map every entity that can be found in the database, use the
--filter option. It will try to match the targeted mapped entity with the
provided pattern string.
php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity
Use the --force option, if you want to override existing mapping files:
php app/console doctrine:mapping:import "MyCustomBundle" xml --force

Symfony2 "php app/console doctrine:schema:update --force" doens't add a column to table

I am trying to add a column to a table through doctrine command:
php app/console doctrine:schema:update --force
I know it can do that, since I've done that before and documentation states that clearly:
"In other words, if you add a new property with mapping metadata to Product and run this task again, it will generate the "alter table" statement needed to add that new column to the existing product table."
The following is the field I'm trying to add as a column:
/**
* #var string
*
* #ORM\Column(name="service_machine_name", type="string", length=40, nullable=false)
*/
private $serviceMachineName;
But when I type:
php app/console doctrine:schema:update --force
It tells me that my database is in sync with my current entity, but it clearly is not, because I have an additional field in my entity class.
I also tried shortening the column name to 3 letters (just in case), but it still doesn't add a column!
Note: I have added columns to a different table on the same database (which is remote) and it worked fine before.
You have to first create setter and getter for that columns like this
php app/console doctrine:generate:entities Acme/DemoBundle/Entity/User
after that you have to use
php app/console doctrine:schema:update --force
This answer is correct: https://stackoverflow.com/a/22198323/2400373
But don't work in symfony 3, for this version is necessary a change, the code is:
You have to first create setter and getter for that columns like this
php bin/console doctrine:generate:entities Acme/DemoBundle/Entity/User
after that you have to use
php bin/console doctrine:schema:update --force

Symfony2: deleted property still used in query

I deleted a property (teaser) from an enitiy (sale).
I used the CLi to update the database & entites:
$ php app/console doctrine:schema:update --complete --force
$ php app/console doctrine:generate:entities Our --no-backup
$ php app/console doctrine:cache:clear-metadata
$ php app/console doctrine:cache:clear-result
And i cleared the cache:
$ php app/console cache:clear --no-optional-warmers --no-warmup
Now i have a query created by query builder:
$qb = $this->createQueryBuilder('s');
$q = $qb
->select('s.id')
->leftJoin('s.bills','b')
->where('b.paid = true')
->getQuery()
->getResult();
This throws an error becuas eit generates a select statement that includes the old property:
Column not found: 1054 Unknown column 's0_.teaser' in 'field list'
I tried to understand how Doctrine executes the query but failed to find the source of the problem.
Does anyone have any clues?
UPDATE: I took the DQL from the query, generated by the querybuilder and then used that to create a new Query. Same Error. Then i changed one word in the DQL from uppercae to lowercase (LEFT is now Left) and that one executed just fine.
$q = $this->getEntityManager()
->createQuery('SELECT s,b FROM Our\BuyBundle\Entity\Sale s Left JOIN s.bills b WHERE s.id IN (:randomIds)')
->setParameter('randomIds', $allSaleIds);
So it seems the DQL Statement is cached somewhere, Using the DQL as a hash to get the corresponding SQL Statement.
I still can't quite figure out where and how to clear that cached Information. Especially given the fact that i deleted all content in the app/cache folder as well.
UPDATE2: Found the Solution. Had to clear the APC Cache manually since the console can't do that.
So the Solution would be:
$ php app/console doctrine:cache:clear-query
And if that doesn't work, clear your APC Cache.
My Problem was the APC Cache.
I could not clear the query-cache in CLI (app/console) since the console can't (or is not allowed to) clear the APC Cache.
So i had to clear the APC Cache manually.
Solution is:
$ php app/console doctrine:cache:clear-result
And if that doesn't work, clear the APC Cache (or whatever cache you use) manually.

Symfony2 generate entities error

I'm trying to generate entities from existing tables in database, I keep getting the same error in a particular entity, which is imported from a second database (or entity manager), but I re-created this table in main database to use the same entity manager and get the same error, so I'm lost about what is happening.
This are my commands to generate it:
php app/console doctrine:mapping:convert yml ./src/MyShop/ProductBundle/Resources/config/doctrine/metadata/orm --from-database --filter="Product" --em=mysecondaryem
(BTW, is there a way to force exact filter? I only need Product)
Which seems to be ok:
Processing entity "ProductSold"
Processing entity "Product"
Exporting "yml" mapping information to...
Then
php app/console doctrine:mapping:import MyShopProductBundle annotation --em=mysecondaryem
Which is weird, as it is logging information about all other tables existing in this db but it's only generating the corresponding to "Product" as filtered (only the files ProductSold.php and Product.php do really exist):
Importing mapping information from "mysecondaryem" entity manager
> writing C:\mysite\src\MyShop\ProductBundle/Entity/ProductSold.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Family.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Category.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Item.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Stock.php
> writing C:\mysite\src\MyShop\ProductBundle/Entity/Product.php
Then, in the third step, I get an error:
php app/console doctrine:generate:entities MyShopProductBundle --no-backup
Generating entities for bundle "MyShopProductBundle"
[RuntimeException]
Bundle "MyShopProductBundle" does not contain any mapped entities.
doctrine:generate:entities [--path="..."] [--no-backup] name
As I sayed, I tried omitting secondary entity manager (replicating tables in my first database) and I get the same error.
You've verified that C:\mysite\src\MyShop\ProductBundle/Entity/Product.php has been created, but have you also checked that it actually contains mapped entities ?
Ok, I found the error, I was missing --force parameter, first step command should be:
php app/console doctrine:mapping:convert yml ./src/MyShop/ProductBundle/Resources/config/doctrine/metadata/orm --from-database --force --filter="Product" --em=mysecondaryem

Resources