繁体   English   中英

无法让DataMapper在CodeIgniter中工作

[英]Cannot get DataMapper to work in CodeIgniter

我正在尝试在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),
        )
    );
}

?>

然后在Controller中我试试这个:

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

public function index()
{   

    $cart = new Cart();
}

但我甚至没有超越构造函数。 调试器停止并给我一条消息“等待与ide密钥xxxxx的传入连接”(随机数)

BTW购物车模型类文件名是小写的,但是大写的类名。 我在构造函数中尝试了两种方法。

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

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

我还要提一下,我是CodeIgniter的新手。

您的代码似乎最适合与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或其他有意义的东西。

我知道你仍然只是想让事情发挥作用,但我觉得你需要考虑一下你的数据结构。 你不会在Cart对象中保存username ,这就是我们使用关系的原因。 所以,我会像这样构造它:

// 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
}

有关模型的CodeIgniter 文档说明您可以通过调用加载模型

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

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

$this->Model_name->function();

所以你应该将你的Controller代码更改为

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

public function index()
{   

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM