I'm trying to start using MongoDB with Doctrine MongoDB ODM 1.1.3 and Laravel 5.4. Everything had been going fine before I removed the database called "doctrine" manually (the default database name I guess) in order to clean up the rubbish in it, so basically I just wanted to remove the database and was hoping that Doctrine will create a new one. Now when I'm trying to call
$mgr->persist($divRoot);
$mgr->flush();
It assigns an ID to the $divRoot object, but doesn't persist it. I.e. when I then call the findAll() method on the repo, it returns nothing. And it doesn't create any databases anymore. The $divRoot has changing fields every time I'm trying to save it. I've got really stuck, please help
UPDATE 1
If I initialize a new DocumentManager specifying a new path to the documents (AnnotationDriver::create($documents)), the ODM works normally persisting and retrieving the documents.
I've figured out what was wrong. I was working only with embedded documents whereas it must be at least one root Document. Originally all of them were marked as Documents that's why I was able to persist them, it's nothing to do with the database removal.
So, I was trying to persist a composite consisting out EmbeddedDocuments only.
The solution was to create a root wrapper for the composite marked as Document.
Related
There does not seem to be proper documentation available on how to configure and use database views with the doctrine migrations bundle.
One probably is not able to map SQL statements which will end up creating/updating a database view (from the sql given somewhere) when migrations:diff and migrations:migrate are run.
If an entity is mapped to a database view with the #table(name="view_name") markup, it ends up causing an error / new table being attempted, instead of understanding that its a database view being used.
Is there a solution? Am I missing something?
I'm not sure that doctrine can get out of the box views. For all I know, you'll have to cheat.
Or:
I think you have to write the migration script yourself.You can generate an empty one and then write the create-statements into it.
In the repository you integrate native sql. The result you map to your entity or DTO.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/native-sql.html
I am using MongoDB as a replacement for MySQL in Zend Framework 2, I was wondering, is there any CREATE TABLE statement like thing in MongoDB to create collections programmatically, most preferably within ZendFramework 2? Or maybe what is the approach one needs to take while creating DBs and collections when working with MongoDB?
Collections are created automatically in MongoDB as soon as you try to save a document to them.
If you really, really want to have an empty collection, try inserting any document to it to create it and then delete the document. But generally it's best to just let MongoDB do its thing.
I hope this helps.
In MongoDB, a collection is created implicitly when you first insert a document into it. Nevertheless, you can create a collection explicitly with MongoDB by using the command db.createCollection(). This command will also allow you to pass options specifying the nature of the collection, such as whether it is a capped collection, what sort of validation it should have, indexing options, etc. The syntax for MongoDB 3.2 is as follows:
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document> });
For more information, you can visit this page in the documentation.
Regarding database creation, as of MongoDB 3.2 there is no method to explicitly create a database. So in order to create a database, you need to insert a document to a collection inside it, or create a collection directly using db.createCollection().
I have a little problem. I am using Apigility within Doctrine to do a one project.
But every time I try to get tables in the database to generate the entities, never finds...
Is there something I forgot to set?
I understand what's going on. It was my mistake even imagined by this screen I could generate the entities automatically.
But from what I saw I would have to generate them through the terminal and just select them later on Apigility Admin.
A few key notes on my environment:
I have an app that is built on Wordpress and we're using Doctrine as our ORM.
In order to maintain WP's integrity, we let WP handle the creation of users
Every WP object and custom object is mapped properly through Doctrine.
The situation is:
// 1. Create a user via **WP** function (which returns ID)
$wp_user_id = wp_insert_user($wp_user_array);
// 2. Then, I need to immediately retrieve that user object via Doctrine
$wp_user = $MyDb->em->getRepository('WpUsers')->findOneBy(array('id'=>$wp_user_id));
// 3. RESULT = NULL
The object is not found, presumably because the Entity is cached or stored in memory somewhere by Doctrine.
How can I force Doctrine to go look at the DB and get this newly created user?
You can clear doctrine cache with
EntityManager::clear()
See http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html (search for clear method)
The solution for this ended up being fairly simple. After trying clear(), refresh() and force clearing Doctrine's cache I was running out of options.
It turns out what I had to do to get Doctrine to check that the new record was created, is close and open the connection:
$this->_em->getConnection()->close();
$this->_em->getConnection()->connect();
Not the most ideal solution, I know, but it works and I have been debugging this for five days now!
What I'm trying to figure out is how to add new fields to a table, using Symfony2 with Doctrine2.
I used this to initially create the Entity:
php app/console doctrine:generate:entity --entity="MyMainBundle:ImagesTable" --fields="title:string(100) file:string(100)"
And I used this to create/update the tables on the database:
php app/console doctrine:schema:update --force
Now if I wanted to add new fields to the ImagesTable entity, is there an easy way to do it using the console, or do I have to manually edit the entity. I am just using 1 entity as an example right now, but in reality, there are many entities I'd be changing; so, there has to be an easier way to do it.
I've been manually editing them to create relationships, so if there is an easier way to do that as well, that'd be great.
I remember this being a lot easier with Symfony1.4 - all I had to do was create the database/tables using phpMyAdmin, and Symfony was able to generate the models with no issues.
I really hope I'm missing something here, because this won't work if I have to manually edit every entity for every change.
Doctrine generator commands are intended to help the developer to quickly prototype an idea. They generally don't produce production ready code, and the code needs to be checked to see if it contains what you want.
You can still create your model in phpmyadmin and use Doctrine reverse engineering tools, but it also doesn't produce production ready code, only intended to use in prototyping.
Creating database/tables beforehand doesn't really work well with Doctrine2, as the underlying relation between tables may not be the same as the relation between objects of your model. The whole point of ORM is to think in classes and letting Doctrine do the rest of the work for you.
Doctrine is not intended to write your entities for you, it gives you tools to build your data model, which you use to code your model in Php.
If you don't like to code your entities by hand (which is what all developers using doctrine does), you may want to have a look at RedbeanPHP, a zero-config ORM framework for PHP. It creates the database tables, columns, indexes on the fly depending on the data model you use.