2016-07-12 42 views
2

這是我的函數登錄我調用存儲過程的匹配用戶名和密碼登錄,但它不爲我工作,它給我的所有行:如何在cakephp 3中執行存儲過程?

public function login($email,$password) 
    { 
    $consumers = TableRegistry::get('Consumers'); 
    $result=$consumers->query("Call login('".$email."','".$password."')"); 
    pr($result->toArray());die; 
    } 

我的存儲過程在phpMyAdmin是如下:

BEGIN 
    SELECT * FROM consumers WHERE email = email_id AND password =  md_password; 
END 

,當我執行查詢它給我反對,但這對象轉換爲數組後,給我table.output的所有行:

<pre class="pr">Array 
(
    [0] => Cake\ORM\Entity Object 
     (
      [_properties:protected] => Array 
       (
        [id] => 1 
        [name] => jeevan 
        [email] => [email protected] 
        [password] => asdf 
        [phone_no] => 8447726137 
        [ota] => cde 
        [status] => 0 
        [created_on] => Cake\I18n\FrozenTime Object 
         (
          [date] => 2016-07-08 17:28:52 
          [timezone_type] => 3 
          [timezone] => UTC 
         ) 

        [token_access] => 
        [device_type] => 1 
        [push_id] => abc 
        [want_news] => 1 
        [postal_code] => 263136 
        [registration_type] => 1 
       ) 

      [_original:protected] => Array 
       (
       ) 

      [_hidden:protected] => Array 
       (
       ) 

      [_virtual:protected] => Array 
       (
       ) 

      [_className:protected] => 
      [_dirty:protected] => Array 
       (
       ) 

      [_new:protected] => 
      [_errors:protected] => Array 
       (
       ) 

      [_invalid:protected] => Array 
       (
       ) 

      [_accessible:protected] => Array 
       (
        [*] => 1 
       ) 

      [_registryAlias:protected] => Consumers 
     ) 

    [1] => Cake\ORM\Entity Object 
     (
      [_properties:protected] => Array 
       (
        [id] => 2 
        [name] => jack 
        [email] => [email protected] 
        [password] => 123 
        [phone_no] => 7409757656 
        [ota] => chb 
        [status] => 1 
        [created_on] => Cake\I18n\FrozenTime Object 
         (
          [date] => 2016-07-20 06:10:14 
          [timezone_type] => 3 
          [timezone] => UTC 
         ) 

        [token_access] => ghcvhgv 
        [device_type] => 0 
        [push_id] => hgnjh 
        [want_news] => 1 
        [postal_code] => 263136 
        [registration_type] => 1 
       ) 

      [_original:protected] => Array 
       (
       ) 

      [_hidden:protected] => Array 
       (
       ) 

      [_virtual:protected] => Array 
       (
       ) 

      [_className:protected] => 
      [_dirty:protected] => Array 
       (
       ) 

      [_new:protected] => 
      [_errors:protected] => Array 
       (
       ) 

      [_invalid:protected] => Array 
       (
       ) 

      [_accessible:protected] => Array 
       (
        [*] => 1 
       ) 

      [_registryAlias:protected] => Consumers 
     ) 

)</pre> 

這意味着存儲過程不工作,任何想法真的幫助我,在此先感謝!

回答

1

您可以使用ConnectionManager的​​方法調用存儲過程。重要的是要記住,讓你刪除SQL注入的風險準備查詢是很重要的: -

$this->connection = ConnectionManager::get('default'); 
$results = $this->connection->execute(
    'CALL login(?, ?)', 
    [$email, md5($password)] 
)->fetchAll('assoc'); 

當CakePHP的運行​​它將替代?$emailmd5($password)逃脫並引述值。詳情請查閱official docs on Preparing a statement

0

第1步:使用類爲使用Cake \ Datasource \ ConnectionManager;
步驟2:然後執行程序 -

$this->connection = ConnectionManager::get('default'); 
     $consumer = $this->connection->execute("CALL login('".$email."','".md5($password)."')")->fetchAll('assoc'); 

現在它會給陣列。 終於我在cakephp食譜中得到了這個答案。

+1

雖然這會起作用,但您現在擁有的是一個可能的SQL注入漏洞!使用綁定/準備語句,或通過驅動程序'quote()'方法手動引用輸入! ** http://book.cakephp.org/3.0/en/orm/database-basics.html#interacting-with-statements** | ** HTTP://api.cakephp.org/3.2/class-Cake.Database.Connection.html#_quote** – ndm