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

limit is not works in the first time #176

Closed
yftzeng opened this issue Jan 23, 2014 · 8 comments
Closed

limit is not works in the first time #176

yftzeng opened this issue Jan 23, 2014 · 8 comments
Assignees
Milestone

Comments

@yftzeng
Copy link

yftzeng commented Jan 23, 2014

Branch: dev-develop

Note: use the same limit code twice.

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

The result:

string(22) "SELECT * FROM position"
int(89)
string(32) "SELECT * FROM `position` LIMIT 1"
int(1)
@treffynnon
Copy link
Collaborator

I cannot replicate this issue. I put together a regression test in commit 3fbafbe which passed Travis-CI tests here: https://travis-ci.org/j4mie/idiorm/builds/17476101

@yftzeng
Copy link
Author

yftzeng commented Jan 24, 2014

Let me replicate the issue.

Database schema

Filename: demo.sql

DROP DATABASE `demo`;
CREATE DATABASE `demo`;

USE `demo`;

DROP TABLE IF EXISTS `position`;
CREATE TABLE `position` (
  `position_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`position_id`)
);

Import to db,

$ mysql -uroot -p < demo.sql

PHP version

$ php -v
PHP 5.5.8 (cli) (built: Jan 11 2014 00:09:38) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies

Test dev-master branch

Fetch idiorm.php from dev-master branch

$ wget https://raw2.github.com/j4mie/idiorm/master/idiorm.php -O dev-master-idiorm.php

PHP demo code

Filename: test.php

include 'dev-master-idiorm.php';

$db_host = 'localhost';
$db_user = 'root';
$db_pass = ''; 
$db_name = 'demo';

ORM::configure(array(
    'connection_string' => "mysql:host=$db_host;dbname=$db_name;unix_socket=/var/run/mysqld/mysqld.sock",
    'username' => $db_user,
    'password' => $db_pass,
    'logging' => true,
));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

The output is correct

$ php test.php
string(32) "SELECT * FROM `position` LIMIT 1"
int(0)
string(32) "SELECT * FROM `position` LIMIT 1"
int(0)

Test dev-develop branch

Fetch idiorm.php from dev-deveop branch

$ wget https://raw2.github.com/j4mie/idiorm/develop/idiorm.php -O dev-develop-idiorm.php

PHP demo code

Filename: test.php

include 'dev-develop-idiorm.php';

$db_host = 'localhost';
$db_user = 'root';
$db_pass = ''; 
$db_name = 'demo';

ORM::configure(array(
    'connection_string' => "mysql:host=$db_host;dbname=$db_name;unix_socket=/var/run/mysqld/mysqld.sock",
    'username' => $db_user,
    'password' => $db_pass,
    'logging' => true,
));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());
var_dump(count($rs));

The output is not correct (no LIMIT in first run)

$ php test.php
string(22) "SELECT * FROM position"
int(0)
string(32) "SELECT * FROM `position` LIMIT 1"
int(0)

@treffynnon
Copy link
Collaborator

Can you run the regression test I put together instead please.

@yftzeng
Copy link
Author

yftzeng commented Jan 25, 2014

@treffynnon I found that was not bug of limit function, but might be configure.

I cannot replicate in your test case, because you have not use configure function, but please try my test case below,

Failed testcase (via configure function)

include 'idiorm.php';

$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'demo';

ORM::configure(array(
    'connection_string' => "mysql:host=$db_host;dbname=$db_name;unix_socket=/var/lib/mysql/mysql.sock",
    'username' => $db_user,
    'password' => $db_pass,
    'logging' => true,
));

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());

Output:

string(22) "SELECT * FROM position"
string(32) "SELECT * FROM `position` LIMIT 1"

The first run is mismatch the second run.

Passed testcase (via 'set_db' function)

include 'idiorm.php';

$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'demo';

ORM::configure('logging', true);

$db = new PDO('mysql:host=localhost;dbname=muzik30;unix_socket=/var/lib/mysql/mysql.sock',$db_user,$db_pass);
ORM::set_db($db);


$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());

$rs = ORM::for_table('position')
    ->limit(1)
    ->find_array();

var_dump(ORM::get_last_query());

Output:

string(32) "SELECT * FROM `position` LIMIT 1"
string(32) "SELECT * FROM `position` LIMIT 1"

@treffynnon
Copy link
Collaborator

Thank you for the update and bug report. When I have access to a MySQL
install I will test this out.

1.5.0 will be delayed until this is resolved.

@treffynnon treffynnon reopened this Jan 29, 2014
@ghost ghost assigned treffynnon Jan 29, 2014
@treffynnon
Copy link
Collaborator

I am struggling to find the time to investigate this issue. Does anyone else have time to put together a PR for this issue?

treffynnon added a commit that referenced this issue May 28, 2014
Correct issue #176: Ensure database setup before building select
@charsleysa
Copy link
Contributor

@treffynnon This issue should be closed as it has been resolved.

@treffynnon
Copy link
Collaborator

@charsleysa thanks!

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

No branches or pull requests

3 participants