Domanda

Inizio il lavoro con il framework PHP di Litio.Devo fare una query per ottenere domande che hanno "% test%" o "% facile%" nel campo "Titolo". Ho provato a farlo usando il codice followin:

$questions = Questions::find('all', array(
        'conditions'    => array(
            'name' => array(
                'like' => array(
                    '%easy%',
                    '%test%'
                )
            )
        )
    ));
.

Ma fa questa query:

SELECT * FROM `questions` AS `Questions` WHERE (`name` LIKE '%easy%' AND `name` LIKE '%test%')
.

Come sostituire e con o?

Grazie

È stato utile?

Soluzione

Come soluzione alla tua domanda puoi usare in (=) anziché come

'conditions'    => array(
    'name' => array(
        '=' => array(
            '%easy%',
            '%test%'
        )
    )
)
.

Generi la query:

WHERE ((`name` IN ('%easy%', '%test%')))
.

o può essere utilizzato durante la ricerca utilizzando due campi diversi:

'conditions' => array(
    'OR' => array(
        'name' => array(
            '=' => array(
                '%easy%',
                '%test%'
            )
        ),
        'name2' => array(
            '!=' => array(
                '%easy%',
                '%test%'
            )
        )
    )
),
.

E questo genererà query:

WHERE ((`name` IN ('%easy%', '%test%')) OR (`name2` IN ('%easy%', '%test%')))
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top