如何生成一个MySQL是不是在CakePHP中NULL条件? [英] How to generate a MySQL IS NOT NULL condition in CakePHP?

查看:150
本文介绍了如何生成一个MySQL是不是在CakePHP中NULL条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到结果的一个子集作为virtualField在我的观点中使用。我甚至可能是遥远的我如何处理这个,但这里是我迄今所做的:

I'm trying to get a subset of results as a virtualField for use in my view. I may even be way off on how I'm approaching this, but here's what I've done so far:

我开始在这里这个问题:<一href=\"http://stackoverflow.com/questions/20458331/cakephp-virtualfield-find-all-not-null/20458367?noredirect=1#comment30568626_20458367\">CakePHP virtualField找到所有不为空而导致的这个小美女

I started with this question here: CakePHP virtualField find all not null which lead to this little beauty.

现在我有一个问题,即查找声明传递(阵列)到MySQL的。

Now I have an issue where the find statement passing (Array) into the MySQL.

我的code如下:

class Transaction extends AppModel {
public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table, $ds);
    $this->virtualFields['Accounts'] = $this->find("all", array("conditions" => array("account !=" => null)));
}

和我看到:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

SQL Query: SELECT `Transaction`.`id`, `Transaction`.`name`, 
`Transaction`.`person_id`, `Transaction`.`account`, (Array) 
AS `Transaction__Accounts` FROM `my_database`.`transactions` 
AS `Transaction` WHERE `Transaction`.`person_id` = (2)

我也试过 $这个 - &GT;&交易 - GT;找到!Transaction.account =,都无济于事。我发现了一些其他问题与(阵列),但没有能够帮助我的情况。在正确的方向的任何指针将是巨大的。

I've also tried $this->Transaction->find and "Transaction.account !=", to no avail. I've found some other issues with the (Array) but none that help my situation. Any pointers in the right direction would be great.

推荐答案

问题:您的查询结果是一个数组,你告诉SQL来字段名分配到包含每个查询结果阵列 - 虚拟领域只取得包含如字符串单级变量

Problem: your query results are an array, and you're telling SQL to assign a field name to each query result containing that array - virtual fields are only made to contain single level variables like strings.

解决方案:使用加入结构本身上与那些将返回一个嵌套的结果以及每个结果的设置的条件。使用 CakePHP的模型关系来做到这一点:

Solution: use a join structure onto itself with those conditions which will return a nested result set along with each of your results. Use CakePHP's model relationships to do this:

<?php    
class Transaction extends AppModel {
    var $hasMany = array(
        'Accounts' => array(
            'className' => 'Transaction',
            'foreignKey' => false,
            'conditions' => array('Accounts.account IS NOT NULL')
        )
    );
}    
?>

示例输出:

Array(
    'Transaction' => array( // transaction data),
    'Accounts' => array( // associated transaction data with account set to null
)

现在,你也许可以从该结果集,如果您从交易返回1​​000行,你会得到的所有的结果帐户嵌套到每个交易结果。这是很不理想。从这里,你可以让加盟条件更加具体的目标相关的帐户记录,或者这是不是适合你的方法。

Now, as you can probably gather from that result, if you return 1000 rows from Transaction, you'll get all results from Accounts nested into each Transaction result. This is far from ideal. From here, you can either make the join conditions more specific to target relevant Accounts records, or this is not the right approach for you.

其他方法可能是:


  • 帐户模式,使用交易数据库表,发现隐含的条件是帐户是空

  • 手动查询,检索这些结果在一个afterFind()方法你的交易 模式,这将检索结果的一次后,你会再收益array_merge( $帐户,$交易)

  • Accounts model, uses Transaction database table, implicit find conditions are that account is null
  • Manual query to retrieve these results in the afterFind() method of your Transaction model, which will retrieve these results once, and you'll then return array_merge($accounts, $transactions)

这篇关于如何生成一个MySQL是不是在CakePHP中NULL条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