簡體   English   中英

cakephp OR 條件

[英]cakephp OR condition

最初發布在 cakephp 問答上,但我會把它放在這里希望得到一些答案。

我有一堆公司的默認狀態為 0,但有時會獲得更高的狀態。 現在我想使用高狀態(如果存在),但如果不存在則恢復為 0。 我嘗試了很多不同的方法,但我總是只得到狀態為 0 的方法或具有我想要的狀態的方法,如果存在則從不給我狀態,如果不給我狀態,則為 0。

只給我我指定的狀態,而不給我狀態為 0 的狀態:

'Company' => array (
    'conditions' =>  array (
        'OR' => array(
            'Company.status' => 0,
            'Company.status' => $status,
        )

    )
)

只給我0的狀態:

'Company' => array (
    'conditions' =>  array (
        'OR' => array(
            'Company.status' => $status,
            'Company.status' => 0
        )
    )
)

代碼中的狀態定義和檢索數據:

function getCountry($id = null, $status = null) {
    // Bunch of code for retrieving country with $id and all it's companies etc, all with status 0.
    $status_less_companies = $this->Country->find...

    if ($status) {
        $status_companies = $this->Country->find('first', array(
            'conditions' => array(
                'Country.id' => $id
            ),
            'contain' => array(
                'Product' => array (
                    'Company' => array (
                        'conditions' =>  array (
                            'OR' => array(
                                'Company.status' => $status,
                                'Company.status' => 0
                            )
                        )
                    )
                )
            )
        )
    }

    // Mergin $status_less_companies and $status_companies and returning data to flex application.
}

我為這個問題更改了模型的名稱只是為了更有意義,當我告訴他們我為我的 flex 應用程序使用 cakephp 時,人們通常會害怕。 我想這個問題的邏輯沒有意義,但相信我,它在我的應用程序中是有意義的。

謝謝!

嘗試

'Company' => array (
    'conditions' =>  array (
        'OR' => array(
            array('Company.status' => 0),
            array('Company.status' => $status),
        )

    )
)

在食譜中,如果條件或條件屬於同一字段,則表示將條件或條件包裝在數組中http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions

我不確定您希望得到什么結果。 如果要檢索狀態為0的所有記錄,再加上狀態為3的記錄,則可以使用“ IN”代替“ OR”。

在Cake中,您可以這樣編寫:

$status = 3;
$conditions = array('Company.status' => array(0, $status));

您也可以使用以下方法獲取記錄:將值放入數組中,例如$ arr = array(1,2);

 $res = $this->Model->find('all', array(                      
        'conditions' =>array('Model.filedname'=>$arr),
        'model.id' => 'desc'
  ));

希望您能找到答案。

    $this->loadModel('Color');
    $colors = $this->Color->find('all', [
        'conditions' => [
            'Color.is_blocked' => 0,
            'Color.is_deleted' => 0,
            'OR' => [
                [
                    'Color.isAdmin' => $user_data['id'],
                ],
                [
                    'Color.isAdmin' => 0,
                ]
            ],
        ]
    ]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM