0

我正在使用敏捷工具包

我有一个 Model_Product

class Model_Product extends Model_Table {
public $table="product";
function init(){
    parent::init();

    $this->addField('name')->mandatory(true);
    $this->addField('price')->mandatory(true)->type('money');
    $this->addField('user_id')->refModel('Model_User')
        ->defaultValue($this->api->auth->get('id'));    
    //$this->hasOne('User',null,'email'); => send me an error message
}
}

和 Model_User

class Model_User extends Model_Table {
public $table="user";

function init(){
    parent::init();

    $this->addField('first_name')->mandatory('Prénom nécesssaire');
    $this->addField('last_name')->mandatory('Nom nécesssaire');
    $this->addField('email')->mandatory('entrez un email valide');
    $this->addField('nationality')->mandatory('nécessaire')->enum(array('FR','EN','US'));
    $this->addField('birthday')->defaultValue(date('Y-m-d'))->type('date');
    $this->addField('is_admin')->type('boolean');       
    $this->hasMany('Product','user_id');
}

我想在用户页面上列出一个用户的所有产品

$q=$this->api->db->dsql();
$q->table('product')->where('product.user_id',$this->api->auth->model['id']);
$tab->add('GRID')->setModel($q);

在某种程度上,我弄错了,因为无论我如何尝试过滤我的模型都会出错。

4

1 回答 1

0

如果你没有使用来自 Github 的最新 ATK4 版本,那么你应该抓住它并保持最新。


你应该这样做:

1)在 Model_Product 创建 hasOne 引用而不是 refModel (已弃用)。

// adding 'user_id' parameter is not needed, it'll be calculated anyway
// but many developers add it anyway to clear thing up a bit.
$this->hasOne('User','user_id')->defaultValue($this->api->auth->get('id'));

2) Model_User 没问题。只是一些关于它的旁注:

  • 我认为您不应该默认设置birthday = today()。令人难以置信的是,这个世界上第一天的孩子会使用电脑:)
  • is_admin 应该是强制性的 + defaultValue(false) - 默认用户不是管理员。

3)如何列出当前用户的所有产品。

// model of current user
$model = $this->add('Model_User')
              ->load($this->api->auth->get('id'));
// add grid
$page->add('Grid')
     // reference Product model with condition already set
     ->setModel($model->ref('Product'));

就是这样。


也许更好更安全的方法是为登录用户定义新的模型类:

class Model_Myself extends Model_User {
    function init(){
        parent::init();
        $this->addCondition('id', $this->api->auth->get('id'));
        $this->loadAny(); // I'm not sure do we have to explicitly load it here
    }
}

然后像这样创建网格

// model of products of current user
$prod_model = $this->add('Model_Myself')->ref('Product');
// add grid
$page->add('Grid')->setModel($prod_model);
于 2013-05-17T22:09:05.930 回答