How to use table() in a service?

Notice: This thread is very old.
netteman
Member | 125
+
0
-

Hi, when I write this:

public function __construct(Nette\Database\Context $database)
{
    $this->database = $database;
}

into the HomepagePresenter.php I can use $this->database->table(‘posts’);

How can I use ->table() in a service? I've tried adding something like this

use Nette\Database\Context;

$database = new Context($connection);

to the code below but it didn't work =/

Container code

namespace App\Presenters;
use Nette;

class MyContainer extends Nette\DI\Container{


    function createConnection()
    {
        return new Nette\Database\Connection('mysql:host=127.0.0.1;dbname=quickstart', 'root', '');
    }

    protected function createServiceArticle()
    {
        return new Article($this->createConnection());
    }


}//end class

Service code (article)

<?php

namespace App\Presenters;

class Article{
    public $database;

    public function __construct($db){
        $this->database = $db;
    }

    public function returnPosts() {
        return $this->database->query("select * from posts");
    }
}//end class
CZechBoY
Member | 3608
+
0
-

You have to specify typehint

use Nette\Database\Context;

class Article
{
	private $db;

	public function __construct (Context $db)
	{
		$this->db = $db;
	}
}
netteman
Member | 125
+
0
-

CZechBoY wrote:

You have to specify typehint

use Nette\Database\Context;

class Article
{
	private $db;

	public function __construct (Context $db)
	{
		$this->db = $db;
	}
}

Your code resulted in Argument 1 passed to App\Presenters\Article::__construct() must be an instance of Nette\Database\Context, instance of Nette\Database\Connection given

So I changed the container and got this error instead

Argument 2 passed to Nette\Database\Context::__construct() must implement interface Nette\Database\IStructure, none given, called in D:\PHP\easyPHP\www\nette-di\app\presenters\MyContainer.php on line 12 and defined

Any idea how to fix it?

Here's the code of MyContainer and Article

<?php

namespace App\Presenters;
use Nette;

class MyContainer extends Nette\DI\Container{

    function createConnection()
    {
        $connection = new Nette\Database\Connection('mysql:host=127.0.0.1;dbname=quickstart', 'root', '');
        $context = new Nette\Database\Context($connection);
        return $context;
    }

    protected function createServiceArticle()
    {
        return new Article($this->createConnection());
    }


}//end class
<?php

namespace App\Presenters;

use Nette\Database\Context;

class Article{
    private $db;

    public function __construct (Context $db)
    {
        $this->db = $db;
    }

    public function returnPosts() {
        return $this->db->table('posts');
    }
}//end class
CZechBoY
Member | 3608
+
0
-

btw why are you not using services?

netteman
Member | 125
+
0
-

I went through the quickstart https://doc.nette.org/cs/quickstart and now I'm experimenting with nette so when you ask why I'm not using services I'm not quite sure what you are talking about ;-) All I'm trying to achieve right now is to be able to use ->table() in an object of the Article class instantiated in MyContainer.

CZechBoY
Member | 3608
+
0
-

https://doc.nette.org/en/configuring
in your bootstrap.php load new configuration fileand fill that file with section services and there you shoud insert all services that should be registered in dic (dependency injection container).
eg. config.neon

services:
	- App\Model\UserRepository
netteman
Member | 125
+
0
-

After couple of days experimenting with services I decided to gave up using ->table() in a service. The documentation seems to be too concise to be comprehensive for me, but thanks anyway, CZechBoY.

CZechBoY
Member | 3608
+
0
-

Which part of documentation is not clear? We can improve it…

netteman
Member | 125
+
0
-

As the page says: “This page is still being prepared” https://doc.nette.org/en/configuring but the section I've read: Custom services probably makes sense for people who have been working with nette for years. But those people probably won't need the documentation. Here's a couple of questions I was faced with.

services:
database: Nette\Database\Connection(%dsn%, %user%, %password%)

should I put it in a config.local.neon? Should I put it in a new neon file and load it? Does it matter or not? Does it connect to database right away or is it still necessary to getService or something else?

How do I access %dsn% ? %parameters.dsn% or just %dsn% ? ne-on.org doesn't say. Does number of “tabs” matter? I have no idea.

A piece of neon code generates a function. But it doesn't say how to use the function – where can I use it? How do I get access to it? Do I have to create an object or not? If yes, which class I should use?

full example – I tried to copy-paste the example in new project and the only thing I got was an error.

Quickstart is pretty sweet. I can use copy & paste. I can try to adapt the code or create something new on my own and see what happens. Quickstart provides you with all the code involved. “Configuring” provides you with random pieces of information taken out of context because you don't know the rest of the code and this fact renders this page as unuseable for people who are learning nette in my opinion.

Documentation written in the style of quickstart would we great.

Edit --------------
Another example

https://doc.nette.org/en/database#…

$database = new Context($connection);

returns error (it asks for context). Is the example wrong or do I need another part of code ommited from the example?

Last edited by netteman (2016-05-23 15:39)

netteman
Member | 125
+
0
-

Here's the complete solution with explanations!

https://doc.nette.org/…kstart/model

I read the English version and this part of quick start isn't in the English version of documentation.

Last edited by netteman (2016-05-25 19:26)