Installing Propel in CodeIgniter

INSTALLING PROPEL IN CODEIGNITER

Download CodeIgniter http://ellislab.com/codeigniter

Download Propel ORM http://propelorm.org/download.html

Inside CodeIgniter application directory, create the following directories:
build
build/data

Note: Anywhere is fine, this is just for organization

Unzip Propel ORM, copy the generator directory and rename to propel in

application/build/data

Copy build.properties-sample from build/data/propel to build/data without the ‘-sample’

Add the following data to build.properties with your own variables

propel.project = ci
propel.database = mysql
propel.database.url = mysql:host=localhost;dbname=demo
propel.database.user = root
propel.database.password = password

Then at the last part of the same file add:


propel.project.dir = ${propel.home}
propel.output.dir = ${propel.project.dir}/output
propel.conf.dir = ${propel.project.dir}
propel.sql.dir = ${propel.output.dir}/sql
propel.graph.dir = ${propel.output.dir}/graphs
propel.omtar.dir = ${propel.output.dir}
propel.php.dir = ../../../models
propel.phpconf.dir = ../../../config
propel.addGenericAccessors = true
propel.addGenericMutators = true

The last 2 lines is to enable the PropelArrayParser so that you could use the functions toArray, toJSON, toCSV etc.

Important, set targetPackage to '.' so that propel would not create ci directory inside the models directory like this models/ci/your_models

propel.targetPackage = .

Also, we can override this in schema.xml by adding package=”PackageName” to the <database> or <table> tag

Next, create the files runtime-conf.xml and buildtime-conf.xml then add the code below with your own variables

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <!-- Uncomment this if you have PEAR Log installed
    <log>
        <type>file</type>
        <name>/path/to/propel.log</name>
        <ident>propel-ci</ident>
        <level>7</level>
    </log>
    -->
    <propel>
        <datasources default="ci">
            <datasource id="ci">
                <!-- sqlite, mysql, mssql, oracle, or pgsql -->
                <adapter>mysql</adapter>
                <connection>
                <dsn>mysql:host=localhost;dbname=demo</dsn>
                <user>root</user>
                <password>password</password>
                </connection>
            </datasource>
        </datasources>
    </propel>
</config>
Change directory to application/build/data/propel/bin then run any of following according to your needs:

Creating Schema from existing database

propel-gen path/to/build.properties reverse

The generated schema is at build/data/schema.xml

Generating the database sql

propel-gen path/to/build.properties sql

The generated sql file is located in the build/data/output/sql directory

Generating database models

propel-gen path/to/build.properties om

The database models can be found at application/models

Since in this demo, we are two directories inside data where build.properties is located, we can type: propel-gen ../../ [taskname]

Next, build the php version of runtime-conf.xml

propel-gen path/to/build.properties convert-conf

The config file is at application/config
classmap-codeigniter-conf.php 
codeigniter-conf.php    

Go back to Propel ORM, copy the runtime/lib directory on application/libraries

Rename lib to propel

Change directory to application/libraries, then create Propel.php

Copy the code in Propel.php

class CI_Propel
{
    public function CI_Propel()
    {
        define('DS',DIRECTORY_SEPARATOR);
    
        // EDIT for Virtual hosts
        set_include_path(get_include_path().PATH_SEPARATOR.APPPATH.'models/'); 
        require dirname(__FILE__) . DS . "propel" .DS . 'Propel.php';
        Propel::init(APPPATH . DS ."config".DS."codeigniter-conf.php");
    }
}

remember to replace codeigniter-conf.php with your own runtime config file

Open application/config/autoload.php then set the following:

$autoload['libraries'] = array('propel'); // on L55
$autoload['helper'] = array('url','file'); // on L67

Now on your controller, you can create propel queries like this:

 $query = UsersQuery::create()->limit(50)->find();


SOURCES

Integrating Propel Into CodeIgniter
http://tenfastfeet.com/interact/blog-archive/39-blog/235-codeigniter-series-integrating-propel-into-codeigniter

Using Propel with Wampserver
http://blog.leonardchallis.com/databases/using-propel-with-wampserver/

Using Propel with CodeIgniter
http://davidcondemarin.blogspot.com/2011/01/using-propel-with-codeigniter.html

Propel
http://propelorm.org/documentation/02-buildtime.html

http://propelorm.org/cookbook/customizing-build.html

Comments

  1. Tip: you may use just one file for run time and build time, just add these lines to your build.properties:

    # just one configuration file
    propel.runtime.conf.file = runtime-conf.xml
    propel.buildtime.conf.file = runtime-conf.xml

    ReplyDelete

Post a Comment