【问题标题】:How to create table with doctrine entity in codeigniter如何在codeigniter中创建带有学说实体的表
【发布时间】:2014-12-06 10:42:35
【问题描述】:

我正在将 Doctrine 集成到 codeigniter 中。我想在运行我的项目或链接时使用实体自动创建表,我该怎么做?

【问题讨论】:

    标签: database codeigniter doctrine-orm codeigniter-2


    【解决方案1】:

    修改你的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);
        }
    
    }
    

    并在 models 文件夹中创建文件夹 Entity 在该文件夹中创建名为 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;
        }
    
    }
    

    现在,如果您点击您的网址,它将自动在您的数据库中创建test table

    【讨论】:

    • 出现此错误需要(application/models\Entity\Test.php):无法打开流:没有这样的文件或目录
    • 你是否在你的模型文件夹中创建了 test.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多