Sameer Kanda Wed Mar 20 00:36:26 -0400 2013

Subject: Possible bug with "has_many"??

My code:

class ZP_Package extends ActiveRecord\Model {
static $table_name = 'packages';
static $has_many = array(
array('items_packages', 'class_name'=>'ZP_Item_Package'),
array('items', 'through'=>'items_packages', 'class_name'=>'ZP_Item', 'foreign_key'=>'item_id')

class ZP_Item extends ActiveRecord\Model {
static $table_name = 'items';
static $has_many = array(
array('items_packages', 'class_name'=>'ZP_Item_Package'),
array('packages', 'through'=>'items_packages', 'class_name'=>'ZP_Package', 'foreign_key'=>'package_id')


class ZP_Item_Package extends ActiveRecord\Model {
static $table_name = 'items_packages';
static $belongs_to = array(
array('items', 'class_name'=>'ZP_Item', 'foreign_key'=>'item_id'),
array('packages', 'class_name'=>'ZP_Package', 'foreign_key'=>'package_id')

When I do:

$item = ZP_Item::find(24); //works fine
$item->pakages; //doesn't work!!!!!

Basically, I don't get any errors, but the query is not generated properly (actually I do get a mysql error, but what I mean to say is I don't get any PHP errors). My mysql query that is generated is something like this:

...INNER JOIN items_packages ON( = items_packages.zp_package_id)....

Notice it says "items_packages.zp_package_id" instead of "items_packages.package_id". ActiveRecord simply takes the class name and uses it as a foreign key... even though the foreign key is defined (it seems to ignore it). I'm new to PHPActiveRecord, but I'm a pro PHP programmer. I've digged into the problem, and came to a conclusion that it was a bug in ActiveRecord, where it simply uses the classname instead of the foriegn key that's defined (look at class "HasMany extends AbstractRelationship", method "load", in particular this line "$this->set_keys......", it's in the "php-activerecord/lib/Relationship.php" file).

If this is not a bug, and I'm just doing something stupid, please let me know. I know my class names don't match with the DB table names, I know I should change that, but that's not the point.... I want to know if this is a bug or not? I'm using PHPActiveRecord 1.0 (haven't tested the nightly build).


Sameer Kanda Wed Mar 20 00:56:06 -0400 2013

just an update, i realized im using the nightly build, not the stable version. but still... any help would be appreciated, thanks!

Sven Leuschner Tue Apr 02 04:24:44 -0400 2013

Hello Sameer,

you can specify the name of the foreign key to be used in your relation between those 3 tables, and you can find examples on how to do that here in this forum - check for other posts regarding problems with foreign keys.
Hope this helps you solve your problem(s)

Short Example

class Payment extends ActiveRecord\Model {
   static $belongs_to = array(
      array('person', 'class_name' => 'User', 'foreign_key'=>'user_id')

Sameer Kanda Tue Apr 02 10:42:20 -0400 2013

Yep, I know that, and that's not working (thats exactly what the bug is). Sorry for the code not being formatted so its readable.... I also posted it on github, and that's more readable: Thanks!

Sven Leuschner Thu Apr 04 03:18:48 -0400 2013

just a small hint .. for code use the
< pre >
< / pre > - Tags , just as you would use them in pure html

Sameer Kanda Thu Apr 04 19:10:03 -0400 2013

Yea.. I'm not much of a forum writer. But... it is formatted well here: :) :)