Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM - Temporal Model - Fetch Related Objects
  • Hi,

    With Orm\Model I could fetch all related object and its properties by the defined relation name.

    $object  = Model_Object::query()->get_one();

    $object->relation_name => related object or iterator of related objects
    $object->relation_name->property => direct access to property of related object

    With Orm\Model_Temporal I can't fetch the related objects anymore and I have to use a seperate query to retrieve the related objects.

    Any suggestions? I'm using FuelPHP 1.7.1 now.

    Thanks
  • I can't immediately see why that would not work.

    Any error messages? What does var_dump($object->relation_name) say?
  • NULL for a to-one relationship and array(0) { } for a to-many relationship.

    Fetching a relationship doesn't work, but saving a relationship works without problems.

    $object->relation_object = $other_object;
    $object->save(); // relation id successfully changed in database
     

  • Very odd. I'll check with Steve.,

    I can't imagine this is a code issue, lots of existing apps would trip over a bug like this.

    No issue with a property and a relation with the same name?
  • Hi,

    There are no duplicates between properties and relation names.

    I will setup an empty project for testing this issue, maybe there is a third party framework disturbing the FuelPHP ORM system. It is indeed a strange uncommon problem.

    Thanks
  • I created an empty FuelPHP 1.7.1 project and activated the ORM package. This is wat happened:

    I created 2 models, a model "Session" and a model "Presentation":


    <?php

    class Model_Session extends Orm\Model_Temporal
    {
        protected static $_primary_key = array('id','temporal_start','temporal_end');


        protected static $_temporal = array(
            'start_column' => 'temporal_start',
            'end_column' => 'temporal_end',
            'mysql_timestamp' => true,
        );

        protected static $_table_name = 'sessions';

        public static $_properties = array(
            'id',
            'temporal_start',
            'temporal_end',
            'title',
        );

        protected static $_has_many = array('presentations');
    }


    <?php

    class Model_Presentation extends Orm\Model_Temporal
    {
        protected static $_primary_key = array('id','temporal_start','temporal_end');


        protected static $_temporal = array(
            'start_column' => 'temporal_start',
            'end_column' => 'temporal_end',
            'mysql_timestamp' => true,
        );

        protected static $_table_name = 'presentations';

        public static $_properties = array(
            'id',
            'temporal_start',
            'temporal_end',
            'title',
            'session_id',
        );

        public static $_belongs_to = array('session');
    }


    Every session can have multiple presentations (one-to-many).
    Every presentation can have only one session (many-to-one).


    I executed the following code in a controller:

      $session = new Model_Session();
            $session->title = "Session";
            $session->save();
            $presentation = new Model_Presentation();
            $presentation->title = "Presentation";
            $presentation->session = $session;
            $presentation->save();

            $fetched_presentation = Model_Presentation::query()->get_one();
            var_dump($fetched_presentation->session);
  • Each action call:

    1. Creates a new Session object
    2. Creates a new Presentation object with the Session as relation.
    3. Fetches a Presentation out of the database and dumps the session relation.
    First call, the database is empty:

    Successful generation of the 2 objects and the var_dump is not empty.

    All calls when the database is not empty anymore:

    Successful generation of the 2 objects, but the var_dump stays NULL all the time
  • HarroHarro
    Accepted Answer
    I checked with Steve, this issue is solved in the current development branch, so it will be part of 1.7.2, which is due later today.
  • Thanks!
  • Any progress with 1.7.2?
    Or can fix the issue by just downloading the ORM package from github 1.8/develop and replacing it in my project?

    Thanks
  • Had been delayed a few days, an issue with Sessions has popped up which needs to be fixed first.

    It's is not a problem to switch to 1.8/develop,
  • Replacing core and orm with 1.8/develop from GitHub didn't solve the relations problem.
  • I have to pass this on to Steve then.

    He doesn't do forums, so either create an issue for it at https://github.com/fuel/orm/issues (if none exists for this issue), or pop into our IRC channel, he's online during most of the day (GMT).
  • OK, will do that.

    Thanks

Howdy, Stranger!

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

In this Discussion