Jezek Jezek Thu Jan 27 22:43:05 -0500 2011

Subject: Basic Has Many Though

So I'd like it so each employee can belong to multiple companies.. so.. these are my models

Company.php http://pastebin.com/QtnVnfpW
CompanyEmployee.php http://pastebin.com/VQRXF4Mb
Employee.php http://pastebin.com/ZbKvLN9D

But with a basic call to $company->employees gives error "Fatal error: Uncaught exception 'ActiveRecord\DatabaseException' with message '42S22, 1054, Unknown column 'employee_id' in 'where clause..."


forrest lyman Fri Jan 28 02:02:15 -0500 2011

The naming convention is a little tricky. I will elaborate once i figure it out better, but here is an example that works for me:

class Institution extends ActiveRecord\Model {
    static $has_many = array(
        array('divisions'),
        array('departments'),
        array('institutions_to_topics'),
        array('topics', 'through' => 'institutions_to_topics')
    );
}

class InstitutionToTopic extends ActiveRecord\Model {
    static $table_name = 'institutions_to_topics';
    static $belongs_to = array(array('institution'), array('topic'));
}

class Topic extends ActiveRecord\Model{

    static $has_many = array(
        array('pages','order' => 'position asc'),
        array('questions'),
        array('departments_to_topics'),
        array('institutions_to_topics')
    );
}

$inst = Institution::first();
// all topics related to the institution
$topics = $inst->topics;

(1-1/1)