0

このような構造で言うと、列は次のとおりです。

ファーストネーム | セカンドネーム | ミドルネーム | 苗字

ここで、「名前」を入力する必要があるフォームがあるとします..名前の種類を指定していません。

コントローラーでは、その「名前」がデータベースにあるかどうかを確認する必要があります。

私はこのようにします:

public function actionCheckName($name){

$name = PersonName::model()->findByAttributes(array('first_name'=> $name));
//then check if the name exists in the first_name column

if(!empty($name)){
$name = PersonName::model()->findByAttributes(array('second_name'=> $name));
}
//if it's not in the first_name column...check in the second_name column

..
..and so on, i hope you get the idea

}

基本的に、ユーザーがどのタイプの名前を入力したかはわかりません...しかし、エントリが存在するかどうかをデータベースで確認する必要があります..これらの名前タイプのいずれかに含まれているかどうか。

上記のコードは動作しますが、見た目は少し...あまり良くありません..これを行うためのより良い方法はありますか? どうもありがとう!

4

2 に答える 2

3
        $criteria=new CDbCriteria;
        $criteria->compare('first_name', $name, false, 'OR'); // The 3th param determine if the string comparation use "LIKE" or "="
        $criteria->compare('second_name', $name, false, 'OR');
        $criteria->compare('middle_name', $name, false, 'OR');
        $criteria->compare('last_name', $name, false, 'OR');

        $nameList = PersonName::model()->findAll($criteria); // Return all PersonName with $name in any field.

        $name = PersonName::model()->find($criteria); // Return the first PersonName with $name in any field.
于 2013-09-20T06:50:08.767 に答える