简体   繁体   English

CodeIgniter和DataMapper

[英]CodeIgniter and DataMapper

I've read through a few different posts here and I can't figure out what I'm doing wrong. 我在这里阅读了一些不同的帖子,无法弄清楚自己在做什么错。

My DB is setup like the following: 我的数据库的设置如下:

homes
- id
- address_id
- price
- etc...

address
- id
- home_id
- address1
- address2
- etc...

Then my models look like this, condensed. 然后,我的模型就这样浓缩了。

home.php
<?php
class Home extends DataMapper {
    public var $has_one = array('address');
}

address.php
<?php
class Address extends DataMapper {
    public var $has_one = array('home');
}

Then my controller uses the following: 然后我的控制器使用以下内容:

homes.php
class Homes extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('datamapper');
    }

    public function index() {
        $homes = new Homes();
        $homes->include_related('address');
        $homes->get_iterated();

        $this->output->enable_profiler(TRUE);

        _p($homes); // Self made function that wraps a print_r() in two <pre> tags.
}
}

If I comment out these two lines I get the standard CI return array. 如果我注释掉这两行,我将得到标准的CI返回数组。

$homes->include_related('address');
$homes->get_iterated();

If I don't then I get a server error. 如果不这样做,则会收到服务器错误。 This is my first time using DataMapper and I'm almost certain I'm doing everything wrong, but have no idea where to start. 这是我第一次使用DataMapper,几乎可以肯定我做错了什么,但不知道从哪里开始。

UPDATE: 更新:

I figured out my issue. 我知道了我的问题。 I had to change the DB table address to addresses and in my address.php model I had to specify var $table = 'addresses'; 我必须将数据库表address更改为addresses ,在我的address.php模型中,我必须指定var $table = 'addresses';

That fixed everything. 固定一切。

Yes you can specify the table name in your model. 是的,您可以在模型中指定表名称。 Also your example was wrong : 你的例子也是错误的:

$homes = new Homes();

Should be 应该

$homes = new Home();

I usually redefine the table name within my model to make sure everything is fine. 我通常会在模型中重新定义表名,以确保一切正常。

Your relations is wrong. 你的关系错了。 I assume you are setting a One to One relation ship. 我假设您正在设置一对一关系。 The doc say : http://datamapper.wanwizard.eu/pages/relationtypes.html 医生说: http : //datamapper.wanwizard.eu/pages/relationtypes.html

Because this is a One to One relationship, the relationship could have been stored in three ways: 因为这是一对一关系,所以该关系可以三种方式存储:

  1. As shown, on the workers table. 如图所示,在worker表上。
  2. On the workplaces table, as worker_id 在工作场所表上,为worker_id
  3. On a dedicated workers_workplaces join table, with the columns id, worker_id, and workplace_id 在专用的worker_workplaces联接表上,具有id,worker_id和workspace_id列

But here you have added *address_id* to home and *home_id* in address . 但是在这里,您已经将* address_id *添加到home,并将* home_id *添加到address You have to choose between on. 您必须在两者之间进行选择。 For example keep *home_id* in address and remove *address_id* in home . 例如,在地址中保留* home_id *,在home中移除* address_id *。

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

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