Question

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. When I change it to cities_lounges it gives me different error like describe cities_lounges

Here is what lounge_cities table contains: "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

Can someone help me out, but don't point me to the manual, cause I hard it find to understand... ? Cheers

Was it helpful?

Solution

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):

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. (source)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top