I am working on the project in which Doctrine is used. So I started learning Doctrine and write simple test and stuck at the very beginning. I have developed lot of cakePHP apps before and I have good idea about cake bake.
I have a test mysql database and 2 tables in it. I want to generate all model classes to start with and I want to include them in controllers of my MVC app to CRUD.
I installed Doctrine using composer and got a folder called /vendor inside project folder. Created a new bootstrap.php inside project folder
/vendor
/bootstrap.php
code inside bootstrap.php is
<?php
// bootstrap.php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entities-or-mapping-files");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'test',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);
I see that there is a method called Doctrine_Core::generateModelsFromDb in older version.
I just want basic models to start with and want to know how to include them in other PHP files to CRUD.
I understand you want to generate a set of entity classes and mapping information from a database. With Doctrine you need both the entity classes and the mapping information for them. The mapping information is basically a bunch of .yml or .xml files. Both things can be generated from a database using the tools provided by Doctrine. This is usually done once at the beginning of your project. From the mapping information Doctrine can automatically generate PHP classes you can use in your code (e.g to persist entitites). The mapping information can also be added as pseudo-annotations in classes but I don't have experience with that so I won't get into that.
The command you're looking for is
php doctrine orm:convert-mapping --from-database yml /path/to/mapping-path-converted-to-yml
This assumes you have configured Doctrine to access your database already.
Once you have done this you can generate the entities with
php doctrine orm:generate-entities
This should create the classes used for CRUD operations. To use the classes you just need to include them in your code and then use an EntityManager object to operate on them.
More information on reverse engineering can be found here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html#reverse-engineering
By the way, if you have a schema in a MySQL Workbench file there are scripts that can generate entity classes and the mapping information from it.
Related
I am currently writing test cases for views, Which eventually uses database also.
By default a test database is being created and removed after test are run.
As the database itself is development database, I don't want my test to create a separate db but use exciting only.
Also I will like to bring to your notice, that in my environment, database are created and provided and django can't run migration or create database.
How can I create unittest which uses real database ?
I think the primary reason for this question is because your django database user is not provided with the create/drop database permission.
Django needs to create and drop a test database for the purpose of unit testing. It cannot use an existing database for this purpose. Why we are not allowed to use an existing database in the unit test is because, the data can be modified by anyone who has the same database permission and django may not have control over the updates they make, This might end up in an unsuccessful unit test.
This is clearly explained in another question's answer
If your DB Admin can provide your Django user the required access for the Test module to work as expected, You can make use of the Fixtures. Fixtures are like data files, can be created from your development environment and then can be used in the Unit test Setup to import the data from Fixtures to the test database created by Django.
The ultimate purpose of any Unit test framework will be to test the functionality of the Back end code logic with a data which is not likely to change. As mentioned in the above links, The Functional testing and Regression Testing is there to cover the real database.
For more details on Fixtures visit Using Fixtures with Django Test Cases
I'm using Django for backend, but for some reason, I want to use Laravel beside Django and share the database between them. so the same database for Django and Laravel. but the problem is that Django migrations are not equal to Laravel migrations so the database is different from ( for example constraints and indexes and some other options).
Is this going to break backend if I use Django as the primary database and use Laravel as a secondary backend?
If true, how I can use Django and Laravel in the same database?
Your database does not depend on Django or Laravel. It just stores data.
Constraints, triggers, indexes, etc are stored on the database itself, and they are completely independent of your framework. Frameworks just abstract the methods and provide easy methods to manage your database. At the core, they use the same commands which are provided by the database. The names of constraints are irrelevant, you can give whatever names you want, frameworks just provide their own uniform naming pattern, which can be customized by the user. So they can be used on both Django and Laravel or any other framework/programming language. That's the main purpose of having a database, to store data in a structured manner so it can be used by any language/framework
Since you already have migrations in Django, there's no need to create the migrations again in laravel. Just reuse the Django database and make your laravel application to properly handle the data (that part is completely in your control)
Can you suggest a ruby gem or show a snippet with exsmple how to make AtiveRecord::Base successor to choose a db connection depending on a model record id?
Ok. Please let me know if the following solution can work if not then I will delete it from here.
I wouldn't use Gem for this.
Here a mud map how I would tackle this project. ( for small to medium projects only )
Ruby Class: manages all the connections.
Query handler: dictates what connection do we need based on the
condition.
Connector is a simple class that grabs stuff from
Rails and trigger the query handlers.
Query handler is the optional bit. You can connect your Rails directly to the Ruby Class. ( I don't like this as they will be coupled a bit too much )
Have your database.yaml file (just to keep Rails happy) Your application will be using the Ruby Class connector and query handler to grab stuff from different databases, No Rails needed there.
Hope this help!
PS: Again if you think this is not good solution let me know and I see what I can do. Cheers mate.
I would like to use Zend Framework 2 with Doctrine 2 and openLDAP. My goal is to create a persistence for my LDAP. How can I accomplish this in ZF2?
I noticed that we can get objects from LDAP with Zend\Ldap\Node.
Can anyone show me an example of how to make a search from a LDAP and convert the results into nodes and/or maybe into a Collection of Nodes?
I'd like to have objects to work on after the search.
Thanks
Well, this may come "a little" late, but just to give others pointers on how to do this. I have created a Module called SamLdapUser which runs its authentication against Ldap / AD. A sample configuration is provided.
The persistence layer (user-object in application) is created through a second Module. This Module hooks in to the getIdentity() function of the authentication Service and then queries the database for the user-object. If no object is existing, a new object will be created. You can check the source code for my module SamLdapUserObject, too.
I am building a web interface for a database at my school. The database will hold our school's versions of academic standards.
When you build a site using django, does it create a clean database? For example, wysiwyg website builders like dreamweaver create ugly html and css code on the backend. I would hate to see a similar degree of auto-generated cruft in my database.
Should I create the database myself and then build a django site to access the database, or go ahead and let django create the database?
Under any simple to moderately complex application, Django will do a fine job creating the database for you. I've yet to run into any issues with what it's made.
I would suggest that you use South to handle your table migrations. And use virtualenv and pip to set up and maintain your Django environment.
You can use the sqlall predicate of manage.py to see the exact SQL that will be executed in order to generate the database.
Obviously django needs database tables for its basic functionality (contrib.apps).
Sure, you don't have to use them, but generally you want to use a least contrib.auth and some other bundled apps:
Each of these applications makes use
of at least one database table,
though, so we need to create the
tables in the database before we can
use them.
I any case you can't and shouldn't compare it to ugly html code generated by dreamweaver or word.
On a more abstract level:
One of key concepts of a web framework (following the mvc pattern) is that you define models which are "translated" (mapped) by the framework into database tables.
A model is the single, definitive
source of data about your data. It
contains the essential fields and
behaviors of the data you’re storing.
Generally, each model maps to a single
database table.
If you want to create the whole database scheme by hand you totally missed the point of using a web framework. In most cases you simply don't need to write sql manually. You define your classes and then you can query your objects using the builtin orm.