Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm
How to set condition in creating relation
Ishwor
July 2017
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
Harro
July 2017
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())));
}
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,088
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
260
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
July 2017