ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

CodeIgniter - Doctrine ORM Tutorial : A way of enhancing CI

November 01, 2009 1:31pm

Subscribe [12]
  • #16 / Nov 16, 2009 9:37pm

    wiredesignz

    2882 posts

    Just to be clear, form (input) validation in the controller is not the same thing as data validation in your model.

    The controller checks and cleans the incoming data and ensures that each field meets a minimum requirement. It should not try to check if the data values are suitable for use by the application.

    ie: A login form with username and password should be form validated only to ensure the fields exist and are minimum length etc. Your business logic in the model should then make the comparisons and verify the actual values submitted and give a result back the controller so it knows how to respond to the user input.

  • #17 / Nov 16, 2009 10:03pm

    Burak Guzel

    52 posts

    Just to be clear, form (input) validation in the controller is not the same thing as data validation in your model.

    The controller checks and cleans the incoming data and ensures that each field meets a minimum requirement. It should not try to check if the data values are suitable for use by the application.

    ie: A login form with username and password should be form validated only to ensure the fields exist and are minimum length etc. Your business logic in the model should then make the comparisons and verify the actual values submitted and give a result back the controller so it knows how to respond to the user input.

    Right, I see what you mean.

    If you look at the Day4 article, you can see I created another class called Current_User, which encapsulates User.

    It’s responsible for authenticating the user, and also it contains an instance of the User object once the user is logged in. And it carries it over between page loads, by putting the user_id in the Session.

    It’s a Singleton pattern class, so it allows for the logged in User instance to be accessed like a global variable: Current_User::user();

    Does that sound like what you are talking about?

  • #18 / Nov 24, 2009 2:12pm

    M4rc0

    68 posts

    lane4, do you think using doctrine would have any performance impact on CI?

    Does anybody think this could slow it down a little?

    I can see the benefits of using ORM, specially for large projects, but I’m not sure if the cost for it is worth it.
    Can anybody share any opinions?

  • #19 / Nov 24, 2009 2:54pm

    Burak Guzel

    52 posts

    lane4, do you think using doctrine would have any performance impact on CI?

    Does anybody think this could slow it down a little?

    I can see the benefits of using ORM, specially for large projects, but I’m not sure if the cost for it is worth it.
    Can anybody share any opinions?

    It depends on how you use it. Sometimes it even improves performance:
    http://www.doctrine-project.org/blog/transactions-and-performance

    They have a manual section with optimization tips:
    http://www.doctrine-project.org/documentation/manual/1_1/en/improving-performance

  • #20 / Nov 26, 2009 9:11pm

    jwright

    28 posts

    I’ve been really liking your tutorials and the CI/Doctrine combo.

    Now I’m trying to get Doctrine into one of the CI module frameworks out there as a module ...

    either using
    http://codeigniter.com/wiki/Modular_Extensions_-_HMVC
    or

    http://codeigniter.com/wiki/Matchbox/

    I’ve only tried Matchbox so far but if I use the approach to installing Doctrine in CI in your Day 1 tutorial, I get this ...

    “An Error Was Encountered

    Matchbox: Unable to load the requested file: plugins/doctrine_pi.php”

    I guess it’s time to start searching the logs.

    I’m wondering if anyone else is interested in a Doctrine module for CI and if anyone has any best practice advice on how to accomplish it and use it from other modules.

    Maybe I should start another thread about this.

  • #21 / Nov 26, 2009 10:53pm

    jwright

    28 posts

    Actually I did get it working using the latest Modular Seperation in this thread
    http://ellislab.com/forums/viewthread/121820/P40/#667656
    Now to figure out the best way to use it from other modules.

  • #22 / Nov 28, 2009 4:21pm

    jwright

    28 posts

    @lane4 @wiredesignz
    After looking into this, it seems there is more to it than I could see initially.
    My goal is to combine the power of Modular_Extensions (or Modular Separation) and Doctrine. But I can’t see the most appropriate way to do so.

  • #23 / Nov 28, 2009 8:26pm

    Burak Guzel

    52 posts

    What part are you having problems with? Getting the plugin to load or getting the models to work?

    I have no experience with Matchbox or HMVC, so I’m not sure if I can help.

  • #24 / Nov 28, 2009 9:12pm

    jwright

    28 posts

    Just to clarify, I have got Doctrine working within a module using wiredesignz Modular Separation code and I don’t think I’d have a problem getting it working with his HMVC code either (since it’s from the same codebase according to wiredesignz).

    I’m trying to figure out the best application architecture using these two. I’d like to achieve a few features…
    - one Doctrine installation powering multiple modules
    - ability to use the migrations feature of Doctrine (in/with multiple modules)
    - each module can load other modules and there can be module dependencies (meaning modules depending on other modules)
    - the above while maintaining the modularity of the modules, meaning that we can still copy a module folder into the modules folder of any HMVC/Doctrine/CI install and it can work with little or no configuration of the module

    I know this is all a bit fuzzy and incomplete talking about it without concrete examples. Articulating it in this post has helped me see a little clearer how it might all fit together.

    Hopefully I can put something together soon and share it for review and improvement/alternative suggestions.

  • #25 / Nov 28, 2009 9:55pm

    wiredesignz

    2882 posts

    Try this as a library alternative:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * Doctrine ORM factory for Modular Extensions - PHP5 
     * 
     * Install this file as application/libraries/Doctrine_orm.php
     * 
     * @version:     0.2
     * $copyright     Copyright (c) Wiredesignz 2009-11-29
     * 
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    require_once APPPATH.'vendor/Doctrine/lib/Doctrine.php';
    
    /* this allows Doctrine to load classes as needed */
    spl_autoload_register(array('Doctrine', 'autoload'));
    
    class Doctrine_ORM {
        
        public $manager;
        
        function __construct() {
            
            include APPPATH.'/config/database.php';
            
            /* create the DSN connection for Doctrine */
            foreach ($db as $connection => $values) {
                $dsn = "{$db[$connection]['dbdriver']}://{$db[$connection]['username']}:{$db[$connection]['password']}@{$db[$connection]['hostname']}/{$db[$connection]['database']}";
                Doctrine_Manager::connection($dsn, $connection);
            }    
            
            $this->manager = Doctrine_Manager::getInstance();    
        }
        
        static function Factory($model) {
            
            list($path, $_model) = Modules::find($model, NULL, 'models/');
            
            if ($path == FALSE) {
                $path = APPPATH.'models/';
            }
            
            Modules::load_file($_model, $path);
            
            $class = ucfirst($_model);
            return new $class();
        }
    }

    Version 0.2 : Only started this a short time ago so there may be issues.

    application/config/autoload.php

    $autoload['libraries'] = array('doctrine_orm');

    Usage:

    $dao = Doctrine_ORM::Factory('module/model');

    Note: The Doctrine libraries should be located in a new application/vendor/Doctrine directory.

  • #26 / Nov 30, 2009 6:33pm

    jwright

    28 posts

    @wiredesignz this code is interesting. At first I thought the $dao would be like a “Doctrine::” class for just that model folders domain model. But after looking closer actually, I’m not sure how to use $dao…

    Also, I’ve started a new topic about this CodeIgniter + Doctrine + HMVC

  • #27 / Sep 12, 2013 10:15am

    davidbarratt

    1 posts

    To make the process easier of using Doctrine with CodeIgnitier, we’ve developed the Doctrine CodeIgniter LIbrary.

    Enjoy!

  • #28 / Sep 12, 2013 1:39pm

    albertleao

    30 posts

    I’ve been using PHPActiveRecord. How does Doctrine compare with that?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases