Thank You,
Here is the real world sample.
Can someone run it and show the output?
I tested this on mac and windows pc, but result is same, php 5.5.25, phalcon 2.0.2, mongo 3.0.3
Mongo Data:
> use testdb
switched to db testdb
> db.users.insert({name: "Oleg"})
WriteResult({ "nInserted" : 1 })
> db.users.insert({name: "James"})
WriteResult({ "nInserted" : 1 })
> db.users.find()
{ "_id" : ObjectId("556ecc523853a3a051cef3a2"), "name" : "Oleg" }
{ "_id" : ObjectId("556ecc6b3853a3a051cef3a3"), "name" : "James" }
>
<?php
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\Collection\Manager as CollectionManager;
use Phalcon\DI\FactoryDefault;
use Phalcon\Events\Manager as EventsManager;
try {
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(array(
'../app/controllers/',
'../app/models/'
))->register();
// Create a DI
$di = new FactoryDefault();
// Setup the view component
$di->set('view', function(){
$view = new View();
$view->setViewsDir('../app/views/');
return $view;
});
// Setup a base URI so that all generated URIs include the "tutorial" folder
$di->set('url', function(){
$url = new UrlProvider();
$url->setBaseUri('/tutorial/');
return $url;
});
// Simple database connection to localhost
$di->set('mongo', function() {
$mongo = new MongoClient();
return $mongo->selectDB("testdb");
}, true);
//Registering the collectionManager service
$di->set('collectionManager', function() {
$modelsManager = new CollectionManager();
return $modelsManager;
}, true);
// Handle the request
$application = new Application($di);
echo $application->handle()->getContent();
} catch(\Exception $e) {
echo "PhalconException: ", $e->getMessage();
}
<?php
use Phalcon\Mvc\Collection;
class User extends Collection
{
public $name;
public function initialize()
{
$this->setSource('users');
}
}
<?php
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function indexAction()
{
echo "<h1>Hello!</h1>";
$user1 = User::findById(new MongoId('556ecc523853a3a051cef3a2'));
$user2 = User::findById(new MongoId('556ecc6b3853a3a051cef3a3'));
echo $user1->name;
echo $user2->name;
}
}
Output:
Hello!
Oleg
Notice: Trying to get property of non-object in /phalcon/app/controllers/IndexController.php on line 16