具有此名称的字段已定义错误

时间:2013-11-23 21:17:07

标签: atk4

我的模型中有一个字段'slug',我在$ id_field属性中设置了这个字段,但是我收到一个错误:'应用程序错误:已经定义了具有此名称的字段'。我需要在页面上显示这个fild,我该怎么做?

模型来源:

<?php
class Model_Post extends Model_Table {
    public $table='posts';
    public $entity_code='posts';
    public $id_field='slug';

    public function prepareData($table, $columns = 'id,name')
    {
        $getData = $this->api->db->dsql()->table($table)
            ->field($columns)
            ->fetchAll();
        $return = array();
        $columns = explode(',', $columns);
        foreach ($getData as $data) 
        {
            $return[$data[$columns['0']]] = $data[$columns['1']];
        }

        return $return;
    }

    function init(){
        parent::init();
        //$this->hasOne('User', 'author');

        $category = $this->prepareData('categories', 'id,name');
        $user = $this->prepareData('users', 'id,realName');


        $this->addField('slug')->caption('Ссылка')->mandatory(true);
        $this->addField('author')->setValueList($user)->caption('Автор')->mandatory(true);
        $this->addField('category')->setValueList($category)->caption('Категория')->mandatory(true);
        //$this->addField('previewIMG')->setModel("filestore/Model_Image")->type("file")->caption('Изображение');
        $this->addField('title')->caption('Заголовок')->mandatory(true);
        $this->addField('date')->type('date')->defaultValue(date('Ymd'))->caption('Дата')->mandatory(true);
        $this->addField('body')->type('text')->caption('Текст поста')->mandatory(true);
        $this->addField('published')->type('boolean')->caption('Опубликовать');
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中不需要prepareData方法。你的模型应该是这样的:

<?php
class Model_Post extends Model_Table {
public $table='posts';
public $entity_code='posts';
public $id_field='slug';


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

    $this->addField('slug')->caption('Ссылка')->mandatory(true);
    $this->addField('author')->setValueList($user)->caption('Автор')->mandatory(true);
    $this->addField('category')->setValueList($category)->caption('Категория')->mandatory(true);
    //$this->addField('previewIMG')->setModel("filestore/Model_Image")->type("file")->caption('Изображение');
    $this->addField('title')->caption('Заголовок')->mandatory(true);
    $this->addField('date')->type('date')->defaultValue(date('Ymd'))->caption('Дата')->mandatory(true);
    $this->addField('body')->type('text')->caption('Текст поста')->mandatory(true);
    $this->addField('published')->type('boolean')->caption('Опубликовать');
}
}

有几种方法可以在页面上显示所有这些字段。如果要输入用户的值

Way # 1:表格

$f = $this->add('Form');
$f->setModel('Post');
$f->addSubmit();

Way # 2:网格

$this->add('Grid')->setModel('Post');