Deleting table using Doctrine2 and Symfony2 - php

How can I delete table using Doctrine2 and Symfony2? I've generated entities and updated schema, now I want to delete this structure.

Not sure if I understood your question correctly. You deleted an entity and want to delete also its generated table from database? If so:
You can't do it, because Doctrine2 only cares about tables it knows - that means the ones that are represented by Entities. Since you deleted some Entity from your application, Doctrine no longer thinks the table belongs to your application. There are situations when there are different tables of different applications in the same database. It wouldn't make sense if Doctrine deleted them just because it doesn't know anything about them. It would be racist... but against tables.
If you just want to drop tables programmatically, you can use raw query. As far as I know, Doctrine doesn't have any method for dropping tables. Or as an alternative, you can do it by hand.

You can do a raw sql.
For example in a Symfony2 controller:
$em = $this->getDoctrine()->getManager();
$sql = 'DROP TABLE hereYourTableName;';
$connection = $em->getConnection();
$stmt = $connection->prepare($sql);
$stmt->execute();
$stmt->closeCursor();

Just delete the tables which you are no longer using manually...Doctrine totally ignores unmapped tables.

In Doctrine 2.6.2 you can DROP table by deleting entity class, after migrations:diff you will get new file with DROP TABLE query, migrations:migrate will execute the command dropping desired table.
Class that was about to get deleted had relationships with other entities, so I had to brutally (manually) delete all mentions of old entity class in other entities.
Just tested.

Related

Does redbean php consider created mysql-triggers?

I am using redbeanphp 4.3 and struggling to understand why a mysql-db-trigger does not get triggered (or may be ignored).
When I work directly in the database (with phpmyadmin) and insert a row in table B, a column in table A gets updated.
When I work indrectly with the database (via a rest-api and therefore with redbean php), the row is inserted as well in table B, but the column in table A does not get updated.
This exact same behaviour I found in my android application with sugarorm too.
I then reasoned, that using an ORM ignores the usage of triggers. But so far I did not find any statement support my thesis, not in some forums, the redbean documentation nor in the rd.php source code.
An explanation or any help would be appreciated.
Thanks in advance.
Edit 1:
A script using mysqli and pdo-driver does result in the same behaviour as manual insertion (the column gets updated). Therefore PDO can not be the reason.
Solution:
Table A and B have an 1:N relation.
The problem was a misuse of redbean's R::store() method. The object1 of table A was loaded, an object2 of table B was inserted (by R::store()), the trigger was executed, but the object1 of table A did not recognize it. A final R::store() on object1 has overwritten the background updates to the initial state.
So the solution is quite simple: removing R::store() on objects of table B.
When R::store() is called on object1 of table A, it first updates entity A and inserts entity B afterwards, which will trigger the update of object1.
Try to debug the sql querys created by redBean and check if there is a column update or something else after your insert.
Configure redBean to log sql querys:
R::debug( TRUE, 3 );
Access and print the log:
$logs = R::getDatabaseAdapter()->getDatabase()->getLogger()->getLogs();
print_r( $logs );
You will find more information about debugging redBean on their official site.

Symfony2 field order in database table

I have generated 2 entities in my bundle and then using app/console doctrine:schema:update --force I recreate database schema. The order of fields that I have defined in the entities does not match the order of fields in the database table. Is there a way to control/define order in which fields are listed in the database table?
Thanks,
Z
It's not a problem; If you have generated the schema first then you updated it, the new fields will be just appended to current columns.
Furthermore you can find this different order for columns which has relationship which will be appended, too (on the generation time). In fact it does not create any issue; but if it's important to you, you can change the arrangement through one of the DataBase Management Tools (e.g. phpMyAdmin)

Symfony2 and Doctrine: One to Many relationship

I am writing an application in Symfony2 and Doctrine. Here's all the codes that might be necessary:
https://gist.github.com/3440325
This code block works fine and creates the relationship correctly:
$twitter->setUser($user);
$skype->setUser($user);
Working correctly means it creates a row in the users table and inserts the correct user id in the handles table.
Where as, this code block doesn't work as expected:
$user->addHandle($skype);
$user->addHandle($twitter);
It successfully inserts all the entries but can't insert the correct user id in the handles table. As a matter of fact, the user_id column remains empty.
What is going wrong here? Am I missing something? Is my expectation incorrect or there is some bug some where?
-- Masnun
Since you have a bidirectional one-to-many relationship, you need to set referenced entities on both sides synchronously.
public function addHandle(\WeCodePHP\HomeBundle\Entity\Handle $handles)
{
$this->handles[] = $handles;
$handles->setUser($this);
}
Otherwise doctrine won't guess what a handle belongs to.

Doctrine 2 multi-level OneToOne Cascade

I have three Doctrine entities: Device, which has a OneToOne relationship with Device\Status, which in turn has a OneToOne relationship with Device\Status\Battery.
I have {cascade="persist"} set between the related entities, and from what I've read, that should be all that is required for Doctrine to automatically persist each of the entities without having to do anything myself in the code.
Here's what I'm having problems with:
$device = new \Entities\Device();
$device->setId(100);
$status = $device->getStatus();
$status->setIpAddress('192.168.0.1');
$battery = $status->getBattery();
$battery->setInternalLevel(60);
$em->persist($device);
$em->flush();
After executing this code, I get the following error:
Entity of type Device\Status\Battery has identity through a foreign entity
Device\Status, however this entity has no identity itself. You have to call
EntityManager#persist() on the related entity and make sure that an identifier
was generated before trying to persist 'Device\Status\Battery'. In case of
Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL)
this means you have to call EntityManager#flush() between both persist
operations.
My question is: what is the correct way to setup my entities to ensure that they're persisted in the correct order?
The code for the entities can be found here: https://gist.github.com/1753524
All tests have been performed using the Doctrine 2.2 sandbox.
I think #CappY is right.
The problem is in the Status entity. when you do getBattery() and create a new Battery instance, it's related to the Status instance on which you called getBattery().
Since that instance hasn't been stored in the database yet, it's id hasn't been generated (because it's annotated as #GeneratedValue). you're almost right about cascade persist. except for that it's performed in memory.
So you need to persist and flush Status entity before doing getBattery() if you want to use that entity as id in Battery. Or else you could simple add an id field for Battery :)
You have to add cascade={"persist"} to your relation mapping. The answer you selected as correct is also correct but with that solution, if anything goes wrong after the parent data is inserted, there will be no transaction rollback. You have to set autocommit=false and perform commit transaction manually. With cascade={"persist"} you dont have to. Anything goes wrong during database action, everything will be rollbacked.

How to implement non explicit 1:1 relationship with Doctrine?

I have two tables, Inventory and, say, Stuff. Inventory is used to store data common to Stuff and other tables. The way the DBA envisioned this working would be with us inserting the Inventory table and then using the generated ID to insert the Stuff table.
How can I implement this scenario using Doctrine 2? I'm tempted to just add a 1:1 relationship on the model but I'm not sure I can convince the DBA to change the database.
With the workaround described here http://www.doctrine-project.org/docs/orm/2.0/en/reference/limitations-and-known-issues.html#foreign-keys-as-identifiers you should be able to get the DBAs schema working. With version 2.1 of Doctrine (or the current master) you can use the new foreign key as identifier feature to get it working.
However if you are not using Sequences of Oracle/Postgresql you need to flush operations for this (persist parent, flush, associate and persist child, flush)

Categories