Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Question how to handle relations
  • Hi there,

    I am writing a Module which captures Countries, States, Regions and Cities.

    Each State has one Country
    Each Region has one State
    Each City has one Region

    What is the best way to work with BelongsTo, HasOne etc.

    Actually I have Countries with no relation and States with:

    namespace Destinations;

    class Model_State extends \Orm\Model
    {

    protected static $_table_name = 'dest_states';

    protected static $_has_one = array(
    'country' =--> array(
    'key_from' => 'id',
    'model_to' => 'Destinations\Model_Country',
    'key_to' => 'country_id',
    'cascade_save' => true,
    'cascade_delete' => false,
    )
    );



    Is that correct?

    I ask, as I receive an error while deleting a state and the systems seem to try to change data for Countries as well:

    Fuel\Core\Database_Exception [ 1054 ]: Unknown column 't0.country_id' in
    'where clause' [ SELECT `t0`.`id` AS `t0_c0`, `t0`.`name` AS `t0_c1`,
    `t0`.`countrycode` AS `t0_c2`, `t0`.`created_at` AS `t0_c3`,
    `t0`.`created_by` AS `t0_c4`, `t0`.`updated_at` AS `t0_c5`,
    `t0`.`updated_by` AS `t0_c6` FROM `dest_countries` AS `t0` WHERE
    `t0`.`country_id` = '3' LIMIT 1 ]

    Hope someone can hit my head and give me the right hint.

    Thanks
    Kay
  • HarroHarro
    Accepted Answer
    Easiest to remember: belongs_to is in the model that has the foreign key.

    So if State has one Country, then:

    Model_County -> has_many -> Model_State
    Model_State -> belongs_to -> Model_Country

    because Model_State contains country_id.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion