0

我正在尝试在 CodeIgniter 应用程序中实现 ORM,但无法使其正常工作。首先,我只是想实例化一个简单的测试模型:

<?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),
        )
    );
}

?>

然后在控制器中我试试这个:

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

public function index()
{   

    $cart = new Cart();
}

但我什至没有通过构造函数。调试器停止并给我一条消息“等待带有 ide 键 xxxxx 的传入连接”(随机数)

顺便说一句,购物车模型类文件名是小写的,但类名是大写的。我在构造函数中都试过了。

我已仔细按照说明进行安装,将两个 datamapper 文件复制到库和配置文件夹,以及自动加载 datamapper 库。

但它只是行不通。我错过了什么吗?我要映射的表只是一个实际上只有一个 id 和一个用户名字段的测试表。我实际上并不了解验证数组,只是按照文档中的示例并修改到我的领域。id 字段似乎没有人放入验证数组。

我还应该提到我是 CodeIgniter 的新手。

4

2 回答 2

2

您的代码似乎最适合与 DataMapper ORM 和 CodeIgniter 一起使用。

稍微解释一下,DataMapper 只是一个抽象层。在使用数据库并将对象映射到表时,它可以处理许多必需品。话虽如此,您不必加载模型等。只要您自动加载数据库库和 datamapper 库,就可以使用 DataMapper。

验证数组让 DataMapper 知道对您的属性的要求。因此,如果您尝试保存对象并且您创建/更改的属性之一不满足这些要求,那么您的保存将失败并且您将收到一条错误消息:

// 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 已经有一个名为 的库Cart,因此您不想命名您的模型Cart。因此,您可以将该模型重命名为Basket或其他有意义的名称。

我知道您仍然只是想让事情正常进行,但我觉得您需要稍微考虑一下您的数据结构。您不会将 保存usernameCart对象中,这就是我们使用关系的原因。所以,我会像这样构造它:

// 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
}
于 2011-10-18T14:41:49.770 回答
0

关于模型的 CodeIgniter文档指出,您可以通过调用

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

在构造函数中,并且您可以通过执行在控制器中访问此模型

$this->Model_name->function();

所以你应该将你的控制器代码更改为

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

public function index()
{   

    $this->Cart->functionCall();
}
于 2011-10-17T17:26:06.013 回答