PHP ActiveRecord - Assosiation不起作用

时间:2014-10-02 08:54:00

标签: php mysql orm phpactiverecord

我刚刚开始使用PHP ActiveRecord类并且安静得很好但是现在我试图将用户与一个组以及一个拥有许多用户的组关联起来。[/ p>

这就是我所拥有的:

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;

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用户:

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

现在如果我搜索一个用户没有附加任何组...有人知道如何解决这个问题或者能告诉我我做错了什么吗?我无法找到我的错误......:)

var_dump输出:

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

非常感谢你看看它:)

1 个答案:

答案 0 :(得分:1)

制定了解决方法:

<?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)));
        }
    }
}

这就是你如何使用它......     

    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')
    );
}
?>