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!';

Howdy, Stranger!

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

In this Discussion