how to connect to database?

- milov
 - Member | 8
 
Hi everyone,
I am a beginner, totally in all: php and etc, only know a litle bit html and
css, so be a nice to me..
But, I decided to learn php using nette framework, my hand will dirty and my
brain will explode.
the question is:
I've uploaded nette from local host to subdomain, install, and I see my homepage (congratulation), but I dont know how to do next!
I've done creating database using adminer. But when i tried to connect the database using the tutorial “https://doc.nette.org/en/quickstart/home-page”.. my page getting error.
here my code:
config.local.neon:
`parameters:
database:
	dsn: ‘mysql:host=localhost;dbname=…_…’
	user: …_…
	password: X......`
and this is my homepage presenter:
`<?php
namespace App\Presenters;
use Nette;
class HomepagePresenter extends Nette\Application\UI\Presenter
{
	public function __construct(Nette\Database\Context $database)
	{
$this->database = $database;
	}
	public function renderDefault()
	{
	$this->template->posts = $this->database->table(‘posts’)
->order(‘created_at DESC’)
->limit(5);
 }
}
`
how to solve it?
thanks
milov

- nettolog
 - Member | 68
 
Hello, what is the error? Check bootstrap.php, if you are including both neon configs (local and classic), remove including config.neon. I had the same problem when I first started. In both configs there is a database connection and if one of them fails, error appears.
Try basicly:
//$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
(but there is a way to auto include right config (if localhost → config.local.neon; else → config.neon)

- milov
 - Member | 8
 
I tried to comment:
$configurator->addConfig(__DIR__ . '/config/config.neon');
//$configurator->addConfig(__DIR__ . '/config/config.local.neon');
and put database parameter to config.neon:
database:
	dsn: 'mysql:host=localhost;dbname=..._...'
	user: ..._....
	password: *.....*
hello, here is the error:
Server Error
We're sorry! The server encountered an internal error and was unable to complete your request. Please try again later.
error 500
here the subdomain which nette installed:
"":http://images.arslitera.net
thanks

- nettolog
 - Member | 68
 
Enable tracy so you will see details of error, add/edit this line in the bootstrap.php:
$configurator->setDebugMode(TRUE);
It may look something like this:
require __DIR__ . '/../vendor/autoload.php';
$configurator = new Nette\Configurator;
$configurator->setDebugMode(TRUE);
(.....)
Now everyone will see errors so use this only for development – on a production server you can enable tracy for specific ip adresses:
$configurator = new Nette\Configurator;
$configurator->setDebugMode('your_ip_adress_here');
Also it looks that error was successfully logged so you can check “logs” folder. But enable the tracy, it is really good thing for fast debug. :-)
Last edited by nettolog (2015-05-24 14:52)

- milov
 - Member | 8
 
Hello, I dont see anyword PDOException, error says: Nette\MemberAccessException..
I've changed bootstrap.php
$container = $configurator->createContainer();
to
$container->getByType("Nette\Database\Connection");
and now I get:
Notice
Undefined variable: container
thanks
Last edited by milov (2015-05-24 20:52)

- milov
 - Member | 8
 
hello again,
temp/cache are empty now.
here the tracey says:
Fatal Error
Call to a member function getByType() on a non-object search►
File: /home/arsliter/netteframe/app/bootstrap.php:19
 9:
10:    $configurator->setTempDirectory(__DIR__ . '/../temp');
11:
12:    $configurator->createRobotLoader()
13:        ->addDirectory(__DIR__)
14:        ->register();
15:
16:    $configurator->addConfig(__DIR__ . '/config/config.neon');
17:    //$configurator->addConfig(__DIR__ . '/config/config.local.neon');
18:
19:    $container->getByType("Nette\Database\Connection");
20:
21:    return $container;
22:
the red backgrounded is :
19:    $container->getByType("Nette\Database\Connection");
Exception (no red background):
ErrorException #01
message protected => "Call to a member function getByType() on a non-object"
string private => ""
code protected => 0
file protected => "/home/arsliter/netteframe/app/bootstrap.php"
line protected => 19
trace private => array (1)
0 => array (4)
function => "shutdownHandler"
class => "Tracy\Debugger"
type => "::"
args => array ()
previous private => NULL
severity protected => 1
Environment
$_SERVER
$_SESSION
empty
Included files (36)
Configuration options
HTTP request
HTTP response
=====================
thanks

- milov
 - Member | 8
 
