如何在cakephp 3中执行存储过程?

时间:2016-07-12 07:22:39

标签: sql cakephp-3.0

这是我的登录的功能我调用存储过程登录以获取匹配的用户名和密码,但它对我不起作用它给了我所有的行:

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] => j@gmail.com
                    [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] => jack@gmail.com
                    [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>

这意味着存储过程不起作用,任何想法真的对我有所帮助,在此先感谢!

2 个答案:

答案 0 :(得分:1)

您可以使用ConnectionManager的{​​{1}}方法调用存储过程。记住准备查询以消除SQL注入的风险非常重要: -

execute()

当CakePHP运行$this->connection = ConnectionManager::get('default'); $results = $this->connection->execute( 'CALL login(?, ?)', [$email, md5($password)] )->fetchAll('assoc'); 时,它会将execute()替换为?$email的转义和引用值。有关详细信息,请查看official docs on Preparing a statement

答案 1 :(得分:0)

步骤1:将该类用作使用Cake \ Datasource \ ConnectionManager;
第2步:然后执行程序 -

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

现在它会给出一个数组。 最后我在cakephp食谱中得到了这个答案。