开发者

Cannot get DataMapper to work in CodeIgniter

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.


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
}


The CodeIgniter documentation about models states that you can load a model by calling

$this->load->model('Model_name');

in the constructor, and that you can access this model in your controller by doing

$this->Model_name->function();

So you should change your Controller code into

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

public function index()
{   

    $this->Cart->functionCall();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