View_CRUD::setModel PHP Method

setModel() public method

public setModel ( string | Model $model, array $fields = null, array $grid_fields = null ) : AbstractModel
$model string | Model Same as parent
$fields array Specify list of fields for form and grid
$grid_fields array Overide list of fields for the grid
return AbstractModel $model
    public function setModel($model, $fields = null, $grid_fields = null)
    {
        $model = parent::setModel($model);
        if ($this->entity_name === null) {
            if ($model->caption === null) {
                // Calculates entity name
                $class = get_class($this->model);
                $class = substr(strrchr($class, '\\') ?: ' ' . $class, 1);
                // strip namespace
                $this->entity_name = str_replace(array('Model_', '_'), array('', ' '), $class);
            } else {
                $this->entity_name = $model->caption;
            }
        }
        if (!$this->isEditing()) {
            $this->configureGrid(is_null($grid_fields) ? $fields : $grid_fields);
        }
        if ($this->allow_add) {
            if ($this->configureAdd($fields)) {
                return $model;
            }
        } elseif (isset($this->add_button)) {
            $this->add_button->destroy();
        }
        if ($this->allow_edit) {
            if ($this->configureEdit($fields)) {
                return $model;
            }
        }
        if ($this->allow_del) {
            $this->configureDel();
        }
        return $model;
    }

Usage Example

示例#1
0
 function setModel($m, $f = null, $f2 = null)
 {
     parent::setModel($m, $f, $f2);
     if ($this->add_button) {
         $this->add_button->setLabel('Add ' . str_replace(array('Model_', '_'), array('', ' '), get_class($this->model)));
     }
 }