Question

I have a field 'slug' in my model and I set this field at $id_field property, but I got a error: 'Application Error: Field with this name is already defined'. I need to show this fild on the page, how can I do that?

model source:

<?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('Опубликовать');
    }
}
Était-ce utile?

La solution

You don't need the prepareData method in your code. Your model should look something like this:

<?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('Опубликовать');
}
}

There are several ways to display all these fields on the page. If you want to input values from user

Way # 1 : Form

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

Way # 2 : Grid

$this->add('Grid')->setModel('Post');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top