We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

why models cant uses DI which i inject in services

my controller can usr my di ,but model can not use , whats wrong with it, hel-p pleace. thanks very much



51.2k

You should be able to call any service defined in DI from your model. For someone to help you, will need at least this information:

  • code snippet
  • phalcon version


577
edited Sep '15

follow is my services di code:

  $di->set('manager', function() {
  return new modelManager();
  });

follow is my Model Cars in namespace Model

public function test(){
    $phql="select * from Model\Cart";
    $rows = $this->manager->excuteQuery($phql);
}

at last Notice: Access to undefined property Models\Cars::manager in C:\wamp\www\front\app\models\Cars.php on line 29



51.2k
Accepted
answer

Can you rename "manger" with something else like "modelsManager" and try again ? I am not sure but I think "manager" is a reserved word.



577

what a f*&k thing, thanks a lot , i have find almost a day follow its session in model

class Robots extends Phalcon\Mvc\Model { public function beforeCreate() { $this->userId = $this->getDI()->getSession()->get('user-id'); } }