Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model related
  • Hello there,

    I have create a model Product and another one called Cart.
    Is it possible every time that I call Product the Cart relation execute automatically?

    For instance:

     public static function _init()
        {
    $cartId= \Session::get('cart_id');
     array_push(static::$_conditions['where'], array('cart.id', '=', $cartId));
        }

    Is it possibe to do it?
  • HarroHarro
    Accepted Answer
    What  do you mean exactly by "execute"? What do you functionally want to achieve?
  • I realized that when a model's find method is executed the _init method is executed first as a construct.
    I have a model called Machine and another one called Shift. In the Shift model there is the machine_id property, and in the Machine model there is the subsidiary_id property, when I execute Shifit :: find I would like to take only the results where subsidiary_id that is in Machine is equal to the value passed.

        public static function _init()
        {
            $userSubsidiary = \Session::get('subsidiary_id', null);

            (!is_null($userSubsidiary)) && array_push(static::$_conditions['where'], array('machine.subsidiary_id', '=', $userSubsidiary));
        }
  • HarroHarro
    Accepted Answer
    That works if that id won't change during the time of the request.
  • This work only if a user related method where I call Shifit model:
    Shift::query()
    ->related('machine')
    ->get();

    If I call Shift Model without related an error occurs because machine.subsidiary_id does not exists. I'd like that related(machine) would be considered when I call Shift::query()
    ->get(); in _init method without having to explicitly use Related(machine)

Howdy, Stranger!

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

In this Discussion