markdown CRUD
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown CRUD相关的知识,希望对你有一定的参考价值。
# ADMIN - GRID
### Tipos de campos do GRID
#### Com filtro chamando uma função callback
```php
$this->addColumn('doctor', array(
'header' => 'Médico',
'align' => 'right',
'index' => 'doctor_fullname',
'filter_condition_callback' => array($this, '_filterFullName')
));
protected function _filterFullName($collection, $column) {
if (!empty($value)) {
$this->getCollection()->getSelect()->where("doctor_entity.value LIKE ? ", '%' . $value . '%');
}
return $this;
}
```
#### Preços
```php
$this->addColumn('value', array(
'header' => 'Valor do(s) Produto(s)',
'align' =>'right',
'index' => 'value',
'type' => 'price',
'currency_code' => 'BRL',
));
```
#### Opções pré-estabelecidas
```php
$this->addColumn('type', array(
'header' => 'Tipo',
'align' =>'right',
'index' => 'type',
'type' => 'options',
'options' => array(
'medical_approved' => 'Médico Aprovado',
'medical_clinical_credit' => 'Compra pelo Médico',
'patient_restriction_credit' => 'Compra pelo Paciente com Restrição',
'patient_credit' => 'Compra pelo Paciente sem Restrição',
'debit' => 'Débito'
),
'renderer' => 'Empresa_Modulo_Block_Adminhtml_Point_Grid_Renderer_Type',
));
// em Empresa/Modulo/Block/Adminhtml/Point/Grid/Renderer/Type.php
public function render(Varien_Object $row)
{
$value = $row->getData('type');
$formattedValue = '';
if ($value == 'medical_approved')
$formattedValue = 'Médico Aprovado';
else if ($value == 'medical_clinical_credit')
$formattedValue = 'Compra pelo Médico';
else if ($value == 'patient_restriction_credit')
$formattedValue = 'Compra pelo Paciente com Restrição';
else if ($value == 'patient_credit')
$formattedValue = 'Compra pelo Paciente sem Restrição';
else if ($value == 'debit')
$formattedValue = 'Débito';
return $formattedValue;
}
```
#### Data
```php
$this->addColumn('data', array(
'header' => 'Data da Compra',
'align' => 'right',
'index' => 'data',
'type' => 'datetime',
));
```
# CRUD
**Módulo**: mynews
**Tabela**: news
### ADD
```php
$data = array('title'=>'hello there','content'=>'how are you? i am fine over here.','status'=>1);
$model = Mage::getModel('mynews/mynews')->setData($data);
try {
$insertId = $model->save()->getId();
echo "Data successfully inserted. Insert ID: ".$insertId;
} catch (Exception $e){
echo $e->getMessage();
}
```
### READ
```php
$model = Mage::getModel('mynews/mynews');
$collection = $model->getCollection();
foreach($collection as $item){
var_dump($item->getData());
var_dump($item->getTitle());
}
```
### UPDATE
```php
// $id = $this->getRequest()->getParam('id');
$id = 2;
$data = array('title'=>'hello test','content'=>'test how are you?','status'=>0);
$model = Mage::getModel('mynews/mynews')->load($id)->addData($data);
try {
$model->setId($id)->save();
echo "Data updated successfully.";
} catch (Exception $e){
echo $e->getMessage();
}
```
### DELETE
```php
// $id = $this->getRequest()->getParam('id');
$id = 3;
$model = Mage::getModel('mynews/mynews');
try {
$model->setId($id)->delete();
echo "Data deleted successfully.";
} catch (Exception $e){
echo $e->getMessage();
}
```
以上是关于markdown CRUD的主要内容,如果未能解决你的问题,请参考以下文章