Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlBuilder: lose parameters for aliasses #150

Merged
merged 3 commits into from
Jan 9, 2017
Merged

SqlBuilder: lose parameters for aliasses #150

merged 3 commits into from
Jan 9, 2017

Conversation

h4kuna
Copy link
Contributor

@h4kuna h4kuna commented Jan 5, 2017

Hi,
i found bug whose lose tables if you are using aliases. I get exception: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound'. Out sql is right if I call $selection->getSql().

Expected output sql looks like:

SELECT forms.insert_date, fd0.value AS 'name', fd1.value AS 'surname', fd2.value AS 'address'
FROM forms
LEFT JOIN form_data AS fd0 ON fd0.forms_id = forms.id AND fd0.name = 'name'
LEFT JOIN form_data AS fd1 ON fd1.forms_id = forms.id AND fd1.name = 'surname'
LEFT JOIN form_data AS fd2 ON fd2.forms_id = forms.id AND fd2.name = 'address';

Here is how to simulate

Sql schema:

SET NAMES utf8mb4;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

CREATE TABLE `forms` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `insert_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

INSERT INTO `forms` (`id`, `insert_date`) VALUES
(1,	'2017-01-05 08:51:14'),
(2,	'2017-01-05 08:57:40');

CREATE TABLE `form_data` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `forms_id` bigint(20) unsigned NOT NULL,
  `name` varchar(50) COLLATE utf8_czech_ci NOT NULL,
  `value` varchar(255) COLLATE utf8_czech_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `form_id_name_value` (`name`,`value`),
  KEY `response_id` (`forms_id`),
  CONSTRAINT `form_data_ibfk_1` FOREIGN KEY (`forms_id`) REFERENCES `forms` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

INSERT INTO `form_data` (`id`, `forms_id`, `name`, `value`) VALUES
(1,	1,	'name',	'Joe'),
(2,	1,	'surname',	'Doe'),
(3,	1,	'address',	'Prague'),
(4,	2,	'name',	'Mark'),
(5,	2,	'surname',	'Zug'),
(6,	2,	'address',	'London');

Prepare environment

git clone [email protected]:nette/database.git nette-database
cd nette-database
composer install
mkdir temp
touch run.php

Content of run.php and edit connection config.

<?php

require __DIR__ . '/vendor/autoload.php';

$config = [
	'dsn' => 'mysql:host=127.0.0.1;dbname=nette_test',
	'user' => 'root',
	'password' => ''
];

$connection = new Nette\Database\Connection($config['dsn'], $config['user'], $config['password']);
$storage = new Nette\Caching\Storages\FileStorage(__DIR__ . '/temp');
$structure = new \Nette\Database\Structure($connection, $storage);
$context = new Nette\Database\Context($connection, $structure);

function createSql(\Nette\Database\Context $context)
{
	$sql = $context->table('forms')
		->select('forms.insert_date');

	$aliases = [];
	foreach (['name', 'surname', 'address'] as $key => $column) {
		$alias = 'fd' . $key;
		$columnAlias = $column;
		$aliases[$columnAlias] = $alias . '.value';
		$sql->select($aliases[$columnAlias] . ' AS `' . $columnAlias . '`')
			->alias(':form_data', $alias)
			->joinWhere($alias, $alias . '.name = ?', $column);
	}
	return $sql;
}

dump(createSql($context)->fetch());

Run script.

php run.php

If i use aliasses the SqlBuilder forget to look at property $this->aliases.

@h4kuna
Copy link
Contributor Author

h4kuna commented Jan 5, 2017

Is duplicity? with #141

@h4kuna h4kuna closed this Jan 5, 2017
@h4kuna h4kuna reopened this Jan 5, 2017
@h4kuna h4kuna changed the title SqlBuilder lose parameters SqlBuilder: lose parameters for aliasses Jan 5, 2017
@dg dg merged commit 19060ea into nette:master Jan 9, 2017
@dg
Copy link
Member

dg commented Jan 9, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants