I can't be sure off the top of my head, but you may have a case sensitivity problem.
Does the following work?
1 Transaction::first()->transactiontag
Nope, nothing is returned. If i use Transaction::first()->TransactionTag, then an Undefined property exception is raised.
Doing a little debugging, i found out that only the following SQL query is executed:
SELECT * FROM `Transaction` LIMIT 0,1
OK, the problem lies in the load function of HasMany class. The constructed SQL query contains an invalid column tag_id:
SELECT `Tag`.* FROM `Tag` INNER JOIN `TransactionTag` ON(`Tag`.TagID = `TransactionTag`.tag_id) WHERE `transactionid`=?
It seems that everytime tag_id column is constructed from class name instead from given foreign key. This is done on line 478 in the HasMany class. The line is as follows:
$this->set_keys($this->get_table()->class->getName(), true);
and the set_keys function is like this:
if (!$this->foreign_key || $override)
$this->foreign_key = array($this->keyify($model_class_name));
if (!$this->primary_key || $override)
$this->primary_key = Table::load($model_class_name)->pk;
The $override parameter is true and it overrides the TagID foreign key set manually. Is there any workaround?
First off, please be sure you are using version 1.0 of php-ar (downloadable here). Next, there is a problem with your $primary_key
declaration for TransactionTag class.
class TransactionTag extends ActiveRecord\Model
{
static $table_name = 'TransactionTag';
static $primary_key = array('TransactionID', 'TagID'); <= This is WRONG.
}
PHP-AR does not accept composite primary keys. You can only define a single primary key and that value needs to be a string -- not an array.
Ah, so this is it. Thanks Jacques for investigating.
(1-5/5)
Subject: Many-to-many relationship
Hi,
i'm having a problem with many to many relationship.
This is the DB schema:
http://www.shrani.si/?1m/ZH/1sm4r6fV/dbschema.png
And these are the models:
http://www.shrani.si/f/17/y7/2zLoDwxu/classes.png
The problem is that Transaction::first() doesn't return any tags. I've tried all sorts of combinations in the $has_many property of the Transaction model, but nothing seems to work. What am I missing here?
Thanks for help and keep up the good work ;)