Welcome Guest, Not a member yet? Register   Sign In
problem registering related models
#1

[eluser]Unknown[/eluser]
Hi everyone,

I've installed CI + DataMapper
When I save a simple Model, it's OK but when I try to save a Model related to another one, I got the error :
Quote:A Database Error Occurred
Error Number:
SELECT * FROM "VSR_COUNTRY_VEHICLE" WHERE "VEHICLE_ID" = '22' AND "COUNTRY_ID" = '24'
Filename: C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\VSR\system\database\DB_driver.php
Line Number: 330

CI is looking for a joining table VSR_COUNTRY_VEHICLE although it's a One-To-Many relationship

Here is my code :
Code:
class Vehicle_model extends DataMapper {

    public $table = 'VEHICLE';

    public $has_one = array(
        'COUNTRY' => array(
            'class' => 'country_model',
            'other_field' => 'VEHICLE',
            'join_other_as' => 'COUNTRY',
            'join_table' => '',
            'reciprocal' => FALSE,
            'auto_populate' => NULL,
        ),
    );
}
Code:
class Country_model extends DataMapper {

    public $table = 'COUNTRY';

    public $has_many = array(
        'VEHICLE' => array(
            'class' => 'vehicle_model',
            'other_field' => 'COUNTRY',
            'join_other_as' => 'VEHICLE',
            'join_table' => '',
            'reciprocal' => FALSE,
            'auto_populate' => NULL,
        ),
    );
}

and in the controller :
Code:
$a = new Vehicle_model();
$a->NAME = 'Ford';
$b = new Country_model();
$b->where('NAME', 'France')->get();
$a->save($b, 'COUNTRY');

Thanks
Vincent
#2

[eluser]Unknown[/eluser]
SOLVED

Models attributes doesn't match the field name in the database

The field name in the database is : MANUFACTURING_COUNTRY_ID

So, I have to modify like this :
Code:
class Vehicle_model extends DataMapper {

    public $table = 'VEHICLE';

    public $has_one = array(
        'MANUFACTURING_COUNTRY' => array(
            'class' => 'country_model',
            'other_field' => 'VEHICLE',
            'join_other_as' => 'MANUFACTURING_COUNTRY',
            'join_table' => '',
            'reciprocal' => FALSE,
            'auto_populate' => NULL,
        ),
    );
}

Code:
class Country_model extends DataMapper {

    public $table = 'COUNTRY';

    public $has_many = array(
        'VEHICLE' => array(
            'class' => 'vehicle_model',
            'other_field' => 'MANUFACTURING_COUNTRY',
            'join_other_as' => 'VEHICLE',
            'join_table' => '',
            'reciprocal' => FALSE,
            'auto_populate' => NULL,
        ),
    );
}

and in the controller :
Code:
$a = new Vehicle_model();
$a->NAME = 'Ford';
$b = new Country_model();
$b->where('NAME', 'France')->get();
$a->save($b, 'MANUFACTURING_COUNTRY');

and it's all good !
Vincent




Theme © iAndrew 2016 - Forum software by © MyBB