-1

Im new with php. Start an application using doctrine 2.

I have two files:

bootstrap.php:

<?php
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

require_once "vendor/autoload.php";

$isDevMode = true;
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/src"), $isDevMode);

$conn = array(
'driver' => 'pdo_pgsql',
'user' => 'postgres',
'password' => '123456', 
'host' => 'localhost', 
'dbname' =>'usrdb',
);

$entityManager = EntityManager::create($conn, $config);

Now i try to call $entityManager from another file:

<?php
   require_once "bootstrap.php";
   ///***code
   $session = $entityManager->find('Sessions', $_SESSION['id']);
   //**code again

And i get error:

Notice: Undefined variable: entityManager in D:\xampp\htdocs\mapFace-backend\geoserv.auth.login.php on line 23

Thsi files located in same directory.
What can be wrong?

UPDATE

Full code:

<?php
require '\bootstrap.php';
//require_once "geoserv.auth.lastact.php";
function login () {
    //ini_set ("session.use_trans_sid", true);
    //session_start();
    print_r($_SESSION['id']);
    if (isset($_SESSION['id'])){ 
        echo("SESSION");
        if(isset($_COOKIE['login']) && isset($_COOKIE['password'])){
            echo("COOKKIE");
            SetCookie("login", "", time() - 1, '/');            
            SetCookie("password","", time() - 1, '/');          

            setcookie ("login", $_COOKIE['login'], time() + 50000, '/');            

            setcookie ("password", $_COOKIE['password'], time() + 50000, '/');          

            $id = $_SESSION['id'];          
            //lastAct($id);             
            return true;
        } else {//иначе добавим cookie с логином и паролем, чтобы после перезапуска браузера сессия не слетала              
            $session = $entityManager->find('Sessions', $_SESSION['id']);               
            if ($session){ //если получена одна строка                              

                setcookie ("login", $session->getUserName(), time()+50000, '/');                
                setcookie ("password", md5($session->getUserName().$session->getPass()), time() + 50000, '/'); 

                $tm = time();
                $session->setLastAct($tm);
                $entityManager->persist($session);
                $entityManager->flush();
                return true;            
            }else{ 
                return false;
            }           
        }   
    }else{
        echo ("NO SESSION");
    }   
}       
4

1 回答 1

2
<?php
require 'bootstrap.php';  // remove /
//require_once "geoserv.auth.lastact.php";
function login ($entityManager) { // passed the variable here
    //ini_set ("session.use_trans_sid", true);
    //session_start();
    print_r($_SESSION['id']);
    if (isset($_SESSION['id'])){ 
        echo("SESSION");
        if(isset($_COOKIE['login']) && isset($_COOKIE['password'])){
            echo("COOKKIE");
            SetCookie("login", "", time() - 1, '/');            
            SetCookie("password","", time() - 1, '/');          

            setcookie ("login", $_COOKIE['login'], time() + 50000, '/');            

            setcookie ("password", $_COOKIE['password'], time() + 50000, '/');          

            $id = $_SESSION['id'];          
            //lastAct($id);             
            return true;
        } else {//иначе добавим cookie с логином и паролем, чтобы после перезапуска браузера сессия не слетала              
            $session = $entityManager->find('Sessions', $_SESSION['id']);               
            if ($session){ //если получена одна строка                              

                setcookie ("login", $session->getUserName(), time()+50000, '/');                
                setcookie ("password", md5($session->getUserName().$session->getPass()), time() + 50000, '/'); 

                $tm = time();
                $session->setLastAct($tm);
                $entityManager->persist($session);
                $entityManager->flush();
                return true;            
            }else{ 
                return false;
            }           
        }   
    }else{
        echo ("NO SESSION");
    }   
}       
于 2013-10-02T09:04:42.520 回答