Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to set condition in creating relation
  • Hi there,
    I've two modals "Model_User" & "Model_Packagecart",
    User has many packages
    but I need only last package that is not expired.

    so I wrote this relation in Model_User :

    protected static $_has_many = array(
            'package' => array(
                'key_from' => 'id',
                'model_to' => 'Model_Packagecart',
                'key_to' => 'user_id',
                'conditions' => array(
                   'where'=>array(array('expired_at','>',time())),
                   'order_by' => array(
                        'id' => 'DESC'
                    ),
                   'limit'=>1,
                ),
            ),
        );

    But this shows error in Where condition, Also limit doesnot work but if i place 
    where=>array('id'=>1),
    it works.
    Can Anyone say what should I do?
    to get the last unexpired row??

    Thanks,
    Ishwor
  • The problem here seems to be your lack of PHP knowledge. ;)

    You are using the function time() in a variable definition, and you are not allowed to, so that is the PHP syntax error you are getting.

    If you want define conditions based on runtime information, you need to use the init class method: 

        /**
         * filter packages on expiration timestamp
         */
        public static function _init()
        {
            parent::_init();

            // add a where clause, we need DB::expr here, we're injecting a literal
            static::$_has_many['package']['conditions']['where'] = array(array(['expired_at', '>', \DB::expr(time())));
        }

Howdy, Stranger!

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

In this Discussion