简体   繁体   English

CodeIgniter和Datamapper:一对多关系

[英]CodeIgniter & Datamapper: One to many Relationship

I am trying to make one to many relationship between lounges and cities So, one lounge can be in many cities - "KFC" => London, Paris, Madrid and etc For the purpose I am using DataMapper, but the manual si a bit confusing and I receive an error - Table 'databasename.cities_lounges' doesn't exist , however there is a table called lounge_cities . 我试图在休息室城市之间建立一对多关系,因此,一个休息室可以在许多城市使用-“肯德基” =>伦敦,巴黎,马德里等。并且我收到一个错误- 表'databasename.cities_lounges'不存在 ,但是有一个名为lounge_cities的表。 When I change it to cities_lounges it gives me different error like describe cities_lounges 当我将其更改为citys_lounges时,它给了我不同的错误,例如describeities_lounges

Here is what lounge_cities table contains: "id, lounge_id(INT, UNSIGNED), city_id(INT,UNSIGNED) 这是lounge_cities表包含的内容:“ id,lounge_id(INT,UNSIGNED),city_id(INT,UNSIGNED)

Here is my models: 这是我的模型:

<?php

 class Lounge_model extends DataMapper
 {
public function __construct()
{
    parent::__construct();
}

var $table = 'lounges';

var $has_one = array('city_model');


  }

<?php

  class City_model extends DataMapper
  { 
public function __construct()
{
    parent::__construct();
}

var $table = 'cities';

var $has_many = array('lounge_model');

   }

and here is the function where I am trying to get it my controller: 这是我要获取它的控制器的功能:

public function id($id)
{
    $lounge = new lounge_model();
    $lounge->where('id', $id)->get();

    $relationshiptest = $lounge->city_model->get();
    var_dump($relationshiptest);
      }

That is what I followed: http://datamapper.wanwizard.eu/pages/accessingrelations.html 这就是我遵循的: http : //datamapper.wanwizard.eu/pages/accessingrelations.html

Can someone help me out, but don't point me to the manual, cause I hard it find to understand... ? 有人可以帮我,但不要指出我的手册,因为我很难理解...? Cheers 干杯

You have a one-to-many relation, so you just need to add a column "city_model_id" to your "lounges" database table... There is no need for the "lounge_cities" table (which should have been "cities_lounges" according to the "Table Naming Rules): 您具有一对多关系,因此只需要在“ lounges”数据库表中添加一列“ city_model_id” ...就不需要“ lounge_cities”表了(根据本来应该是“ cities_lounges”到“表命名规则”:

Joining tables must be named with both of the table names it is joining, in alphabetical order, separated by an underscore (_). 联接表必须使用要联接的两个表名按字母顺序命名,并用下划线(_)分隔。 For example, the joining table for users and countries is countries_users. 例如,用户和国家/地区的联接表是country_users。 ( source ) 来源

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

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