将查询转换为 cakePHP find 语句

发布于 2024-10-12 21:44:11 字数 1338 浏览 2 评论 0原文

任何人都可以帮我将此查询转换为 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 技术交流群。

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

发布评论

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

评论(1

季末如歌 2024-10-19 21:44:11

除非有特殊原因需要使用 find 方法,否则我会简单地创建一个模型方法并使用 query() 来执行 SQL 语句。如果月份和年份是参数,请确保它们是整数,以避免 SQL 注入攻击。

Unless there is some special reason for using the find method, I would simply create a model method and use query() to execute the SQL statement. And if month and year are parameters, make sure they are integers to avoid an SQL injection attack.

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