diff --git a/Database.php b/Database.php index 6d360a9..b88bb04 100644 --- a/Database.php +++ b/Database.php @@ -76,7 +76,10 @@ public function __construct($opts) /** @var callable */ private $_type; + + /** @var callable|null */ private $_debugCallback; + private $query_driver; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -127,7 +130,7 @@ public function commit() * @param array $where Where condition for what to select - see {@see * Query->where()}. * - * @return Number + * @return int */ public function count($table, $field = 'id', $where = null) { @@ -145,14 +148,14 @@ public function count($table, $field = 'id', $where = null) /** * Get / set debug mode. * - * @param bool $set Debug mode state. If not given, then used as a getter. + * @param false|callable $set Debug mode state. If not given, then used as a getter. * * @return ($set is null ? bool : $this) Debug mode state if no parameter is given. */ public function debug($set = null) { if ($set === null) { - return $this->_debugCallback ? true : false; + return is_callable($this->_debugCallback) ? true : false; } elseif ($set === false) { $this->_debugCallback = null; } else { diff --git a/Editor.php b/Editor.php index 862e403..ab302ca 100644 --- a/Editor.php +++ b/Editor.php @@ -705,7 +705,8 @@ public function process($data) $debugInfo = &$this->_debugInfo; $debugInfo[] = 'Editor PHP libraries - version ' . $this->version; - $debugVal = $this->_db->debug(static function ($mess) use (&$debugInfo) { + + $this->_db->debug(function ($mess) use (&$debugInfo) { $debugInfo[] = $mess; }); } @@ -988,11 +989,12 @@ public function write($_writeVal = null) private function _process($data) { $this->_out = [ - 'fieldErrors' => [], - 'error' => '', + 'cancelled' => [], 'data' => [], + 'error' => '', + 'fieldErrors' => [], 'ipOpts' => [], - 'cancelled' => [], + 'options' => [] ]; $action = Editor::action($data); @@ -1154,11 +1156,8 @@ private function _get($id = null, $http = null) } $res = $query->exec(); - if (!$res) { - throw new \Exception('Error executing SQL for data get. Enable SQL debug using `->debug(true)`'); - } - $out = []; + while ($row = $res->fetch()) { $inner = []; $inner['DT_RowId'] = $this->_idPrefix . $this->pkeyToValue($row, true); @@ -1174,15 +1173,18 @@ private function _get($id = null, $http = null) // Row based "joins" for ($i = 0; $i < count($this->_join); ++$i) { - $this->_join[$i]->data($this, $out, $options); + $this->_join[$i]->data($this, $out); } $this->_trigger('postGet', $out, $id); - return [ - 'data' => $out, - 'files' => $this->_fileData(null, null, $out), - ]; + return array_merge( + [ + 'data' => $out, + 'files' => $this->_fileData(null, null, $out), + ], + $ssp + ); } /** @@ -2047,7 +2049,7 @@ private function _get_where($query) * @param string $name Field name * @param string $type Matching name type * - * @return Field Field instance + * @return Field|null Field instance or null if not found */ private function _find_field($name, $type) { @@ -2067,14 +2069,14 @@ private function _find_field($name, $type) /** * Insert or update a row for all main tables and left joined tables. * - * @param int|string $id ID to use to condition the update. If null is - * given, the first query performed is an insert and the inserted id - * used as the value should there be any subsequent tables to operate - * on. Mote that for compound keys, this should be the "joined" value - * (i.e. a single string rather than an array). + * @param int|string|null $id ID to use to condition the update. If null is + * given, the first query performed is an insert and the inserted id + * used as the value should there be any subsequent tables to operate + * on. Mote that for compound keys, this should be the "joined" value + * (i.e. a single string rather than an array). * - * @return Database\Result Result from the query or null if no - * query performed. + * @return int|string Result from the query or null if no + * query performed. */ private function _insert_or_update($id, $values) { @@ -2157,8 +2159,8 @@ private function _insert_or_update($id, $values) * @param string $table Database table name to use (can include an alias) * @param array $where Update condition * - * @return Database\Result Result from the query or null if no query - * performed. + * @return Database\Result|null Result from the query or null if no query + * performed. */ private function _insert_or_update_table($table, $values, $where = null) { @@ -2251,13 +2253,9 @@ private function _options($refresh) $options = $field->options(); if ($options) { - $opts = $options($this->_db, $refresh); + $opts = $options->exec($this->_db, $refresh); if ($opts !== false) { - if (!isset($this->_out['options'])) { - $this->_out['options'] = []; - } - $this->_out['options'][$field->name()] = $opts; } } @@ -2286,6 +2284,10 @@ private function _options($refresh) } } } + + for ($i = 0; $i < count($this->_join); ++$i) { + $this->_join[$i]->options($this->_out['options'], $this->_db, $refresh); + } } private function _optionsSearch() diff --git a/Editor/Field.php b/Editor/Field.php index 4a18b56..d1de4f2 100644 --- a/Editor/Field.php +++ b/Editor/Field.php @@ -153,8 +153,8 @@ public function __construct($dbField = null, $name = null) /** @var array[] */ private $_validator = []; - /** @var Upload */ - private $_upload; + /** @var Upload|null */ + private $_upload = null; /** @var callable */ private $_xss; @@ -297,7 +297,7 @@ public function name($_ = null) * @param callable(array): string $format Function will render each label * @param string $order SQL ordering * - * @return ($table is null ? Options : $this) + * @return ($table is null ? Options|null : $this) */ public function options($table = null, $value = null, $label = null, $condition = null, $format = null, $order = null) { @@ -470,7 +470,7 @@ public function setValue($_ = null) * * @param Upload $_ Upload class if used as a setter * - * @return ($_ is null ? Upload : $this) Value if used as a getter. + * @return ($_ is null ? Upload|null : $this) Value if used as a getter. */ public function upload($_ = null) { diff --git a/Editor/Join.php b/Editor/Join.php index 5c0696c..079832e 100644 --- a/Editor/Join.php +++ b/Editor/Join.php @@ -486,11 +486,10 @@ public function whereSet($_ = null) * @param Editor $editor Host Editor instance * @param mixed[] $data Data from the parent table's get and were we need * to add out output. - * @param array $options options array for fields * * @internal */ - public function data($editor, &$data, &$options) + public function data($editor, &$data) { if (!$this->_get) { return; @@ -640,18 +639,6 @@ public function data($editor, &$data, &$options) } } } - - // Field options - foreach ($this->_fields as $field) { - $opts = $field->optionsExec($db); - - if ($opts !== false) { - $name = $this->name() . - ($this->_type === 'object' ? '.' : '[].') . - $field->name(); - $options[$name] = $opts; - } - } } /** @@ -687,6 +674,38 @@ public function create($editor, $parentId, $data) } } + /** + * Get options for the fields in this join + * + * @param array $options Array to write the read options into + * @param Database $db Database connection object + * @param bool $refresh Refresh indication flag + * @return void + * + * @internal + */ + public function options(&$options, $db, $refresh) { + // Field options + foreach ($this->_fields as $field) { + $optsInst = $field->options(); + + if ($optsInst) { + $opts = $optsInst->exec($db, $refresh); + + if ($opts !== false) { + if ($this->_type === 'object') { + $name = $this->name() . '.' . $field->name(); + } + else { + $name = $this->name() . '[].' . $field->name(); + } + + $options[$name] = $opts; + } + } + } + } + /** * Update a row. * diff --git a/Editor/Options.php b/Editor/Options.php index a189800..6094be5 100644 --- a/Editor/Options.php +++ b/Editor/Options.php @@ -101,7 +101,7 @@ class Options extends Ext private $_manualAdd = []; /** @var callable|null */ - private $_customFn; + private $_customFn = null; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Public methods @@ -301,14 +301,15 @@ public function where($_ = null) /** * Execute the options (i.e. get them). * - * @param Database $db Database connection - * @param bool $refresh Indicate if this is a refresh or a full load + * @param Database $db Database connection + * @param bool $refresh Indicate if this is a refresh or a full load + * @param string|null $search Search term * - * @return array List of options + * @return array|false List of options * * @internal */ - public function exec($db, $refresh, $search) + public function exec($db, $refresh, $search=null) { // If search only, and not a search action, then just return false if ($this->searchOnly() && !$search) { @@ -320,8 +321,9 @@ public function exec($db, $refresh, $search) return false; } - if ($this->fn()) { - return $this->_customFn($db); + $fn = $this->_customFn; + if (is_callable($fn)) { + return $fn($db); } $label = $this->_label; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 51254da..f8c8755 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -115,51 +115,6 @@ parameters: count: 1 path: Editor.php - - - message: "#^If condition is always true\\.$#" - count: 3 - path: Editor.php - - - - message: "#^Left side of && is always true\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Method DataTables\\\\Editor\\:\\:_find_field\\(\\) should return DataTables\\\\Editor\\\\Field but returns null\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Method DataTables\\\\Editor\\:\\:_insert_or_update\\(\\) should return DataTables\\\\Database\\\\Result but returns int\\|string\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Method DataTables\\\\Editor\\:\\:_insert_or_update_table\\(\\) should return DataTables\\\\Database\\\\Result but returns null\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Negated boolean expression is always false\\.$#" - count: 8 - path: Editor.php - - - - message: "#^Parameter \\#1 \\$id of method DataTables\\\\Editor\\:\\:_insert_or_update\\(\\) expects int\\|string, null given\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Parameter \\#1 \\$pkeyVal of method DataTables\\\\Editor\\:\\:_pkey_submit_merge\\(\\) expects string, DataTables\\\\Database\\\\Result given\\.$#" - count: 1 - path: Editor.php - - - - message: "#^Parameter \\#1 \\$set of method DataTables\\\\Database\\:\\:debug\\(\\) expects bool\\|null, Closure given\\.$#" - count: 1 - path: Editor.php - - message: "#^Parameter \\#2 \\$parentId of method DataTables\\\\Editor\\\\Join\\:\\:create\\(\\) expects int, string given\\.$#" count: 1 @@ -182,11 +137,6 @@ parameters: - message: "#^Result of && is always false\\.$#" - count: 2 - path: Editor.php - - - - message: "#^Strict comparison using \\!\\=\\= between DataTables\\\\Database\\\\Result and null will always evaluate to true\\.$#" count: 1 path: Editor.php @@ -195,11 +145,6 @@ parameters: count: 1 path: Editor.php - - - message: "#^Strict comparison using \\!\\=\\= between int\\|string and null will always evaluate to true\\.$#" - count: 1 - path: Editor.php - - message: "#^Strict comparison using \\!\\=\\= between string and null will always evaluate to true\\.$#" count: 1 @@ -210,11 +155,6 @@ parameters: count: 1 path: Editor.php - - - message: "#^Strict comparison using \\=\\=\\= between DataTables\\\\Database\\\\Result and null will always evaluate to false\\.$#" - count: 1 - path: Editor.php - - message: "#^Strict comparison using \\=\\=\\= between int and false will always evaluate to false\\.$#" count: 1 @@ -260,11 +200,6 @@ parameters: count: 1 path: Editor/Field.php - - - message: "#^Property DataTables\\\\Editor\\\\Field\\:\\:\\$_upload is never written, only read\\.$#" - count: 1 - path: Editor/Field.php - - message: "#^Strict comparison using \\=\\=\\= between \\(callable\\(mixed\\)\\: string\\)\\|false and true will always evaluate to false\\.$#" count: 1 @@ -390,11 +325,6 @@ parameters: count: 1 path: Editor/Options.php - - - message: "#^Property DataTables\\\\Editor\\\\Options\\:\\:\\$_order is never written, only read\\.$#" - count: 1 - path: Editor/Options.php - - message: "#^Property DataTables\\\\Editor\\\\Options\\:\\:\\$_renderer is never written, only read\\.$#" count: 1