Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
[Bug report][fuel/dev-1.9]I could not get related data using orm
  • Hi there,

    I found a bug, so, could you check it out?

    Also, I uploaded following files that are called "query_old.php" and "query_new.php" becuse this forum dose not allow me to upload file.

    Then, I used to have query_old.php in my production environment as fuel/packages/orm/classes/query.php.

    When I updated fuelphp yesterday, I suddenly could not get related model data.

    So, I was looking at fuelphp changes and found query.php has been updated 5 month ago as well as some other changes for orm.

    And so, I swap source code from query_new.php to my query_old.php, it start fetching my data.

    I will post example of source code and create statements.
  • Here is example of my models as following.

    class Model_A extends Model \Orm\Model
    {
      protected static $_properties = array(
              'id',
              'name',
              'created_at',
              'updated_at',
      );
      protected static $_has_many = array(
                      'table_b' => array(
                              'key_from'      => 'id',
                              'model_to'      => 'Model_Table_B',
                              'key_to'        => 'a_id',
                              'cascade_save' => true,
                              'cascade_delete' => false,
              )
      );
      protected static $_table_name = 'table_a';
      protected static $_primary_key = array('id');
       protected static $_observers = array(
              'Orm\Observer_CreatedAt' => array(
                      'events' => array('before_insert'),
                      'property' => 'created_at',
                      'mysql_timestamp' => true,
              ),
              'Orm\Observer_UpdatedAt' => array(
                      'events' => array('before_insert','before_update'),
                      'property' => 'updated_at',
                      'mysql_timestamp' => true,
              ),
      );
    }

    class Model_Table_B extends \Orm\Model
    {
      protected static $_properties = array(
              'a_id',
              'c_id',
              'created_at',
              'updated_at',
      );
       protected static $_observers = array(
              'Orm\Observer_CreatedAt' => array(
                      'events' => array('before_insert'),
                      'property' => 'created_at',
                      'mysql_timestamp' => true,
              ),
              'Orm\Observer_UpdatedAt' => array(
                      'events' => array('before_insert','before_update'),
                      'property' => 'updated_at',
                      'mysql_timestamp' => true,
              ),
      );
      protected static $_table_name = 'table_b';
      protected static $_primary_key = array('a_id','c_id');
      protected static $_has_many = array();
      protected static $_many_many = array();
      protected static $_has_one = array();
    }

    Here is create table.

    CREATE TABLE `table_a` (
      `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `name` mediumtext NOT NULL,
      `created_at` datetime NOT NULL,
      `updated_at` datetime NOT NULL,
      PRIMARY KEY (`id`),
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    CREATE TABLE `table_b` (
      `a_id` int(11) unsigned NOT NULL,
      `c_id` int(11) unsigned NOT NULL,
      `created_at` datetime NOT NULL,
      `updated_at` datetime NOT NULL,
      PRIMARY KEY (`a_id`,`c_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8


    Here is example of source code that I could not get data.

    $model = Model_A ::find_by_id(1);
    print_r($model->table_b); // This gives me empty array() although there are some data.

    It looks like $_has_many relation is not working.

    Also, when I  look at code profiler, orm executes sql statement, but, it dose not fetch the data from database.

    Best regards,
  • use Orm\Model;use \Fuel\Core\Debug;
    class Model_One extends Model
    {
        public static string $_table_name   = 'table_one';
        protected static array $_properties = ['id', 'name',];
        protected static array $_has_many   = [
            'many' => [
                        'key_from' => 'id',
                        'model_to' => 'Model_Many',
                        'key_to' => 'one_id',
            ],
        ];
    }
    class Model_Many extends Model
    {
        public static string $_table_name   = 'table_tow';
        protected static array $_properties = ['id', 'one_id', 'name',];
        protected static array $_belongs_to = [
            'one' => [
                        'key_from' => 'one_id',
                        'model_to' => 'Model_One',
                        'key_to' => 'id',
            ],
        ];
    }
    $one = model_One::find(1);
    if($one)
    {
        // Debug::dump($data);
        if($one->many)
        {
            $data = [];
            foreach($one->many as $many)
            {
                  $data[] = $many->name;
            }
            // Debug::dump($data);
            return $data;
        }
        else
        {
            return 'many relation not found!';
        }
    }
    return 'record not found!';
  • @atabak

    Thank you.

    I looked at packages/orm/classes/query.php and it seems $this->relations dose not have related model that defined in the Model.

    The line number is 1089.

    When I have a time, I will take a look at that again.
  • I can't imagine that being the case, it is such an important function of the ORM, if that wouldn't work, most of our apps won't run.

    I'm currently our of office, I can only have a look at it when I'm back next week.
  • @Harro Verton

    Thank you for your message.

    Yes, that is what I was thought as well.
    So, I reinstall all packages installed by composer yesterday, but, still dose not work.

    Then, it looks like only model with multiple primary keys does not work.
    Except for that model, it looks like working at a moment.

    When you are back, please take a look.

    Best regards,
  • I tested with 1.9/dev just now. Need further testing but there is definitively something odd with relations.
  • Very interested to see what you come up with.

    All our apps run on 1.9/dev, and I haven't heard of any issues.

    Having said that, we never design apps with multiple primary keys, all our tables have meaningless auto-increment ID keys.
  • So, after more tests, I did find an issue in the ORM relation on 1.9/dev. Not the one OP first mentioned, through.

    The empty relations, that would normally exists in _data_relations and _original_relations (with null value for belongs_to and has_one, and empty array for has_many and many_many) aren't defined.

    Not breaking anything, but any call to those relations will cause an additional SQL query to try to lazyload the relation per relation and model instance.
  • You mean:

    you run a query with a related model, but there are no related records. In this case the empty relation is not created, so access assumes there was no relared model, and it will fire a lazy load query (which obviously doesn't return anything?

    Ok, I'll have a look.
  • Exactly. I looked up the code, and this behavior seems to be caused by : query.php -> process_row() -> l.1527 (And the same behavior for multiple pks just below)

    // construct the PK string representation for this record
    if (count($model['pk']) == 1)
    {
    $pk = $record[$model['pk'][0]];
    // skip empty results
    if (is_null($pk))
    {
    continue;
    }
    }

    A related model without record has its PK set to null, and hence not being handled further. Wanna me to PR a fix ?
  • If you can, please.

    My home server has crashed with a mainboard failure earlier today, so I can't do much until the replacement has arrived and it is up and running again...
  • Done and tested. Let me know if it works for you.
  • Thanks.

    Without the option to test it myself at the moment, I've left some comments on the PR.
  • Thank you very much for investigation.

    I really appreciate it.

    If you want me to test it on my environment, I can help you :)

  • HarroHarro
    Accepted Answer
    Fixes have been committed to 1.9/dev, so if you want to test, please.
  • I checked it out this morning and working fine now :)
    Thank you very much Harro and AdamW. 

Howdy, Stranger!

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

In this Discussion