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 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM