如何使用 CakePHP 创建用户定义的 sql 函数?

发布于 2024-09-27 18:51:21 字数 864 浏览 0 评论 0原文

考虑此页面上给出的有关在 SQL 中创建用户定义函数的示例,然后在 WHERE 子句中使用用户定义的函数

mysql> CREATE FUNCTION myFunction(in_rep_id INT)
    ->      RETURNS INT
    ->      READS SQL DATA
    -> BEGIN
    ->      DECLARE customer_count INT;
    ->
    ->      SELECT COUNT(*)
    ->           INTO customer_count
    ->        FROM employee
    ->       WHERE id=in_rep_id;
    ->
    ->      RETURN(customer_count);
    ->
    -> END$$

mysql> SELECT id,myFunction(id)
    ->   FROM employee
    ->  WHERE myFunction(id)>0
    ->  ORDER BY myFunction(id) desc;

我如何使用 CakePHP 复制这个?我是否需要将该函数定义为 CakePHP 模型的一部分?或者有没有办法从 CakePHP 内部执行 CREATE FUNCTION 语法?

任何帮助将不胜感激。

Consider the example given on this page about creating user-defined functions in SQL and then using the user-defined function in the WHERE clause.

mysql> CREATE FUNCTION myFunction(in_rep_id INT)
    ->      RETURNS INT
    ->      READS SQL DATA
    -> BEGIN
    ->      DECLARE customer_count INT;
    ->
    ->      SELECT COUNT(*)
    ->           INTO customer_count
    ->        FROM employee
    ->       WHERE id=in_rep_id;
    ->
    ->      RETURN(customer_count);
    ->
    -> END$

mysql> SELECT id,myFunction(id)
    ->   FROM employee
    ->  WHERE myFunction(id)>0
    ->  ORDER BY myFunction(id) desc;

How do I replicate this using CakePHP? Do I need to define the function as part of a CakePHP Model? Or is there a way to execute the CREATE FUNCTION syntax but from within CakePHP?

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城已无爱 2024-10-04 18:51:22

CREATE FUNCTION 语法实际上是 DDL(数据定义语言),与其他 DDL 语法(例如 CREATE TABLE)一样,Cake 不支持。我怀疑,你可以通过 $this->Model->query( 'your sql here' ) 来执行一个函数,尽管我从来没有需要尝试过。

我应该补充一点,您确实可以通过 Model::query() 方法运行任何任意 SQL,但我假设这是您想要的东西,就像您想要表一样在实际需要之前放置。

The CREATE FUNCTION syntax is really DDL (data definition language) and, like other DDL syntax (e.g. CREATE TABLE), isn't supported by Cake. You can execute a function via $this->Model->query( 'your sql here' ), I suspect, though I've never needed to try it.

I should add that you can really run any arbitrary SQL via the Model::query() method, but I made an assumption that this is something you want in place in the same way you want a table in place before it's ever actually needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文