Sebastian Frohm
Sebastian Frohm

Reputation: 417

Models and Pivot Tables

I have a custom written solution, which I'm porting over to Phalcon. This is my first run with Phalcon, and, though it's easy to use and well documented, I can't seem to find anything about linking models that are linked through a pivot table.

Here's the run down:

I have users. Users have stats. The two are linked with a table called users_stats. The table contains two columns: a user ID and a stat ID. I created a third class called Users_Stats to model after the pivot table.

User Model:

<?php
class Users extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->hasMany('stat_id', 'Users_Stats', 'user_id', array('foreignKey' => true));
    }
}

Stats Model:

<?php
class Stats extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->belongsTo('stat_id', 'Users_Stats', 'id');
    }
}

Users_Stats Model:

<?php
class Users_Stats extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->hasMany('user_id', 'Users', 'id');
        $this->hasMany('stat_id', 'Stats', 'id');
    }
}

I just want to be able to gather the stats based on users. I am not sure if all I'm missing is that each of the models needs to belong and hasMany (Stats has many User_Stats && User_Stats has many Stat; etc.). I know I'm missing something though.

Any help would be appreciated! Thanks!

Upvotes: 2

Views: 1882

Answers (2)

Nikolaos Dimopoulos
Nikolaos Dimopoulos

Reputation: 11485

Try this (source):

User Model:

<?php
class Users extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->hasMany(
            'stat_id', 
            'Users_Stats', 
            'user_id', 
            array('foreignKey' => true)
        );
    }
}

Stats Model:

<?php
class Stats extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->hasMany('stat_id', 'Users_Stats', 'id');
    }
}

Users_Stats Model:

<?php
class Users_Stats extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->belongsTo('user_id', 'Users', 'id');
        $this->belongsTo('stat_id', 'Stats', 'id');
    }
}

User 1->many Users_Stats many<-1 Stats

Upvotes: 3

Sebastian Frohm
Sebastian Frohm

Reputation: 417

After some playing around, and some great help from Nikolaos Dimopoulos, here's something things I found out.

  1. Classes can't have underscores.
  2. A lookup table's model must belong to each of the classes, as well as must have many of those same classes.
  3. Each class belonging to a lookup table's model must belong to the lookup table's model, as well as must have many of the lookup table's model.

User Model:

<?php
class Users extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->belongsTo('id', 'UserStats', 'user_id');

        $this->hasMany('user_id', 'UserStats', 'id');
    }
}

Stat Model:

<?php
class Stats extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->hasMany('stat_id', 'UserStats', 'id');

        $this->belongsTo('stat_id', 'UserStats', 'id');
    }
}

UserStats Model:

<?php
class UserStats extends \Phalcon\Mvc\Model {
    /**
     * Set database name as there is no UserStats DB
     *
     * @return string
     */
        public function getSource() {
        return 'user_stats';
    }

    public function initialize() {
        $this->belongsTo('user_id', 'Users', 'id');
        $this->belongsTo('stat_id', 'Stats', 'id');

        $this->hasMany('id', 'Users', 'user_id');
        $this->hasMany('id', 'Stats', 'stat_id');
    }
}

Upvotes: 3

Related Questions