🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.php
Article #231088

Re: MVC: zapytanie SQL z kontrolera

#231088
From: jivanmukta
Date: Fri, 24 Jul 2015 13:39
76 lines
5271 bytes
Mam ostatnie pytanie dotyczace MVC. Gdzie mam umieszczać obsługę błędów operacji bazodanowych? Aktualnie mam bibliotekę Common:

class Common {
private $ci;
public function __construct() {
    $this->ci =& get_instance();
}
public function error($msg) {
    $this->ci->mylog->logError($msg);
    show_error($msg);
}
...
}

Błędy obsługuję tak: kontroler zawiera:

public function modify_agency() {
    $agencyId = intval($this->input->post('agency_id'));
    $this->load->model('Agencies_model');
    if (!$this->Agencies_model->modifyAgency($agencyId, 
                                             trim($this->input->post('name')),
                                             trim($this->input->post('email')),
                                             trim($this->input->post('www')),
                                             trim($this->input->post('mobile1')),
                                             trim($this->input->post('mobile2')),
                                             trim($this->input->post('phone1')),
                                             trim($this->input->post('phone2')),
                                             trim($this->input->post('contact_person')),
                                             trim($this->input->post('town')),
                                             trim($this->input->post('remarks')))) {
        $this->common->error(ERRMSG_DATABASE_OPERATION_FAILED);
    }
    redirect('/admin/report/agencies');
}

natomiast model zawiera:

public function modifyAgency($agencyId, $name, $email, $www, $mobile1, $mobile2, $phone1, $phone2, $contactPerson, $town, $remarks) {
    $ok = TRUE;
    if (preg_match('/' . EMAIL_REGEXP . '/i', $email, $matches) > 0) {
        $email = $matches[0];
    } else {
        $ok = FALSE;
    }
    $ok = ($ok && (is_empty($mobile1) || $this->ci->common->isValidMobile($mobile1)));
    $ok = ($ok && (is_empty($mobile2) || $this->ci->common->isValidMobile($mobile2)));
    $ok = ($ok && (is_empty($phone1) || $this->ci->common->isValidPhone($phone1)));
    $ok = ($ok && (is_empty($phone2) || $this->ci->common->isValidPhone($phone2)));
    if (!$ok) {
        $this->ci->common->error(ERRMSG_INVALID_AGENCY_DATA);
    }
    $www = (is_empty($www) ? NULL : $www);
    $mobile1 = (is_empty($mobile1) ? NULL : $mobile1);
    $mobile2 = (is_empty($mobile2) ? NULL : $mobile2);
    $phone1 = (is_empty($phone1) ? NULL : $phone1);
    $phone2 = (is_empty($phone2) ? NULL : $phone2);
    $contactPerson = (is_empty($contactPerson) ? NULL : $contactPerson);
    $town = (is_empty($town) ? NULL : $town);
    $remarks = (is_empty($remarks) ? NULL : $remarks);
    $sql = 'UPDATE agencies SET ' .
           'name = ' . $this->ci->common->convertToDb($name) . ', ' .
           'email = ' . $this->ci->common->convertToDb($email) . ', ' .
           'www = ' . $this->ci->common->convertToDb($www) . ', ' .
           'mobile1 = ' . $this->ci->common->convertToDb($mobile1) . ', ' .
           'mobile2 = ' . $this->ci->common->convertToDb($mobile2) . ', ' . 
           'phone1 = ' . $this->ci->common->convertToDb($phone1) . ', ' .
           'phone2 = ' . $this->ci->common->convertToDb($phone2) . ', ' . 
           'contact_person = ' . $this->ci->common->convertToDb($contactPerson) . ', ' . 
           'town = ' . $this->ci->common->convertToDb($town) . ', ' .
           'remarks = ' . $this->ci->common->convertToDb($remarks) .            
           " WHERE agency_id = $agencyId";
    $this->db->query($sql);
    return $this->db->affected_rows() == 1;
}

Czy tak jest dobrze?
Technical Details
Message-ID: <140d567e-85f8-4073-a5ea-6bcfc4362b09@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newspeer1.nac.net!border2.nntp.dca1.giganews.com!nntp.giganews.com!f3no2258016igg.0!news-out.google.com!4ni81980qgh.1!nntp.google.com!z61no3033113qge.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
References: <40546498-26c6-4e09-a62f-815df4bdc3f8@googlegroups.com> <18ib0oawqi49n$.zkxevcy1a7jc.dlg@40tude.net> <db28497e-bddd-44e5-9575-5cf1a3fe5c41@googlegroups.com> <1mg1pouopguup.jcxkalwncm2x$.dlg@40tude.net> <41f4e3a9-f1eb-4566-b59d-070e8adb52c6@googlegroups.com> <f1w4cvuwawti$.in9jfueie26q$.dlg@40tude.net>

Show Full Headers
Complete Article Headers
X-Received: by 10.107.26.129 with SMTP id a123mr12997836ioa.17.1437745149960;        Fri, 24 Jul 2015 06:39:09 -0700 (PDT)
X-Received: by 10.140.109.132 with SMTP id l4mr285481qgf.15.1437745149928; Fri, 24 Jul 2015 06:39:09 -0700 (PDT)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!agh.edu.pl!news.agh.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newspeer1.nac.net!border2.nntp.dca1.giganews.com!nntp.giganews.com!f3no2258016igg.0!news-out.google.com!4ni81980qgh.1!nntp.google.com!z61no3033113qge.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.php
Date: Fri, 24 Jul 2015 06:39:09 -0700 (PDT)
In-Reply-To: <f1w4cvuwawti$.in9jfueie26q$.dlg@40tude.net>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=83.28.55.158; posting-account=Fd8BNwoAAACl8PCi6-fI0bkHGOgt6LMa
NNTP-Posting-Host: 83.28.55.158
References: <40546498-26c6-4e09-a62f-815df4bdc3f8@googlegroups.com> <18ib0oawqi49n$.zkxevcy1a7jc.dlg@40tude.net> <db28497e-bddd-44e5-9575-5cf1a3fe5c41@googlegroups.com> <1mg1pouopguup.jcxkalwncm2x$.dlg@40tude.net> <41f4e3a9-f1eb-4566-b59d-070e8adb52c6@googlegroups.com> <f1w4cvuwawti$.in9jfueie26q$.dlg@40tude.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <140d567e-85f8-4073-a5ea-6bcfc4362b09@googlegroups.com>
Subject: Re: MVC: zapytanie SQL z kontrolera
From: jivanmukta@poczta.onet.pl
Injection-Date: Fri, 24 Jul 2015 13:39:09 +0000
Lines: 97
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit