如何使用 CakePHP 创建用户定义的 sql 函数?
考虑此页面上给出的有关在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.