alexandr m Tue Jan 24 12:09:35 -0500 2012

Subject: Relation doesn't work..

Hello!
I have a trouble with relation, I read all i found here but it didn't help me...
These ara my classes:

class GamesCategoryType extends CommonObject {
public static $table_name = 'mod_flash_games_categories_types';
...
}

class GamesCategory extends CommonObject {
public static $table_name = 'mod_flash_games_categories';

static $belongs_to = array(
array('cat_type', 'class_name' => 'GamesCategoryType', 'foreign_key' => 'type')
);
....
}

class CommonObject extends ActiveRecord\Model {
...
}

Tables:
mod_flash_games_categories_types:
- id
- name
mod_flash_games_categories:
- id
- name
- type
...

I try, for example:
$categories = GamesCategory::find('all');
echo $categories\[0\]->cat_type->name;

It returns NULL... var_dump of $categories also doesn't show any cat_type... What did I do wrong?
Thanks!


alexandr m Thu Jan 26 02:05:05 -0500 2012

Can anybody help me?:)
I have a project and I stopped working on it because of this issue..

Mr. Carl Thu Jan 26 14:56:03 -0500 2012

Please format the code:

<pre class="code"><code class="php">
...code
</code></pre>

Formatted code:

 1 
 2 class CommonObject extends ActiveRecord\Model 
 3 {
 4     //...
 5 }
 6 
 7 class GamesCategoryType extends CommonObject 
 8 {
 9     public static $table_name = 'mod_flash_games_categories_types';
10     //...
11 }
12 
13 class GamesCategory extends CommonObject 
14 {
15     public static $table_name = 'mod_flash_games_categories';
16     static $belongs_to = array
17     (
18         array('cat_type', 'class_name' => 'GamesCategoryType', 'foreign_key' => 'type')
19     );
20 
21     // ....
22 }
23 
24 $categories = GamesCategory::find('all');
25 echo $categories[0]->cat_type->name; // It returns NULL.

Database (unknown type and version) and tables relationship are unknown:

 1 
 2 Tables: mod_flash_games_categories_types:
 3     - id            --Type unknown
 4     - name        
 5 
 6 Tables:  mod_flash_games_categories:
 7     - id        --Type unknown        
 8     - name        
 9     - type    --Type unknown

Before we can help you must tell us the following thing
What version of php are you using?
What version of php.activerecored are u using?
What database and version are you using?
Have you tested each model by themselves for creating, save, delete, etc?
When you test each model what is the var_dump/etc on it after load data from the database?
What is more complete table schema?

alexandr m Fri Jan 27 02:18:31 -0500 2012

Thank you for attention!
My code:

 1 class CommonObject extends ActiveRecord\Model
 2 {
 3 protected $images;
 4 protected $files;
 5 //...
 6 }
 7 
 8 class GamesCategoryType extends CommonObject
 9 {
10   public static $table_name = 'mod_flash_games_categories_types';
11 }
12 
13 class GamesCategory extends CommonObject
14 {
15   public static $table_name = 'mod_flash_games_categories';
16 
17   static $belongs_to = array(
18       array('typeObj', 'class_name' => 'GamesCategoryType', 'foreign_key' => 'type')
19     );
20 //..
21 }
22 
23 //This code doesn't work:
24 $categories = GamesCategory::find('all');
25  echo $categories[0]->typeObj->name; // It returns NULL.

I use PHP 5.3 and php.activerecored 1.0
MySQL 5.2.10

Yes, I have tested models and functions find, save, delete etc. work well.

Var dumps of models:

$categories = GamesCategory::find('all');
 var_dump($categories[0]);

object(GamesCategory)#74 (8) { ["images"]=> array(0) { } ["files"]=> array(0) { } ["errors"]=> NULL ["attributes":"ActiveRecord\Model":private]=> array(15) { ["id"]=> int(1) ["name_short"]=> string(22) "Головоломки" ["name_full"]=> string(31) "Игры Головоломки" ["name_of_list"]=> string(44) "Головоломки для девочек" ["type"]=> int(1) ["url"]=> string(16) "igry-golovolomki" ["title"]=> string(0) "" ["meta_description"]=> string(0) "" ["meta_keywords"]=> string(0) "" ["description"]=> string(0) "" ["created"]=> int(1327229205) ["updated"]=> int(1327229205) ["active_from"]=> NULL ["active_to"]=> NULL ["active"]=> int(1) } ["__dirty":"ActiveRecord\Model":private]=> array(0) { } ["__readonly":"ActiveRecord\Model":private]=> bool(false) ["__relationships":"ActiveRecord\Model":private]=> array(0) { } ["__new_record":"ActiveRecord\Model":private]=> bool(false) } 

$types = GamesCategoryType::find('all');
 var_dump($types[0]);

object(GamesCategoryType)#74 (7) { ["images":protected]=> array(0) { } ["errors"]=> NULL ["attributes":"ActiveRecord\Model":private]=> array(2) { ["id"]=> int(1) ["name"]=> string(15) "по жанру" } ["__dirty":"ActiveRecord\Model":private]=> array(0) { } ["__readonly":"ActiveRecord\Model":private]=> bool(false) ["__relationships":"ActiveRecord\Model":private]=> array(0) { } ["__new_record":"ActiveRecord\Model":private]=> bool(false) } 

1 

My DB:

 1 --
 2 --  `mod_flash_games_categories`
 3 --
 4 
 5 CREATE TABLE IF NOT EXISTS `mod_flash_games_categories` (
 6   `id` int(11) NOT NULL AUTO_INCREMENT,
 7   `name_short` varchar(256) NOT NULL,
 8   `name_full` varchar(256) NOT NULL,
 9   `name_of_list` varchar(256) NOT NULL,
10   `type` int(11) NOT NULL,
11   `url` varchar(256) NOT NULL,
12   `title` varchar(256) NOT NULL,
13   `meta_description` varchar(256) NOT NULL,
14   `meta_keywords` varchar(256) NOT NULL,
15   `description` text NOT NULL,
16   `created` int(11) NOT NULL,
17   `updated` int(11) NOT NULL,
18   `active_from` int(11) DEFAULT NULL,
19   `active_to` int(11) DEFAULT NULL,
20   `active` tinyint(1) NOT NULL,
21   PRIMARY KEY (`id`)
22 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=34 ;
23 
24 -- --------------------------------------------------------
25 
26 --
27 --  `mod_flash_games_categories_types`
28 --
29 
30 CREATE TABLE IF NOT EXISTS `mod_flash_games_categories_types` (
31   `id` int(11) NOT NULL AUTO_INCREMENT,
32   `name` varchar(256) NOT NULL,
33   PRIMARY KEY (`id`)
34 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

Mr. Carl Fri Jan 27 14:35:11 -0500 2012

Change the following in class GamesCategory:

1 static $belongs_to = array
2 (
3        array('i_hate_cap_letters', 
4               'class_name' => 'GamesCategoryType', 'foreign_key' => 'type')
5 );

I'm dieing lol after wasting a hour on this. Also see Date object vs string depending on capitalizaion? which seem same type of problem for different issues.

A side note you should place foreign_key on the table(s) themselves too. However note that if the table has more then one foreign_key you still need to add the foreign_key to the associations array setup

alexandr m Fri Jan 27 16:42:48 -0500 2012

It works now! Thank you!

(1-5/5)