Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
"Has many" relation conditions
  • Hi,

    I want to add conditions to a "has many" relation. I've done this but I've got an error :

    protected static $_has_many = array(
        'maintenance_tasks' => array(
            'key_from' => 'id',
            'model_to' => 'Model_Maintenance_Task',
            'key_to' => 'user_id',
            'cascade_save' => false,
            'cascade_delete' => true,
            'conditions' => array(
                'where' => array(
                    array(DB::expr('YEAR(date)'), '=', DB::expr('YEAR(CURDATE())')),
                    array(DB::expr('MONTH(date)'), '=', DB::expr('MONTH(CURDATE())'))
                ),
                'order_by' => array(
                    'date' => 'desc'
                )
            )
        )
    );

    My problem is in my "where". Here the error :

    syntax error, unexpected '(', expecting ')'

    Thank you in advance for your help :)
  • HarroHarro
    Accepted Answer
    This is a class property definition, and you can not add function calls (or in this case class method calls) to it.

    You can work around this in the _init static method:

    public static function _init()
    {
        static::$_has_many['maintenance_tasks']['conditions'] = array(
                'where' => array(
                    array(DB::expr('YEAR(date)'), '=', DB::expr('YEAR(CURDATE())')),
                    array(DB::expr('MONTH(date)'), '=', DB::expr('MONTH(CURDATE())'))
                )
        );

    }
  • Ok thank you for your help !
  • Hi,

    Now I've the same problem in a Controller, not in a Model. Here is my error :

    "Argument 1 passed to Orm\Query::_parse_where_array() must be of the type array, object given"

    Here my code :

    $start_date = Model_Maintenance_Task::find('first', array(
        'where' => array(
            array(DB::expr('YEAR(date)'), '=', Session::get('maintenance_date_Y')),
            array(DB::expr('MONTH(date)'), '=', Session::get('maintenance_date_m')),
        )
    ));


    Thank you in advance for your help !
  • HarroHarro
    Accepted Answer
    I never use array syntax, so I have to dive into it. From the looks of it that should be ok. Are you sure you're on the latest version? DB::expr() for where() has been present for a while.

    You might want to reconsider using it as well, support for it will be dropped in Fuel v2.

    Alternatively, use:

    $start_data = Model_Maintanance::query()
        ->where(DB::expr('YEAR(date)'), '=', Session::get('maintenance_date_Y'))
        ->where(DB::expr('MONTH(date)'), '=', Session::get('maintenance_date_m'))
        ->get_one();
  • Yes, I'm on Fuel version 1.7.2.

    Ok, I will use this syntaxe. Thank you.

Howdy, Stranger!

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

In this Discussion