tags:

views:

193

answers:

3

Hi,

I'm pretty used to Django. With Django you define your models for the ORM and it takes care of generating / altering the database itself for storing the data based on the models.

In php i'm looking for a similar solution. Doctrine seems to be the standard php ORM, but, as far as I've seen, you have to write the DB and then attach to it, or script the database creation, then generate the models and start working.

Is there a one step model definition to db creation with Doctrine or another php orm solution? Thanks.

+2  A: 

Not 100% the same but I use Propel (http://propel.phpdb.org/trac/) when in need for ORM in PHP. It has been around for a while and it is quite mature. It is also part of the Symfony Framework. Handling is not quite as nice as in Django but still pretty convenient.

markmywords
does it allow a one-step solution for creating the models and accordingly create /alter the db?
pistacchio
It creates the database and tables for you. Thus, you do not have to bother with database stuff. Propel standalone is not a all in one solution in itself. For this you might want to check out http://www.symfony-project.org/ - a PHP Framework that utilizes and automizes Propel. There you generate your apps and models via the command line and let it build your db too.However, most of the time I am fine with using Propel alone and glue my code to it as it is rather well thought and I do not mind doing one or two little extra steps.
markmywords
+2  A: 

Are you that only the ORM in Django itself is responsible for creating the DB and all that stuff?

But Doctrine can do the setup automatically. I copied the most important part:

// generate.php

require_once('bootstrap.php');

Doctrine_Core::dropDatabases();
Doctrine_Core::createDatabases();
Doctrine_Core::generateModelsFromYaml('schema.yml', 'models');
Doctrine_Core::createTablesFromModels('models');

schema.yml contains your table definitions in YAML format.

As you are already talking about frameworks, you might have a look at Symfony which does pretty much the things you want and which provides an even easier interface for it (than Doctrine itself). You can either use Propel or Doctrine as ORM.

Felix Kling
A: 

phpDataMapper was designed specifically to do this. There are no distributed shell or cli scripts that come with it, but you can do this from your codebase:

Setup adapter:

try {
    $adapter = new phpDataMapper_Adapter_Mysql('localhost', 'db', 'root', '');
} catch(Exception $e) {
    echo $e->getMessage();
    exit();
}

Define your mapper/table fields:

class PostMapper extends phpDataMapper_Base
{
    // Specify the data source (table for SQL adapters)
    protected $_datasource = "blog_posts";

    // Define your fields as public class properties
    public $id = array('type' => 'int', 'primary' => true, 'serial' => true);
    public $title = array('type' => 'string', 'required' => true);
    public $body = array('type' => 'text', 'required' => true);
    public $status = array('type' => 'string', 'default' => 'draft');
    public $date_created = array('type' => 'datetime');
}

Instantiate mapper with adapter(s), and setup tables:

$postMapper = new PostMapper($adapter [, $adapterSlave]);
$postMapper->migrate();

http://phpdatamapper.com/documentation/getting-started/

Vance Lucas