在本文中,您将会了解到关于php–SQL到ActiveRecord标准的新资讯,同时我们还将为您解释phpsqlsrv的相关在本文中,我们将带你探索php–SQL到ActiveRecord标准的奥秘,
在本文中,您将会了解到关于php – SQL到ActiveRecord标准的新资讯,同时我们还将为您解释php sqlsrv的相关在本文中,我们将带你探索php – SQL到ActiveRecord标准的奥秘,分析php sqlsrv的特点,并给出一些关于activerecord ruby:php + ruby with activerecord 範例、activerecord – Rails 3:使用form_for的非Active Record对象、activerecord – ruby-on-rails3,并使用activereccord3选择distinct、activerecord – 如何使用Active Record使Sinatra中的SQLite3记录器静音?的实用技巧。
本文目录一览:- php – SQL到ActiveRecord标准(php sqlsrv)
- activerecord ruby:php + ruby with activerecord 範例
- activerecord – Rails 3:使用form_for的非Active Record对象
- activerecord – ruby-on-rails3,并使用activereccord3选择distinct
- activerecord – 如何使用Active Record使Sinatra中的SQLite3记录器静音?
php – SQL到ActiveRecord标准(php sqlsrv)
我试图在ActiveRecord标准中重现以下sql:
SELECT COALESCE(price, standardprice) AS price
FROM table1
LEFT JOIN table2
ON (pkTable1= fkTable1)
WHERE pkTable1= 1
到目前为止,我有以下内容:
$price = Table1::model()->find(array(
"select" => "COALESCE(price, standardprice) AS price",
'with' => array(
'table2' => array(
'joinType' => 'LEFT JOIN',
'on' => 'pkTable1= fkTable1',
)
),
'condition' => 'pkTable1=:item_id',
'params' => array(':item_id' => 1)
));
但这导致以下错误:’活动记录“Table1”正在尝试选择无效列“COALESCE(价格”.注意,该列必须存在于表中或者是带别名的表达式.
该列应该存在,这里是2个表结构:
表格1
pkTable1 int(11) - Primary key
standardprice decimal(11,2)
name varchar(255) //not important here
category varchar(255) //not important here
表2
pkTable2 int(11) - Primary key //not important here
fkType int(11) - Foreign key //not important here
fkTable1 int(11) - Foreign key, linking to Table1
price decimal(11,2)
我究竟做错了什么?
解决方法:
您需要使用CDbExpression
作为COALESCE()表达式:
$price=Table1::model()->find(array(
'select'=>array(
new CDbExpression('COALESCE(price, standardprice) AS price'),
),
'with' => array(
'table2' => array(
'joinType'=>'LEFT JOIN',
'on'=>'pkTable1=fkTable1',
),
),
'condition'=>'pkTable1=:item_id',
'params'=>array(':item_id'=>1)
));
我进一步相信如果table2已经在Table1模型的relations()方法中链接,则以下行应该足够了:
'with'=>array('table2'),
activerecord ruby:php + ruby with activerecord 範例
如果老闆要求使用php,可是您卻是ruby狂熱者,這.. 怎辦呢?
沒關係! 一樣用ruby寫,php只要做一點點的處理就好!
how to? php中有這個函式:exec
( 本範例實作於windows xp professional搭配instantrails;在其他作業系統上沒有測試過,不過各位還是可以嘗試看看 )
我們來試試看吧!
先寫個test.rb:
#!/usr/bin/env ruby
#
# filename: test.rb
#
puts "hello"
puts "world"
再寫個test.php:
exec("test.rb", $args);
foreach($arg as $args)
echo $arg . "
";
?>
將兩個檔案放在同一個目錄下後,打開瀏覽器瀏覽test.php;看!是不是顯示結果出來了?
ok,我們直接來用activerecord幫我們新增資料吧!
我們需要一張普通頁面、一張php網頁跟一個ruby檔案:
#!/usr/bin/env ruby
#
# filename: ar.rb
#
require ''rubygems''
gem ''activerecord''
activerecord::base.establish_connection(
:adapter => ''mysql'',
:host => ''localhost'',
:username => ''root'',
:password => '''',
:database => ''cal''
)
class event
name, descr = argv[0], argv[1]
puts event.new({:name => name, :descr => descr, :date => date.today, :time => time.now}).save
好了,接下來是普通頁面,這是送出表單:
這是php網頁:
// filename: ar_save.php
exec("2.rb " . $_post["usrname"] . " " . $_post["descr"], $arg);
if($arg[0]) echo "success!";
?>
ok,讓我們來試試看吧! 本文链接http://www.cxybl.com/html/wlbc/Php/20120607/28694.html
activerecord – Rails 3:使用form_for的非Active Record对象
解决方法
看看here.
看看Github的源代码.
activerecord – ruby-on-rails3,并使用activereccord3选择distinct
特别是下面的电话!
Error.find(:all,:select => 'disTINCT type')
有人有一个想法,如何将此调用转换为ActiveRecord3有效语句?
我在网路上找不到任何东西
谢谢
解决方法
select
查询方法.
Error.select('disTINCT type')
activerecord – 如何使用Active Record使Sinatra中的SQLite3记录器静音?
D,[2012-11-19T18:36:30.391459 #53057] DEBUG -- : (0.1ms) begin transaction D,[2012-11-19T18:36:30.392668 #53057] DEBUG -- : sql (0.4ms) INSERT INTO "foo" ("created_at","id","foo","updated_at","value") VALUES (?,?,?) [["created_at",2012-11-19 18:36:30 -0800],["id",4],["bar",2],["foo",12],["updated_at",["value",true]] D,[2012-11-19T18:36:30.393572 #53057] DEBUG -- : (0.7ms) commit transaction
如何关闭此输出?
我试过ActiveRecord :: Base.logger = nil,但这似乎没有做任何事情.
使用sinatra-activerecord,rspec和guard.
解决方法
ActiveRecord::Base.logger.level = 1
任何1或更大的值都有效.我想它也适用于其他数据库.
Reference
关于php – SQL到ActiveRecord标准和php sqlsrv的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于activerecord ruby:php + ruby with activerecord 範例、activerecord – Rails 3:使用form_for的非Active Record对象、activerecord – ruby-on-rails3,并使用activereccord3选择distinct、activerecord – 如何使用Active Record使Sinatra中的SQLite3记录器静音?等相关知识的信息别忘了在本站进行查找喔。
本文标签: