简体   繁体   中英

Yii framework how to get an object from find() or findBySql()

find() and findBySql() both return a string with all the attributes concatenated together, that's not very useful to me.

        $customers = Customer::model()->find("
            CONCAT( fName, ' ', lName ) LIKE  ?
            ", array( '%' . $searchVal . '%'));

How can I get something useful like an object or array, so I can do something like this?

        $str = '';
        foreach ($customers as $customer){
            $str .= "<option>" . $customer->fName . " " . $customer->lName . "</option>" ;
        }

Nobody was lobotomized. In fact, there are several functions that can be used to query the db and all of them are useful in different scenarios.

find() returns the first row satisfying the specified condition
findByPk() returns the row with the specified primary key
findByAttributes() returns the first row using the specified SQL statement
findBySql() returns the first row using the specified SQL statement
findAll() returns all rows satisfying the specified condition
findAllByPk() returns all rows with the specified primary keys
findAllByAttributes() returns all rows with the specified attribute values
findAllBySql() returns all rows using the specified SQL statement

All these functions have their uses, just use the one appropriate for your scenario.

Try FindAll() instead of find(). find() is intended for a single result.

    $customers = Customer::model()->findAll("
        CONCAT( fName, ' ', lName ) LIKE  ?
        ", array( '%' . $searchVal . '%'));

find返回匹配条件的所有对象的第一个对象findAll返回列表...只需使用您需要的...它们不返回字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM