nefunguje injectConfigParams

jpobs
Člen | 6
+
0
-

ahoj, uz si s tim nevim rady.
mam tu entitu app/models/documents/entity/accountingDocument.php

nasledujici kod:

public function injectConfigParams(ConfigParams $config){
$this->config = $config->getParams()[‚roundingMethod‘][‚roundingMethodNoRounding‘];
print_r($this->config);
 }

public function __construct(
ActiveRow $row = NULL) {

$this->price = 0;
$this->tax = 0;
$this->priceTotal = 0;
$this->rounding = 0;
$this->discountPercentage = 0;
$this->discountPrice = 0;
$this->discountPriceTotal = 0;
$this->discountTax = 0;

parent::__construct($row);
if ($this->roundingMethod === NULL) {
// výchozí způsob zaokrouhlování
if ($this->config) $this->roundingMethod = self::ROUNDING_NO_ROUNDING;
else $this->roundingMethod = self::ROUNDING_TOTAL_MATH;
}
 }

potrebuji aby se mi z local.config.neon nacetl parametr roundingMethodNoRounding a pouzil se v construktoru v podmince if ($this->config) predtim to bylo bez podminky, ale potrebuji to nacitat z configu

parameters:
showFulltext: true
testing: false

roundingMethod:
roundingMethodNoRounding: true

dekuji moc za pomoc

Editoval jpobs (29. 9. 2021 17:39)

Marek Bartoš
Nette Blogger | 1171
+
0
-

Jestliže vytváříš objekt přes new, tedy např. new AccountingDocument(), tak se ti závislosti do třídy nemají jak dostat, ty se předávají pouze službám v DI kontejneru.

inject*() metody a @inject anotace properties navíc jsou zapnuté pouze pro presentery. Lze je sice zapnout i pro ostatní služby, ale správně bys měl ConfigParams vyžadovat v konstruktoru.

V tvém případě bys mohl třídu vytvářet přes create:

<?php declare(strict_types=1);

namespace App\Example;

use Nette\Database\Table\ActiveRow;

interface AccountingDocumentFactory
{

	public function create(?ActiveRow $activeRow = null): AccountingDocument;

}

Tu zaregistruješ do služeb

services:
	- App\Example\AccountingDocumentFactory

Konstruktor v AccountingDocument upravíš takto

public function __construct(?ActiveRow $row = null, ConfigParams $config) {}

A následně budeš dokument vytvářet přes factory.

A používej prosím zvýraznění kódu, takto se to vážně blbě čte. Nad editorem jsou pro to tlačítka.

jpobs
Člen | 6
+
0
-

ahoj dekuji moc za rychlou reakci. Zapomnel jsem uvest , ze projekt jede na php 5.64 nemuzu to zmenit, kdyz bych mel upgradovat tak bych se asi z toho zblaznil.

myslim, ze tu uz i factory pro accountingDocument je:

<?php

namespace Model\Documents\Factory;

use Model\Documents\Repository\AccountingDocumentRepository;
use Nette\Database\Connection;

/**
 * Description of AccountingDocument
 *
 */
class AccountingDocument {
	/** @var Connection */
	protected $database;

	/** @var AccountingDocumentRepository */
	protected $repository;

	public function __construct(Connection $database) {
		$this->database = $database;
		$this->repository = NULL;
	}

	/**
	 * Vytváří AccountingDocumentRepository
	 * @return AccountingDocumentRepository
	 */
	public function create() {
		if ($this->repository === NULL)
			$this->repository = new AccountingDocumentRepository($this->database);

		return $this->repository;
	}
}

mel bych ji tedy upravit nejak podobne jak jsi psal, nebo mam vytvorit dalsi novou AccountingDocumentFactory ? to jsem udelal a hodilo mi to nasledujici chybu:

syntax error, unexpected ‚?‘, expecting variable (T_VARIABLE)

kod vypada nyni takto:

<?php

namespace Model\Documents\Factory;

use Nette\Database\Table\ActiveRow;

interface AccountingDocumentFactory
{

	public function create(?ActiveRow $activeRow = null): AccountingDocument;

}

jo tak jeste jsem dal ty otazniky pryc ?ActiveRow a haze mi to chybu:

Parse Error
syntax error, unexpected ':', expecting ';' or '{' search►

Source file ▼
File: .../www/app/models/Documents/Repository/Factory/AccountigDocumentFactory.php   Line: 10

 1:    <?php
 2:
 3:    namespace Model\Documents\Factory;
 4:
 5:    use Nette\Database\Table\ActiveRow;
 6:
 7:    interface AccountingDocumentFactory
 8:    {
 9:
10:        public function create(ActiveRow $activeRow = null): AccountingDocument;
11:
12:    }

v config.local.neon to mam takto:

services:
AccountingDocument:
class: \Model\Documents\Entity\AccountingDocument
AccountingDocumentFactory:
class: \Model\Documents\Factory\AccountingDocumentFactory

AccountingDocument.php

	/** @var Boolean */
	protected $config;

	public function __construct(?ActiveRow $row = null, ConfigParams $config) {
		$this->config = $config->getParams()['roundingMethod']['roundingMethodNoRounding'];

		$this->price = 0;
		$this->tax = 0;
		$this->priceTotal = 0;
		$this->rounding = 0;
		$this->discountPercentage = 0;
		$this->discountPrice = 0;
		$this->discountPriceTotal = 0;
		$this->discountTax = 0;

		parent::__construct($row);
		if ($this->roundingMethod === NULL) {
			// výchozí způsob zaokrouhlování
			if ($this->config) $this->roundingMethod = self::ROUNDING_NO_ROUNDING;
			else $this->roundingMethod = self::ROUNDING_TOTAL_MATH;
		}
	}


Editoval jpobs (30. 9. 2021 7:42)

Michal Kumžák
Člen | 106
+
0
-

PHP 5 podle mě neumí return type, takže to uprav takto

interface AccountingDocumentFactory
{
	/** @return AccountingDocument */
	public function create(ActiveRow $activeRow = null);
}
jpobs
Člen | 6
+
0
-

Michal Kumžák napsal(a):

PHP 5 podle mě neumí return type, takže to uprav takto

interface AccountingDocumentFactory
{
	/** @return AccountingDocument */
	public function create(ActiveRow $activeRow = null);
}

tak jsem to zmenil

mam tu nasledujici chybu:

Service 'AccountingDocumentFactory': Unknown key '0' in definition of service.

v config.local.neon to mam takto

services:
	AccountingDocument:
		class: \Model\Documents\Entity\AccountingDocument
	AccountingDocumentFactory:
		- App\Model\Documents\Factory\AccountingDocumentFactory



<?php

namespace Model\Documents\Factory;

use Nette\Database\Table\ActiveRow;

interface AccountingDocumentFactory
{
	/** @return AccountingDocument */
	public function create(ActiveRow $activeRow = null);
}

Entity\AccountingDocument.php



	public function __construct(ActiveRow $row = null, ConfigParams $config) {
		$this->config = $config->getParams()['roundingMethod']['roundingMethodNoRounding'];

		$this->price = 0;
		$this->tax = 0;
		$this->priceTotal = 0;
		$this->rounding = 0;
		$this->discountPercentage = 0;
		$this->discountPrice = 0;
		$this->discountPriceTotal = 0;
		$this->discountTax = 0;

		parent::__construct($row);
		if ($this->roundingMethod === NULL) {
			// výchozí způsob zaokrouhlování
			if ($this->config) $this->roundingMethod = self::ROUNDING_NO_ROUNDING;
			else $this->roundingMethod = self::ROUNDING_TOTAL_MATH;
		}
	}



Editoval jpobs (30. 9. 2021 10:01)

Michal Kumžák
Člen | 106
+
0
-

Ve chvíli kdy voláš metodu create, musíš do ní poslat ActiveRow. Jinak v configu máš podle mě zbytečně navíc část s AccountingDocument, protože to ti přece vytvoří ta továrnička.

