Baptiste ROUSSEL Sun May 22 15:02:42 -0400 2011

Subject: [Getting Started] No error, but no set

Hello,
i actually have problem with retrieving data from the database...
I don't have any error, but i have an empty set with find('all') method.

I work on PHP 5.3.0, MySQL 5.0.51b.
My configuration :
ActiveRecord\Config::initialize(function($cfg) {
// Récupération des connexions dans la configuration
$connections = AppCore::getConfig("data_orm","connexions");
$cfg->set_model_directory('model');
$cfg->set_connections($connections);
// Définition de la connexion à utiliser
$cfg->set_default_connection('0');
});

My visiteur model :
class Visiteur extends ActiveRecord\Model{
static $primary_key = 'visiteurId';
/** * Constructor
*/
function _construct(){
parent::
_construct();
}
}

Then my code :
$visiteurs = Visiteur::find('all');
var_dump($visiteurs);
foreach($visiteurs as $v)
var_dump($v->visiteurid);

And the output :
array(1) { [0]=> object(Visiteur)#18 (6) { ["errors"]=> NULL ["attributes":"ActiveRecord\Model":private]=> array(3) { ["visiteurid"]=> NULL ["visiteurip"]=> NULL ["visitedate"]=> NULL } ["__dirty":"ActiveRecord\Model":private]=> NULL ["__readonly":"ActiveRecord\Model":private]=> bool(false) ["__relationships":"ActiveRecord\Model":private]=> array(0) { } ["__new_record":"ActiveRecord\Model":private]=> bool(true) } } NULL

I don't understand what i do wrong... I have records in my table visiteurs. :/


Yoan B Mon May 23 01:58:49 -0400 2011

Try to check the generated SQL and call it from MySQL directly to compare.

1 $visiteurs = Visiteur::find('all');
2 echo Visiteur::table()->last_sql;
Baptiste ROUSSEL Mon May 23 14:36:36 -0400 2011

I try this code :

1 $visiteurs = Visiteur::find('all');
2 echo Visiteur::table()->last_sql;
3  

But the generated SQL is fine and works in MySQL console :

SELECT * FROM `visiteurs`

Could it be a specific connexion error ? I have all PHP error activated, and a try..catch(Exception) on my code but nothing.
Is there a special way to active ActiveRecord error ?

I tried to set wrong datas for the connexion (wrong database or server) and i successfully catch a PDOException...

PS: i use the stable version (1.0) of ActiveRecord, should i try with the latest one ?

Yoan B Mon May 23 16:20:18 -0400 2011

Give the master version a try yes.

BTW this looks strange to me:

1 $cfg->set_default_connection('0');

It might be linked to the way you store your configuration.

Baptiste ROUSSEL Tue May 24 13:20:39 -0400 2011

Yoan Blanc wrote:

BTW this looks strange to me:

1 > $cfg->set_default_connection('0');
2 > 

If i don't set the default connexion i have a fatal error : Empty connection string

Here a try with the two builds in one test file (without my framework, it's a stand-alone file test).

 1 require_once("lib/php-activerecord-20110425/ActiveRecord.php");
 2 ActiveRecord\Config::initialize(function($cfg)
 3 {
 4     $cfg->set_model_directory('model');
 5     $cfg->set_connections(array("mysql://root:@127.0.0.1:3306/xtg_blog"));
 6     // Définition de la connexion à utiliser
 7     $cfg->set_default_connection("0");
 8 });
 9 $visiteurs = new Visiteur();
10 var_dump($visiteurs->table()->last_sql);
11 $result = $visiteurs->find('all');
12 var_dump($visiteurs->table()->last_sql);
13 foreach($result as $v)
14     var_dump($v->visiteurid);
15 </div>

Here the result :
NULL 
string(25) "SELECT * FROM `visiteurs`" 
NULL 

The same result with Stable and Nightly Build.

I have some more informations. If i try to create a new record like this :

<div class="php">$insert_test = new Visiteur();
$insert_test->visiteurip = "127.0.0.1";
$insert_test->visitedate = date("Y-m-d H:i:s");
$insert_test->save();</div>

I have the following Error :
Notice: Trying to get property of non-object in C:\wamp\www\blog2\lib\php-activerecord-20110425\lib\Model.php on line 823
string(62) "INSERT INTO `visiteurs`(`visiteurip`,`visitedate`) VALUES(?,?)" 

Yoan B Tue May 24 14:39:41 -0400 2011

Can you output this?

1 print_r(Visiteur::table()->columns());

BTW: why not using the standard way of setting connections?? http://www.phpactiverecord.org/projects/main/wiki/Configuration__Setup#default-connections

Baptiste ROUSSEL Fri May 27 09:10:19 -0400 2011

The function doesn't exist, it is a property and seems ok :

Array
(
    [visiteurId] => ActiveRecord\Column Object
        (
            [name] => visiteurId
            [inflected_name] => visiteurid
            [type] => 2
            [raw_type] => int
            [length] => 5
            [nullable] => 
            [pk] => 1
            [default] => 
            [auto_increment] => 
            [sequence] => 
        )

    [visiteurIp] => ActiveRecord\Column Object
        (
            [name] => visiteurIp
            [inflected_name] => visiteurip
            [type] => 1
            [raw_type] => varchar
            [length] => 15
            [nullable] => 
            [pk] => 
            [default] => 
            [auto_increment] => 
            [sequence] => 
        )

    [visiteDate] => ActiveRecord\Column Object
        (
            [name] => visiteDate
            [inflected_name] => visitedate
            [type] => 4
            [raw_type] => datetime
            [length] => 19
            [nullable] => 
            [pk] => 
            [default] => 
            [auto_increment] => 
            [sequence] => 
        )

)

Btw i tried with a copy/paste from your link and just change the dsn. Nothing more. (global variables are a little ugly btw ;o )

One more information ! When i do a save it write in the table.
But i have the notice from my last message. (i did not see that the tuple was writed.)

Do you have any other idea ? :(

Baptiste ROUSSEL Fri May 27 09:58:29 -0400 2011

I tested a lot of stuff. And i successfully find what is wrong !
It came from the model class, if i delete my constructor the script work and don't write null.

class Visiteur extends ActiveRecord\Model{
    static $primary_key = 'visiteurId';
    /**
     * Constructor
     */

    /*public function __construct(){
        parent::__construct();
    }*/
}

Yoan B Sat May 28 08:05:52 -0400 2011

When you override a method you should respect its signature, PHP should through a notice of warning otherwise. http://www.phpactiverecord.org/docs/ActiveRecord/Model#method__construct

ActiveRecord\Config is a Singleton which is a global state: http://www.phpactiverecord.org/docs/ActiveRecord/Config and $connections doesn't have to be global.

Try to run the tests on your machine and spot the failing ones.

(1-8/8)