1

I have a many-to-many relationship created using fuelphp's ORM.

The pseudocode for the relation looks like this

class MyModel extends Model
{
protected static $_many_many = [
    'relatedmodel' => [
        'conditions' => [
            'where' => [
                    ['ctime', '>', DB::expr(('now() - interval 1 week'))],
                ],
            ],
        ]
    ];
}

The idea here is that I only want the relationship to look at newer relatedmodels that were created in the last week.

However, this obviously won't work because of a php language constraint - an expression is not allowed as a field default value.

How can I get the desired behavior in FuelPHP despite that constraint?

johncorser
  • 9,262
  • 17
  • 57
  • 102
  • 1
    AFAIK where conditions in the relation is actually used in the JOIN as an ON condition. Have you tried putting it into a "real" where condition in a query? Even if you want to make it a permanent condition for all cases: you can override the `query` method in the Model so that every query is executed with that condition. – mark.sagikazar Nov 10 '15 at 08:20

1 Answers1

1

The work around for the language constraint here is to use Fuel autoloader's public static _init() function to set the value. This gets called automatically when the class is loaded by the autoloader.

http://fuelphp.com/docs/general/classes.html#/init_method

Emlyn
  • 852
  • 6
  • 16