Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
belongs_to cannot find the from key without configuration
  • FuelPHP Version: 1.8.1
    Models: 
    class Model_CalendarItem extends Orm\Model {}
    class Model_CalendarSchedule extends Orm\Model {
        protected static $_belongs_to = array('calendaritem' => array(
    //        'key_from' => 'calendar_item_id',
    //        'model_to' => 'Model_CalendarItem',
    //        'key_to' => 'id',
    //        'cascade_save' => true,
    //        'cascade_delete' => false,
        ));
    }
    class Model_Carervisitlog extends Orm\Model
    {
        protected static $_belongs_to = array('calendarschedule' => array(
    //        'key_from' => 'calendar_schedule_id',
    //        'model_to' => 'Model_CalendarSchedule',
    //        'key_to' => 'id',
    //        'cascade_save' => true,
    //        'cascade_delete' => false,
        ));
    }

    Controller: 
    class Controller_Visits extends Controller_RestBase
    {
        public function get_index()
        {
            $this->options['related'] = array('calendarschedule' => array('related' => array('calendaritem')));

            $this->result = Model_Carervisitlog::find($this->id, $this->options);
        }
    }

    Request: 
    GET /visits
    GET /visits/1

    Without the belongs_to configuration (the disabled lines), I get an error:

    1054!



    Fuel\Core\Database_Exception [ 1054 ]:
    SQLSTATE[42S22]:
    Column not found: 1054 Unknown column 't0.calendarschedule_id' in 'on
    clause' with query: "SELECT `t0`.`id` AS `t0_c0` FROM `carervisitlogs` AS `t0` LEFT JOIN `calendar_schedules` AS
    `t1` ON (`t0`.`calendarschedule_id` = `t1`.`id`) LEFT JOIN
    `calendar_items` AS `t2` ON (`t1`.`calendaritem_id` = `t2`.`id`)"



    C:/test/fuel/core/classes/database/pdo/connection.php @ line 253



    248                            {
    249                                $error_code 0;
    250                            }
    251                        }
    252
    253                        throw new \Database_Exception($e->getMessage().' with query: "'.$sql.'"'$error_code$e);
    254                    }
    255                }
    256
    257                // no more attempts left, bail out
    258                else

    Backtrace



    1. C:/test/fuel/core/classes/database/query.php @ line 314


    2. C:/test/fuel/packages/orm/classes/query.php @ line 1564


    3. C:/test/fuel/packages/orm/classes/model.php @ line 623


    4. C:/test/fuel/app/classes/controller/visits.php @ line 20


    5. C:/test/fuel/core/base56.php @ line 37


    6. C:/test/fuel/core/classes/controller/rest.php @ line 153


    7. C:/test/fuel/core/classes/request.php @ line 454


    8. C:/test/public/index.php @ line 71


    9. C:/test/public/index.php @ line 92

    Should the foreign key reference be fixed to calendar_schedule_id?

    I would like to set the belongs_to relations without the configurations but this error would force me to set the configuration for each relation similar to CalendarItem (calendar_items) and CalendarSchedule (calendar_schedules).
  • HarroHarro
    Accepted Answer
    Relations always have to be defined both ways. I will not work without, as you have noticed.
  • Relations are defined except that the options aren't in the array (basic):

    //        'key_from' => 'calendar_schedule_id',
    //        'model_to' => 'Model_CalendarSchedule',
    //        'key_to' => 'id',
    //        'cascade_save' => true,
    //        'cascade_delete' => false,

    I just thought it's optional as in the example here: https://fuelphp.com/docs/packages/orm/relations/belongs_to.html and here: https://fuelphp.com/docs/packages/orm/relations/intro.html - Basic and Fully configured.

    Thanks!
  • HarroHarro
    Accepted Answer
    It is not optional, there are two ways of defining the relation:

    1. as a relation name:

    protected static $_belongs_to = array('calendaritem');

    This way the ORM will use the relation name to determine the rest of the values

    2. as a relation definition:

    protected static $_belongs_to = array('calendaritem' => array(
       ... // your definition goes here
    ));

    What you've done, is go for option 2, but then not give a definition, you pass an empty array instead.
  • I used and tried #1 as a relation name before but I got the error. I also tested it with empty array and it worked with table name not using underscore. It's just failing if the table name has underscore.

    For example:

    Table Name: calendar_schedules
    Model: Model_CalendarSchedule
    belongs_to: array('calendarschedule')

    The above fails, but if:

    Table Name: calendarschedules
    Model: Model_Calendarschedule
    belongs_to: array('calendarschedule')

    it works.

  • HarroHarro
    Accepted Answer
    If one doesn't work, your table structure doesn't conform to the standard, so the ORM can't determine all relation parameters from the relation name.

    Which is why you have the option to completely configure it.
  • You are right that I need to completely configure it and I will but I believe the table structures follows the standard.

    Thanks.


  • It doesn't use the tablename, that is defined in the destination model. It only uses the relation name, so if your relation is "calendarschedule", this will be generated:

    'key_from' => 'calendarschedule_id',
    'model_to' => 'Model_Calendarschedule',
    'key_to' => 'id',
    'cascade_save' => true,
    'cascade_delete' => false,

    As you can see, that is different from what you have specified.
  • What should be the models and relations to calendar schedule if the table name is calendar_schedules?
     
    I have carervisitlogs.calendar_schedule_id, calendar_schedules.calendaritem_id and calendaritems


  • HarroHarro
    Accepted Answer
    Inflector::tableize() is used to convert the Model name to the table name.

    /data/www/fuelphp/1.9/develop  (1.9/develop)
    [wanwizard@catwoman] $ oil console
    Fuel 1.9-dev - PHP 7.1.16 (cli) (Mar 28 2018 07:11:55) [Linux]
    >>> Inflector::tableize('Model_Calendar_Schedule')
    calendar_schedules

  • I created calendar_schedule.php containing Model_Calendar_Schedule, linked Model_Carervisitlog using $_belongs_to = array('calendar_schedule') but It can't find it. I switched to FuelPHP 1.9 but still not working.

    I will just use full configuration to make it work.

    Thanks.
  • HarroHarro
    Accepted Answer
    It can not find what? What is the error? I can't really do anything with "it doesn't work".

    I recreated your setup:

    fuel/app/classes/model/carervisitlog.php:

    <?php
    class Model_Carervisitlog extends \Orm\Model
    {
        protected static $_properties = array(
            'id',
        );

        protected static $_belongs_to = array(
            'calendar_schedule'
        );
    }

    fuel/app/classes/model/calendaritem.php:

    <?php
    class Model_Calendaritem extends \Orm\Model
    {
        protected static $_properties = array(
            'id',
        );
    }

    fuel/app/classes/model/calendar/schedule.php:

    <?php
    class Model_Calendar_Schedule extends \Orm\Model
    {
        protected static $_properties = array(
            'id',
        );

        protected static $_belongs_to = array(
            'calendaritem'
        );
    }

    And this works fine, 

        $options['related'] = array('calendar_schedule' => array('related' => array('calendaritem')));
        $result = Model_Carervisitlog::find(1, $options);

    generates:

    SELECT `t0`.`id` AS `t0_c0`, `t1`.`id` AS `t1_c0`, `t2`.`id` AS `t2_c0` FROM (SELECT `t0`.`id` FROM `carervisitlogs` AS `t0` WHERE `t0`.`id` = 1 LIMIT 1) AS `t0` LEFT JOIN `calendar_schedules` AS `t1` ON (`t0`.`calendar_schedule_id` = `t1`.`id`) LEFT JOIN `calendaritems` AS `t2` ON (`t1`.`calendaritem_id` = `t2`.`id`)
  • Thanks. It's working now!

Howdy, Stranger!

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

In this Discussion