here file from log folder:
/home/arsliter/netteframe/log/error.log:
[2015-05-24 19-00-13] PHP Notice: Undefined variable: container in /home/arsliter/netteframe/app/bootstrap.php:19  @  http://images.arslitera.net/
[2015-05-24 19-03-53] PHP Notice: Undefined variable: container in /home/arsliter/netteframe/app/bootstrap.php:19  @  http://images.arslitera.net/
[2015-05-24 19-08-22] PHP Notice: Undefined variable: container in /home/arsliter/netteframe/app/bootstrap.php:19  @  http://images.arslitera.net/
and I cant post file (exception.log) because forum system has a limit: Posts cannot be longer that 10000 characters.
many many thanks

- milov
 - Member | 8
 
hello. i put back $container = $configurator->createContainer();
delete cache
and here the result:
Nette\MemberAccessException
Cannot write to an undeclared property
App\Presenters\HomepagePresenter::$database.
Source file
File: /home/arsliter/netteframe/vendor/nette/utils/src/Utils/ObjectMixin.php:180
170:
171:            } elseif (self::hasProperty($class, $name)) { // unsetted property
172:                $_this->$name = $value;
173:
174:            } elseif (isset($methods[$m = 'set' . $uname])) { // property setter
175:                $_this->$m($value);
176:
177:            } else { // strict class
178:                $type = isset($methods['get' . $uname]) || isset($methods['is' . $uname])
179:                    ? 'a read-only' : 'an undeclared';
180:                throw new MemberAccessException("Cannot write to $type property $class::\$$name.");
181:            }
182:        }
183:
184:
red b:
180:                throw new MemberAccessException("Cannot write to $type property $class::\$$name.");
Call stack
/home/arsliter/netteframe/vendor/nette/utils/src/Utils/Object.php:138 source Nette\Utils\ObjectMixin:: set (arguments)
128:
129:        /**
130:         * Sets value of a property. Do not call directly.
131:         * @param  string  property name
132:         * @param  mixed   property value
133:         * @return void
134:         * @throws MemberAccessException if the property is not defined or is read-only
135:         */
136:        public function __set($name, $value)
137:        {
138:            ObjectMixin::set($this, $name, $value);
139:        }
140:
141:
142:        /**
red b:
138:            ObjectMixin::set($this, $name, $value);
/home/arsliter/netteframe/app/presenters/HomepagePresenter.php:12 source Nette\Object-> __set (arguments)
2:
 3:    namespace App\Presenters;
 4:
 5:    use Nette;
 6:
 7:
 8:    class HomepagePresenter extends Nette\Application\UI\Presenter
 9:    {
10:        public function __construct(Nette\Database\Context $database)
11:        {
12:            $this->database = $database;
13:        }
14:        public function renderDefault()
15:        {
16:        $this->template->posts = $this->database->table('posts')
red backg:
12:            $this->database = $database;
here the next result:
/home/arsliter/netteframe/temp/cache/Nette.Configurator/Container_4c70c1c2ea.php:177	source
App\Presenters\HomepagePresenter->	__construct (arguments)
red backg: /--php
177: $service = new
App\Presenters\HomepagePresenter($this->getService(‘database.default.context’));
\--
inner-code Container_4c70c1c2ea-> createServiceApplication__1 ()
/home/arsliter/netteframe/vendor/nette/di/src/DI/Container.php:159	source
call_user_func_array (arguments)
red backg:
177:            $service = new App\Presenters\HomepagePresenter($this->getService('database.default.context'));
/home/arsliter/netteframe/vendor/nette/application/src/Bridges/ApplicationDI/PresenterFactoryCallback.php:59	source
Nette\DI\Container->	createService (arguments)
red back:
59:            return $this->container->createService($services[0]);
$name
“application.1” (13)
inner-code	Nette\Bridges\ApplicationDI\PresenterFactoryCallback->	__invoke
(arguments)
/home/arsliter/netteframe/vendor/nette/application/src/Application/PresenterFactory.php:52	source
call_user_func (arguments)
red back:
52:            return call_user_func($this->factory, $this->getPresenterClass($name));
/home/arsliter/netteframe/vendor/nette/application/src/Application/Application.php:146	source
Nette\Application\PresenterFactory->	createPresenter (arguments)
red back:
146:            $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
$name
“Homepage” (8)
/home/arsliter/netteframe/vendor/nette/application/src/Application/Application.php:88	source
Nette\Application\Application->	processRequest (arguments)
red back:
88:                $this->processRequest($this->createInitialRequest());
…/images/index.php:4 source Nette\Application\Application-> run ()
4:    $container->getByType('Nette\Application\Application')->run();
Nette Application
Environment
HTTP request
HTTP response
Last edited by milov (2015-05-24 22:19)

- nettolog
 - Member | 68
 
I think you forgot to declare public $database. So edit HomePage presenter like this:
(....)
class HomepagePresenter extends Nette\Application\UI\Presenter
{
	public $database;
    public function __construct(Nette\Database\Context $database)
(....)
					Last edited by nettolog (2015-05-24 22:16)