2012-08-26 58 views
1

我有以下DB:DataMapper的ORM /笨 - - - 驗證關係


CREATE TABLE users 
(
id BIGINT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY (id) 
); 

CREATE TABLE likes 
(
id BIGINT NOT NULL AUTO_INCREMENT, 
user_id BIGINT, 
idea_id BIGINT, 
PRIMARY KEY (id) 
); 

CREATE TABLE ideas 
(
id BIGINT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY (id) 
); 

用戶模型:

// -------------------------------------------------------------------- 
// Relationships 
// -------------------------------------------------------------------- 

public $has_many = array('like' , 'idea' , 'comment'); 

// -------------------------------------------------------------------- 
// Validation 
// -------------------------------------------------------------------- 

var $validation = array(
    'username' => array(
     'label' => 'Username', 
     'rules' => array('required', 'trim', 'unique', 'min_length' => 3) 
    ), 
    'password' => array(
     'label' => 'Password', 
     'rules' => array('required', 'trim', 'min_length' => 3 ,'encrypt') 
    ) 
); 

想法型號:

public $has_one = array('user'); 

public $has_many = array('like'); 

// -------------------------------------------------------------------- 
// Validation 
// -------------------------------------------------------------------- 

public $validation = array(
    'user' => array(
     'rules' => array('required') 
    ) 
); 

像模型:

// Relationships 
// -------------------------------------------------------------------- 

public $has_one = array('user' , 'idea'); 

// -------------------------------------------------------------------- 
// Validation 
// -------------------------------------------------------------------- 

var $validation = array(
    'user' => array(
     'rules' => array('required', 'unique_pair' => 'idea') 
    ), 
    'idea' => array(
     'rules' => array('required') 
    ) 
); 

像控制器

$idea = new Idea(1); 

    $user = new User(1); 

    $like = new Like(); 

    if(!$like->save(array($idea , $user))) 
    { 
      echo $like->error->all; 
    } 
    else 
    { 
     echo "ok";  
    } 

的問題是,我總是得到確定和喜歡被保存在數據庫中,不沒有得到驗證。

我想要的是讓這兩個領域(USER_ID,idea_id)獨特

回答

0

在模型中添加$model$table性質,這種支持的關係。之後,驗證您的foreign keys。記住模型名稱的單數和表名的複數。

public $model = 'user'; 
public $table = 'users'; 

.... 
public function __construct($id = NULL) { 
    parent::__construct($id); 
}