Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Orm and Auth conditions
  • Hello,

    I am working on a small task managment tool for logged in user. For this, I use the ORM and the auth package.

    My goal is to limit all query's to the database tables to the specific user_id of the user logged in. I know, I could add to every single query a "where" condition  like "where user_id = XYZ", but is there a better way to do that?

    My first suggest was to add a "protected static $_conditions" variable to the Models. But I can't use "Auth::get_user_id()" in a static class variable:


    class Model_Settings extends \Orm\Model
    {
        protected static $_conditions = array(
            'where' => array(
                array('user_id', '=', Auth::get_user_id() ),
            )
        );
    }

    The funny thing is, that the code in the docs: http://fuelphp.com/docs/packages/orm/creating_models.html#/conditions
    Isn't working, since I get an error:
    ErrorException [ Parsing Error ]: syntax error, unexpected '(', expecting ')'
  • Ok, I solved it by setting a constant in the controller before method.
    At least, the documentation is wrong, since you can't use methods/functions in static variables.
  • HarroHarro
    Accepted Answer
    You can only use scalars in property definitions, no function calls. In any type of property. If needed, you can work around that by setting the value at runtime in the _init() method of the Model class.

    I'll fix the example, time() is a function call too, so obviously that fails for the same reason.

    Other then that, conditions work fine, both on the model and on individual relations.
  • Ok, I accept your (or mine?) answer :D

Howdy, Stranger!

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

In this Discussion