将查询转换为 cakePHP find 语句
任何人都可以帮我将此查询转换为 CakePHP find 语句,
SELECT
SUM(ISNULL(tg_prd_value,0)) as total,MAX(tblMarketingArea.mkt_Area_ID) as mkt_Area_ID,MAX(tblMarketingArea.mkt_Area) as mkt_Area
FROM
tblTarget right outer join
tblMarketingArea on (tblMarketingArea.mkt_Area_ID = tblTarget.tg_area_id and tblTarget.tg_month = 1 and tblTarget.tg_year = 2011)
where tblMarketingArea.isDeleted!=1
GROUP BY
tblMarketingArea.mkt_Area_ID order by tblMarketingArea.mkt_Area_ID
我有 "Target" 和 "MarketingArea" 模型。(我知道表名不符合 cakephp 约定但我必须使用这些名称)。由于我使用sql server,我根据蛋糕约定
模型MarketingArea (marketing_area.php)
class MarketingArea extends AppModel{
var $name='MarketingArea';
var $primaryKey='mkt_Area_ID';
}
为表创建了同义词,我已经尝试过这种绑定
$this->bindModel(array(
'hasMany' => array(
'Target' => array(
'foreignKey' => 'tg_area_id',
'conditions' => array(
'tg_month' => 1,
'tg_year' => 2011
)
)
)
)
);
,但我仍然面临问题SUM(tg_prd_value)
谢谢您。
Can any one help me to convert this query into CakePHP find statement
SELECT
SUM(ISNULL(tg_prd_value,0)) as total,MAX(tblMarketingArea.mkt_Area_ID) as mkt_Area_ID,MAX(tblMarketingArea.mkt_Area) as mkt_Area
FROM
tblTarget right outer join
tblMarketingArea on (tblMarketingArea.mkt_Area_ID = tblTarget.tg_area_id and tblTarget.tg_month = 1 and tblTarget.tg_year = 2011)
where tblMarketingArea.isDeleted!=1
GROUP BY
tblMarketingArea.mkt_Area_ID order by tblMarketingArea.mkt_Area_ID
i have "Target" and "MarketingArea" model.(I know the tablenames are not according to the cakephp convention but i have to use these names). Since i am using sql server i created Synonyms for the tables according to the cake convension
model MarketingArea (marketing_area.php)
class MarketingArea extends AppModel{
var $name='MarketingArea';
var $primaryKey='mkt_Area_ID';
}
i have tried this binding
$this->bindModel(array(
'hasMany' => array(
'Target' => array(
'foreignKey' => 'tg_area_id',
'conditions' => array(
'tg_month' => 1,
'tg_year' => 2011
)
)
)
)
);
But still i am facing problem with SUM(tg_prd_value)
thankz in advace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非有特殊原因需要使用 find 方法,否则我会简单地创建一个模型方法并使用 query() 来执行 SQL 语句。如果月份和年份是参数,请确保它们是整数,以避免 SQL 注入攻击。
Unless there is some special reason for using the
find
method, I would simply create a model method and usequery()
to execute the SQL statement. And if month and year are parameters, make sure they are integers to avoid an SQL injection attack.