function action_add() - работа над данными должна выполняться в моделях,а не контроллерах.
а как сделать в моделях можете показать пример?
в случи ORM как же сделать в model ях?
public function action_add() {
//Получение категорий $categories = ORM::factory('category'); $categories = $categories->fulltree()->as_array();
$manufactures = ORM::factory('manufactures')->find_all(); if (isset($_POST['submit'])) { // Работа с товаром $data = Arr::extract($_POST, array('cat_id', 'manufacture_id', 'title', 'code', 'description', 'price', 'images','meta_description', 'meta_keywords', 'status')); $products = ORM::factory('product'); $products->values($data); try { $products->save(); $products->add('categories', $data['cat_id']);
// Работа с изображениями if (!empty($_FILES['images']['name'][0])) { print_r($_FILES['images']['name'][0]); foreach ($_FILES['images']['tmp_name'] as $image) { $filename = $this->_upload_img($image);
// Запись в БД $im_db = ORM::factory('image'); $im_db->product_id = $products->pk(); $im_db->name = $filename; $im_db->save();
$p_db = ORM::factory('product', $products->pk()); if ($p_db->image_id == 0){ $p_db->image_id = $im_db->pk(); $p_db->save(); } } }
$this->request->redirect('admin/products'); } catch (ORM_Validation_Exception $e) { $errors = $e->errors('validation'); }
} $content = View::factory('admin/addproduct',array('manufactures'=>$manufactures)) ->bind('errors', $errors) ->bind('cats', $categories) ->bind('data', $data);
// Вывод в шаблон $this->template->page_title = 'Товары :: Добавить'; $this->template->block_center = $content; }
Какой часть можно в моделях написать а как сделать в моделях можете показать пример?
class Model_Product extends ORM { protected $_table_name = 'products'; protected $_primary_key = 'id'; protected $_db_group = 'default';
protected $_has_many = array( 'images' => array( 'model' => 'image', 'foreign_key' => 'product_id', ), 'categories' => array( 'model' => 'category', 'foreign_key' => 'product_id', 'through' => 'products_categories', 'far_key' => 'category_id', ),
'orders' => array( 'model' => 'order', 'foreign_key' => 'product_id', 'through' => 'orders_products', 'far_key' => 'orders_id', ), );
protected $_belongs_to = array( 'main_img' => array( 'model' => 'image', 'foreign_key' => 'image_id', ),
'manufactures' => array( 'model' => 'manufactures', 'foreign_key' => 'manufacture_id', ), );
public function rules() { return array( 'title' => array( array('not_empty'), ), 'description' => array( array('not_empty'), ), 'price' => array( // array('not_empty'), array('numeric'), ), ); }
public function labels() { return array( 'title' => 'Наименование', 'description' => 'Описание', 'price' => 'Цена', ); }
public function filters() { return array( TRUE => array( array('trim'), ), 'title' => array( array('strip_tags'), ), 'price' => array( array('strip_tags'), ), ); }
}
|