<?php defined('SYSPATH') or die('No direct script access.'); //внутряняя кухня коханы отвечающая за безопасность
Class Model_Newcustomer extends Model {
protected $_tableCustomer = 'customer';
//добавление нового заказчика
public function get_customer($customer_id)
{
$query = DB::select('fio', 'adres', 'tels', 'info')
->from($this->_tableCustomer)
->where('customer_id', '=', $customer_id)
->execute()
->as_array();
if($query)
return $query;
else
return array();
}
/**
* Create new customer
*/
public function create_customer($customer_id, $fio, $adres, $tels, $info)
{
DB::insert($this->_tableCustomer, array('customer_id', 'fio', 'adres', 'tels', 'info'))
->values(array($customer_id, $fio, $adres, $tels, $info))
->execute();
}
public static function unique_fio($fio)
{
// Проверка, существет ли уже в базе данных
// указанное имnbsp;
return ! DB::select(array(DB::expr('COUNT(fio)'), 'total'))
->from('customer')
->where('fio', '=', $fio)
->execute()
->get('total');
}
public function rules()
{
return array(
'username' => array(
array('not_empty'),
array('Model_Newcustomer::unique_fio'),
),
);
}
}
<?php defined('SYSPATH') or die('No direct script access.'); //внутряняя кухня коханы отвечающая за безопасность
class Controller_Newcustomer extends Controller_Template {
public $template = 'basecus'; //загрузка базового вида
public function action_index()
{
/* if($this->request->is_initial())
Request::initial()->redirect(URL::site('/newcustomer'));*/
$customer_id = $this->request->param('id');
$content = View::factory('newcustomerview')
->bind('customers', $customers);
if($_POST)
{
$_POST = Arr::map('trim', $_POST);
$post = Validation::factory($_POST);
$post -> rule('fio', 'not_empty')
-> rule('fio', 'Model_Newcustomer::unique_fio');
if($post -> check())
{
Model::factory('Newcustomer')->create_customer($customer_id, $_POST['fio'], $_POST['adres'], $_POST['tels'], $_POST['info']);
/* $uri = Request::detect_uri('/newcustomer');
Request::initial()->redirect($uri); */
}
else
{
$errors = $post -> errors('customer');
}
die(Debug::vars($errors));exit;
}
$customers = Model::factory('Newcustomer')->get_customer($customer_id);
$this->template->content = $content;
}
}