如何在codeigniter中创建具有doctrine实体的表

时间:2021-12-20 06:46:20

I am integrating Doctrine in codeigniter. I want to create table automatically with entity when running my project or a link, How can i do that?

我在Docignine中集成了Doctrine。我想在运行我的项目或链接时自动创建表,我该怎么做?

1 个解决方案

#1


0  

modify your library/doctrine.php

修改你的library / doctrine.php

<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;

class Doctrine {

    public $em;

    public function __construct() {
        require_once __DIR__ . '/Doctrine/ORM/Tools/Setup.php';
        Setup::registerAutoloadDirectory(__DIR__);

        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver' => 'pdo_pgsql', // change to yours
            'user' => $db['default']['username'],
            'password' => $db['default']['password'],
            'host' => $db['default']['hostname'],
            'dbname' => $db['default']['database'],
            'charset' => $db['default']['char_set'],
            'driverOptions' => array(
                'charset' => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $dev_mode = false;

        // If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
        // to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);

        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
        $classes = array(

        $this->em->getClassMetadata('Entity\Test'),
        $tool->updateSchema($classes);
    }

}

and in models folder create folder Entity in that folder create file called as test.php

并在模型文件夹中创建文件夹实体在该文件夹中创建名为test.php的文件

<?php

namespace Entity;

/**
 * @Entity
 * @Table(name="test")
 */
class Test {

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    protected $id = null;


    /**
     * @Column(type="datetime", name="created_at", nullable=false)
     */
    protected $created_at = null;

    /**
     * @Column(type="datetime", name="updated_at", nullable=false)
     */
    protected $updated_at = null;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * set id
     *
     * @return integer 
     */
    public function setId($id) {
        $this->id = $id;
        return $this;
    }

    /**
     * Set created_at
     *
     * @param datetime $created_at
     */
    public function setCreated_at($created_at) {
        $this->created_at = $created_at;
        return $this;
    }

    /**
     * Get created_at
     *
     * @return datetime 
     */
    public function getCreated_at() {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param datetime $updated_at
     */
    public function setUpdated_at($updated_at) {
        $this->updated_at = $updated_at;
        return $this;
    }

    /**
     * Get updated_at
     *
     * @return datetime 
     */
    public function getUpdated_at() {
        return $this->updated_at;
    }

}

now if you hit your url it will automatically create test table in your database

现在,如果你点击你的网址,它将自动在你的数据库中创建测试表

#1


0  

modify your library/doctrine.php

修改你的library / doctrine.php

<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;

class Doctrine {

    public $em;

    public function __construct() {
        require_once __DIR__ . '/Doctrine/ORM/Tools/Setup.php';
        Setup::registerAutoloadDirectory(__DIR__);

        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver' => 'pdo_pgsql', // change to yours
            'user' => $db['default']['username'],
            'password' => $db['default']['password'],
            'host' => $db['default']['hostname'],
            'dbname' => $db['default']['database'],
            'charset' => $db['default']['char_set'],
            'driverOptions' => array(
                'charset' => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $dev_mode = false;

        // If you want to use a different metadata driver, change createAnnotationMetadataConfiguration
        // to createXMLMetadataConfiguration or createYAMLMetadataConfiguration.
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);

        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($this->em);
        $classes = array(

        $this->em->getClassMetadata('Entity\Test'),
        $tool->updateSchema($classes);
    }

}

and in models folder create folder Entity in that folder create file called as test.php

并在模型文件夹中创建文件夹实体在该文件夹中创建名为test.php的文件

<?php

namespace Entity;

/**
 * @Entity
 * @Table(name="test")
 */
class Test {

    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     */
    protected $id = null;


    /**
     * @Column(type="datetime", name="created_at", nullable=false)
     */
    protected $created_at = null;

    /**
     * @Column(type="datetime", name="updated_at", nullable=false)
     */
    protected $updated_at = null;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId() {
        return $this->id;
    }

    /**
     * set id
     *
     * @return integer 
     */
    public function setId($id) {
        $this->id = $id;
        return $this;
    }

    /**
     * Set created_at
     *
     * @param datetime $created_at
     */
    public function setCreated_at($created_at) {
        $this->created_at = $created_at;
        return $this;
    }

    /**
     * Get created_at
     *
     * @return datetime 
     */
    public function getCreated_at() {
        return $this->created_at;
    }

    /**
     * Set updated_at
     *
     * @param datetime $updated_at
     */
    public function setUpdated_at($updated_at) {
        $this->updated_at = $updated_at;
        return $this;
    }

    /**
     * Get updated_at
     *
     * @return datetime 
     */
    public function getUpdated_at() {
        return $this->updated_at;
    }

}

now if you hit your url it will automatically create test table in your database

现在,如果你点击你的网址,它将自动在你的数据库中创建测试表