Marek Bartoš
Nette Blogger | 1171
+
0
-

Jestliže přijímáš ActiveRow, tak by AccountingDocument asi neměl být služba, ne? ActiveRow není služba, ale záznam z databáze. A máš špatně zaregistrovanou továrnu. Říká ti to,že nezná klíč '0', protože do služby AccountingDocumentFactory předáváš pole a ne název služby.

services:
	AccountingDocument:
		class: \Model\Documents\Entity\AccountingDocument
	AccountingDocumentFactory:
		- App\Model\Documents\Factory\AccountingDocumentFactory

Správně tedy takto

services:
	AccountingDocumentFactory: App\Model\Documents\Factory\AccountingDocumentFactory

Nebo takto

services:
	AccountingDocumentFactory:
		class: App\Model\Documents\Factory\AccountingDocumentFactory
jpobs
Člen | 6
+
0
-

Marek Bartoš napsal(a):

Jestliže přijímáš ActiveRow, tak by AccountingDocument asi neměl být služba, ne? ActiveRow není služba, ale záznam z databáze. A máš špatně zaregistrovanou továrnu. Říká ti to,že nezná klíč '0', protože do služby AccountingDocumentFactory předáváš pole a ne název služby.

services:
	AccountingDocument:
		class: \Model\Documents\Entity\AccountingDocument
	AccountingDocumentFactory:
		- App\Model\Documents\Factory\AccountingDocumentFactory

Správně tedy takto

services:
	AccountingDocumentFactory: App\Model\Documents\Factory\AccountingDocumentFactory

Nebo takto

services:
	AccountingDocumentFactory:
		class: App\Model\Documents\Factory\AccountingDocumentFactory

zmenil jsem to v services a pise mi to nasledujici chybu :

Nette\InvalidStateException
Class \Model\Documents\Factory\AccountingDocumentFactory used in service ‚AccountingDocumentFactory‘ has not been found or is not instantiable.

services:
	AccountingDocument:
		class: \Model\Documents\Entity\AccountingDocument

	AccountingDocumentFactory:
		class: App\Model\Documents\Factory\AccountingDocumentFactory

Marek Bartoš
Nette Blogger | 1171
+
0
-

Tak místo class použít implement. Myslel jsem že to bylo ve starším Nette jinak. A AccountingDocument nemá být služba.

Michal Kumžák
Člen | 106
+
0
-

Ne má špatně cestu, má namespace

namespace Model\Documents\Factory;

a v configu má

App\Model\Documents\Factory\AccountingDocumentFactory

Zapsal bych to takto úplně zkráceným zápisem:

services:
 - Model\Documents\Factory\AccountingDocumentFactory
jpobs
Člen | 6
+
0
-

Michal Kumžák napsal(a):

Ne má špatně cestu, má namespace

namespace Model\Documents\Factory;

a v configu má

App\Model\Documents\Factory\AccountingDocumentFactory

Zapsal bych to takto úplně zkráceným zápisem:

services:
 - Model\Documents\Factory\AccountingDocumentFactory

tak jsem to opet zmenil

chyba :
Nette\InvalidStateException
Class Model\Documents\Factory\AccountingDocumentFactory used in service ‚76‘ has not been found or is not instantiable.

ted mam v services

services:
	AccountingDocument:
		class: \Model\Documents\Entity\AccountingDocument

	- Model\Documents\Factory\AccountingDocumentFactory

Michal Kumžák
Člen | 106
+
0
-

Zruš úplne z configu tu část s AccountingDocument, tu k ničemu nepotřebuješ. Tu třídu ti vytváří ta továrnička.

services:
	- Model\Documents\Factory\AccountingDocumentFactory

Můžeš sem dát celou tu továrnu, ať vidíme jak to máš napsané? A zkontroluj si název souboru, musí být stejný jako název třídy.

jpobs
Člen | 6
+
0
-

Michal Kumžák napsal(a):

Zruš úplne z configu tu část s AccountingDocument, tu k ničemu nepotřebuješ. Tu třídu ti vytváří ta továrnička.

