drtj40036 2014-10-02 08:54
浏览 27
已采纳

PHP ActiveRecord - Assosiation不起作用

I just started to use the PHP ActiveRecord class and get along with it quiet well but now I'm stuck by trying to associate a user with a group, and a group with many users.

This is what I've got:

The SQL-Part:

CREATE TABLE `users` (
      `id`          INT(255)        NOT NULL AUTO_INCREMENT,
      `group_id`    INT(255)        NOT NULL ,
      `name`        VARCHAR(150)    NOT NULL ,
      `email`       VARCHAR (150)   NOT NULL ,
      `passwd`      VARCHAR (512)   NOT NULL ,
      `salt`        VARCHAR (16)   NOT NULL ,
      PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `groups` (
      `id`          INT(255)        NOT NULL AUTO_INCREMENT,
      `name`        VARCHAR(150)    NOT NULL ,
      PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

The PHP-Class:

#Class User.php
class User extends ActiveRecord\Model{

    static $belongs_to = array(
        array('group')
    );

    static $validates_presence_of = array(
        array('name'),
        array('email'),
        array('passwd'),
        array('salt')
    );

    static $validates_uniqueness_of = array(
        array('name'),
        array('email')
    );
}

#Class Group.php
class Group extends ActiveRecord\Model{

    static $has_many = array(
        array('users')
    );

    static $validates_presence_of = array(
        array('name')
    );

    static $validates_uniqueness_of = array(
        array('name')
    );
}

VAR DUMP User:

<?php
    var_dump(User::find(1));
?>

Now if I search for an user no group is being attached... Does someone know how to solve this or can tell me what I'm doin wrong? I just can't find my mistake... :)

var_dump output:

object(User)[32]
  public 'errors' => null
  private 'attributes' (ActiveRecord\Model) => 
    array (size=6)
      'id' => int 1
      'group_id' => int 1
      'name' => string 'admin' (length=5)
      'email' => string 'user@email.com' (length=17)
      'passwd' => string 'f22b07f3b0f2d93a696336f040a6d08b8c36fe53fc5f080b5a3ad1db3387b4553f16ed2c7c4196900f3ff9b8aa516b115e8250be6c0a60f6e22cf768fe43c291' (length=128)
      'salt' => string '6976e5b5410415bd' (length=16)
  private '__dirty' (ActiveRecord\Model) => 
    array (size=0)
      empty
  private '__readonly' (ActiveRecord\Model) => boolean false
  private '__relationships' (ActiveRecord\Model) => 
    array (size=0)
      empty
  private '__new_record' (ActiveRecord\Model) => boolean false

Thanks anyway for having a look at it :)

  • 写回答

1条回答 默认 最新

  • douzong2206 2014-10-13 07:09
    关注

    Made a workaround:

    <?php
    class Model extends ActiveRecord\Model{
    
        public $Extensions = array();
        protected $relations = array('has_one' => array(), 'belongs_to' => array());
    
        static $after_construct = array('set_relations');
    
        public function set_relations(){
            foreach($this->relations['has_one'] as $relation){
                $upper =  ucfirst($relation);
                $lower = strtolower($relation);
                $id_table = $lower.'_id';
    
                $this->Extensions[$upper] = $upper::find($this->$id_table);
            }
    
            foreach($this->relations['belongs_to'] as $relation){
                $model =  ucfirst($relation[0]);
                $id_table = $relation[1];
    
                $this->Extensions[$model] = $model::find(array('conditions' => array($id_table.' = ?', $this->id)));
            }
        }
    }
    

    And this is how you use it...

        protected $relations = array('has_one' => array('group'), 'belongs_to' => array(array('publisher', 'user_id')));
    
        static $validates_presence_of = array(
            array('name'),
            array('email'),
            array('passwd'),
            array('salt'),
            array('group_id')
        );
    
        static $validates_uniqueness_of = array(
            array('name'),
            array('email')
        );
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动