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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器