4

I'm trying to change my PHP statement to Yii2. Here's the original code:

$sql = "select id from users where member_type='d'";
$max = @mysql_num_rows($r=mysql_query($sql));
for($i=0;$i<$max;$i++){
    $demo2=mysql_fetch_assoc($r);
    some_functions($demo2['id'], 'something');
}

I'm new to Yii2 framework and trying to convert it to Yii2 but have no idea how. I'm doing this function under a modal file.

Here's what I can do at best:

$max= Yii::$app->dbFxprimus->createCommand("select count(id) from ". $this->tableName() ." where member_type='d'")->queryAll();
for($i2=0;$i2<$max;$i2++){
    $demo=mysql_fetch_assoc($max); //this is definitely wrong, help me to fix this, I don't know what is the Yii2 function for this.
    some_function($demo['id'], 'something');
}

Can anyone help me to fix this?

3 Answers 3

6

First of all you need to build query and get the results.

1) If you don't have model, you can do it like that:

use yii\db\Query;

...

$users = (new Query())
    ->select('id')
    ->from('users')
    ->where(['member_type' => 'd'])
    ->all()

Alternatively you can build query with Yii::$app->db->createCommand(), but this is better looking.

2) If you have model, and want get results as associative arrays:

use app\models\User;

...

$users = User::find()
    ->select('id')
    ->where(['member_type' => 'd'])
    ->asArray()
    ->all();

Then just loop through and apply your function:

foreach ($users as $user) {
    some_function($user['id'], 'something');
}

And you need replace some_function to call of some method class, because it's OOP.

P.S. Take your time and read the docs. It's covered there.

Sign up to request clarification or add additional context in comments.

Comments

1
$ids = Yii::$app->dbFxprimus->createCommand("select id from ". $this->tableName() ." where member_type='d'")->queryColumn();
foreach($ids as $id){
    some_function($id, 'something');
}

And if '$this' is an ActiveRecord instance:

$ids = self::find()
    ->select(['id'])
    ->where(['member_type' => 'd'])
    ->column();
foreach($ids as $id){
    some_function($id, 'something');
}

9 Comments

Code only answers are not welcomed on Stack Overflow. Please add some explanation.
May I know if I'm not using the "foreach", I want to use the "for loop", what Yii2 command can I use ?
There is no Yii command for common php functions. You still use for($i=0;$i<(count($ids)-1);$i++) and so on.
I recommend spend some time and read the docs. And even for plain PHP your example is not optimum.
actually the answer from @arogachev is better, as he shows you how to use a Model. In your case this is the Yii way to go. If you want to learn Yii, forget about writing plain SQL
|
1

The Yii way to go would be to create a DB Model first, and then use it. It is very simple to load all data from a table using models. Forget about writing plain SQL - if not absolutly needed - and use the ORM approach of Yii.

'User' is the model connected to the DB table. It looks similar to this (there may be a few more functions needed)

namespace app\models\db;

class User extends \yii\db\ActiveRecord {

    public static function tableName()
    {
        return 'users'
    }
    ...
}

Check the documentation on how to use models and how to generate them automatically, so you don't have to write everything by yourself. Then you can use the model to retrieve data easily:

$users = User::find()->where(['member_type' => 'd'])->all();

you do not need to concert the result into an array, as you can easily use the OOP way of Yii:

foreach ($users as $user) {
    some_function($user->id, 'something');
}

This is much easier than converting to arrays, and using for instead of foreach, and getting the count of rows... If you for some other reason would need to get the count of objects returned, you can now use only count($users)

As simple as that. Maybe you should also check the Introdcution Guide of Yii2 to get an overview on how to use it.

1 Comment

Actually, I'm lost on the User::find().... mind explaining that ? is 'User' a class inside the model?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.