Symfony The default context does not exist

You may have to use in your model class or somewhere else the “context instance” in your code but when this code is used by a task, a test or data fixture, you get a “The default contect does not exist” error.
To avoid that, you can use the sfContext::hasInstance() fonction. See below

class Demande extends BaseDemande
{
   public function save(Doctrine_Connection $con = null)
   {
       if(sfContext::hasInstance())
       {
          $id_service = sfContext::getInstance()->getUser()->getAttribute('id_service', false, 'sfMatchlib/user');        
          $this->setIdService($id_service);
        }
        parent::save($con);
    }
}

Leave a Reply