0

私は Agile Toolkit を使用しています

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

1 人のユーザーのすべての製品をユーザー ページに一覧表示したい

$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 で、refModel ではなく hasOne 参照を作成します (非推奨です)。

// 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 は OK です。それに関するいくつかの補足事項:

  • デフォルトで 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 に答える