taylor scott Sat Nov 19 15:57:51 -0500 2011

Subject: has_may association not working but belongs to is.

class Quiz extends ActiveRecord\Model {
static $table_name = "quizzes";
static $belongs_to = array(
array('user')
);
}

class User extends ActiveRecord\Model {
static $table_name = "users";
static $has_many = array(
array("quizzes")
);
}
--------------------------------------
my tables look like this
---------------------------------------
CREATE TABLE `database`.`quizzes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(45) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_fk` (`user_id`),
CONSTRAINT `user_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1

CREATE TABLE `database`.`users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`admin` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1

$quiz = Quiz::first();
echo $quiz->user->name;
-------------------------------------
the above work but:
---------------------------------------------------------------------
$user = User::first();
echo $user->quizzes->title
-------------------------------------------------------------------------------
this isn't working and i'm not sure why, I've made sure that the first user has an existing row its associated with so i know for sure thats not the problem. any suggestions would be great. thankyou.


taylor scott Sun Nov 20 12:22:34 -0500 2011

I am a fool, I solved this. I forgot that
$user->quizzes
is returning an array of objects so i would change it to something like
$user = User::first();
echo $user->quizzes[0]->title

(1-1/1)