Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Condition errors
  • I would like to build a webshop with fuel. My problem is that I would like to get the prices from different stores and always want to use the lowest price. It looks like this: Products model (1 row): prodid, name
    has_many
    Prices model (many rows): prodid, storeid, price I wanted to use the relation conditions, but it didn't worked (Products has many relation): 'prices' => array(
    'key_from' => 'prodid',
    'model_to' => 'Model_Prices',
    'key_to' => 'prodid',
    'cascade_save' => false,
    'cascade_delete' => false,
    'conditions' => array(
    'where' => array(
    array('prices.price', '>', '0')
    )
    )
    ) It produces a 0 fieldname in the query:
    AND `prices`.`price` > `'0'`) I also wanted to use has_one relations and in the conditions I also tried different values and expressions but they didn't work.
    I know this lists only the prices that are over 0, but actually I would be happy when it work.
  • I'm looking for the same. I want to put static values in my relationship conditions. Would it be bad or even possible to have a "db::expr" type of method for orm? It would solve a lot of headaches. Thanks in advance!!
  • Would it be bad or even possible to have a "db::expr" type of method for orm? It would solve a lot of headaches.
    The ORM uses the QB to generate queries, thus you can just use DB::expr(). Any conditions are expected to be columns, thus to use something else you have to use DB::expr() here. To use the OP's code as an example:
    'prices' => array(
        'key_from' => 'prodid',
        'model_to' => 'Model_Prices',
        'key_to' => 'prodid',
        'cascade_save' => false,
        'cascade_delete' => false,
        'conditions' => array(
        'where' => array(
        array('prices.price', '>', DB::expr(0))
    )
    
  • Thanks for the quick reply Jelmer! I've tried that but I get syntax errors... unexpected '(', expecting ')' What am I doing it wrong here? protected static $_has_many = array(
    'userown' => array(
    'key_from' => 'id',
    'key_to' => 'id',
    'conditions' => array(
    'where' => array( array('user_id', '=', DB::expr('5')) ),
    ),
    ),
    );
    Thanks!!!!
  • I don't see any problems there, but if I were you I'd read the full error message and check the line number it mentions and start reading your code for where you made the syntax error. Those have nothing to do with the ORM, just with your code.
  • Eh, I can't define a function within a static array, so I had to change the array later on in the code. Thanks! Solved.

Howdy, Stranger!

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

In this Discussion