Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6699159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:39:45+00:00 2026-05-26T06:39:45+00:00

I’m trying to implement an ORM in a CodeIgniter application, but cannot get it

  • 0

I’m trying to implement an ORM in a CodeIgniter application, but cannot get it to work. To start I’m just trying to instantiate a simple test model:

<?php

class Cart extends DataMapper
{

    public function __construct()
    {
        // model constructor
        parent::__construct();
    }

    var $validation = array(
        'username' => array(
            'label' => 'UserName',
            'rules' => array('required', 'trim', 'unique', 'alpha_dash', 'min_length' => 1, 'max_length' => 50),
        )
    );
}

?>

And then in the Controller I try this:

public function __construct()
{
    parent::__construct();
    $this->load->model('cart');
}

public function index()
{   

    $cart = new Cart();
}

But I don’t even get past the constructor. The debugger stops and gives me a message saying “Waiting for an incoming connection with ide key xxxxx” (random number)

BTW the cart model class file name is in lower case, but the class name in upper case. I tried both in the constructor.

I have followed the instructions for installation carefully, copying the two datamapper files to libraries and config folders, as well as autoloading the datamapper library.

But it just doesn’t work. Am I missing something? The table I’m trying to map is only a test table that actually only has an id and a username field. I don’t actually understand the validation array, but just followed the examples in the docs and modified to my field. The id field doesn’t seem like anyone has put in the validation array.

I should also mention that I’m a newbie at CodeIgniter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:39:45+00:00Added an answer on May 26, 2026 at 6:39 am

    Your code seems mostly correct for use with DataMapper ORM and CodeIgniter.

    To explain things a bit, DataMapper is just an abstraction layer. It handles a lot of the necessities when working with databases and mapping your objects to your tables. That being said, you don’t have to load your models, etc. As long as you are autoloading your database library and datamapper library, you can use DataMapper.

    The validation array lets DataMapper know the requirements to your properties. So, if you try to save an object and one of the properties that you’ve created/changed doesn’t meet those requirements, then your save will fail and you’ll get an error message:

    // For example
    if ($myObj->save())
    {
        // $myObj validation passed and is saved to db
    }
    else
    {
        // $myObj validation failed, save did not complete
        echo $myObj->error->string;
    }
    

    Codeigniter already has a library named Cart, so you wouldn’t want to name your model Cart. So you could rename that model to Basket or something else that makes sense.

    I know you’re still just trying to get things to work, but I feel you need to think about your data structure a bit. You wouldn’t save the username in the Cart object, that’s why we use relations. So, I would structure it a bit like this:

    // baskets table (a table represents many baskets, therefore it is plural)
    id
    user_id
    blah
    blah
    created
    updated
    
    // users table
    id
    username
    email_address
    created
    updated
    
    // basket model (a model represents 1 basket, therefore it is singular)
    class Basket extends DataMapper
    {
        public function __construct()
        {
            parent::__construct();
        }
        var $has_one = array('user'); // each basket belongs to one user
        var $validation = array(...);
    }
    
    // user model
    class User extends DataMapper
    {
        public function __construct()
        {
            parent::__construct();
        }
        var $has_many = array('basket'); // each user can have many baskets
        var $validation = array(...);
    }
    
    // controller
    public function __construct()
    {
         parent::__construct();
    }
    
    public function index()
    {   
        $basket = new Basket();
        $basket->blah = 'whatever';
        $basket->save();
        // at this point, $basket is saved to the database
        // now let's add it to the user
        $user = new User();
        $user->where('id', 1)->get(1);
        // now we have a user
        // save the relationship to the basket
        $user->save($basket);
        // now $basket->user_id == 1
    
        // get the username from the basket
        $u = $basket->user->get();
        $username = $u->username;
    
        // yes, there are faster and shorter ways to write most of this,
        // but I think for beginners, this syntax is easier to understand
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.