services:
	- Model\Documents\Factory\AccountingDocumentFactory

Můžeš sem dát celou tu továrnu, ať vidíme jak to máš napsané? A zkontroluj si název souboru, musí být stejný jako název třídy.

mam tu opet error:

Nette\InvalidStateException
Class Model\Documents\Factory\AccountingDocumentFactory used in service ‚75‘ has not been found or is not instantiable.

kod vypada nasledovne:

accountingDocument.php

<?php

namespace Model\Documents\Entity;

use Exception;
use Model\Contacts\Entity\Branch;
use Model\Contacts\Entity\Entity;
use Model\Documents\Entity\Items\AccountingDocumentItem;
use Model\Stores\Entity\DocumentSerie;
use Nette\ArrayHash;
use Nette\Callback;
use Nette\Database\Table\ActiveRow;
use Nette\DateTime;
use Services\ConfigParams;


/**
 * Description of AccountingDocument
 *
 */
class AccountingDocument extends Document {

	/* total_math = celková částka zaokrouhlena matematicky */
	const ROUNDING_TOTAL_MATH = 'total_math';
	/* total_down = celková částka zaokrouhlena vždy dolu */
	const ROUNDING_TOTAL_DOWN = 'total_down';
	/* total_up = celková částka zaokrouhlena vždy nahoru */
	const ROUNDING_TOTAL_UP = 'total_up';
	/* base_vat_up = základ daně a daň zaokrouhlena vždy nahoru */
	const ROUNDING_BASE_VAT_UP = 'base_vat_up';
	/* no_rounding = nezaokrouhleno */
	const ROUNDING_NO_ROUNDING = 'no_rounding';
...

	/** @var Boolean */
	protected $config;

	public function __construct(ActiveRow $row = null, ConfigParams $config) {
		$this->config = $config->getParams()['roundingMethod']['roundingMethodNoRounding'];

		$this->price = 0;
		$this->tax = 0;
		$this->priceTotal = 0;
		$this->rounding = 0;
		$this->discountPercentage = 0;
		$this->discountPrice = 0;
		$this->discountPriceTotal = 0;
		$this->discountTax = 0;

		parent::__construct($row);
		if ($this->roundingMethod === NULL) {
			// výchozí způsob zaokrouhlování
			if ($this->config) $this->roundingMethod = self::ROUNDING_NO_ROUNDING;
			else $this->roundingMethod = self::ROUNDING_TOTAL_MATH;
		}
	}

dalsi accountingDocument.php v repository/factory

<?php

namespace Model\Documents\Factory;

use Model\Documents\Repository\AccountingDocumentRepository;
use Nette\Database\Connection;

/**
 * Description of AccountingDocument
 *
 */
class AccountingDocument {
	/** @var Connection */
	protected $database;

	/** @var AccountingDocumentRepository */
	protected $repository;

	public function __construct(Connection $database) {
		$this->database = $database;
		$this->repository = NULL;
	}

	/**
	 * Vytváří AccountingDocumentRepository
	 * @return AccountingDocumentRepository
	 */
	public function create() {
		if ($this->repository === NULL)
			$this->repository = new AccountingDocumentRepository($this->database);

		return $this->repository;
	}
}

accountingDocumentFactory.php jak jste mi poradili

<?php

namespace Model\Documents\Factory;

use Nette\Database\Table\ActiveRow;

interface AccountingDocumentFactory
{
	/** @return AccountingDocument */
	public function create(ActiveRow $activeRow = null);
}

config.local.neon

services:
	# AccountingDocument:
	# 	class: \Model\Documents\Entity\AccountingDocument

	- Model\Documents\Factory\AccountingDocumentFactory

config.neon

services:
		# AccountingDocument:
		# 	class: \Model\Documents\Entity\AccountingDocument

		accountingDocumentModel:
			class: \Model\Documents\Factory\AccountingDocument

dekuji vam za vsechny rady, jiz jsem problem vyresil

Editoval jpobs (5. 10. 2021 14:29)