Trying to migrate SQL commands from plain PHP using Yii2

I am trying to change my PHP statement to Yii2. Here's the source 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 am new to Yii2 framework and am trying to convert it to Yii2 but have no idea how. I am performing this function in 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 fix this?

+3


source to share


3 answers


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

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

use yii\db\Query;

...

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

      

Alternatively, you can build your query with Yii::$app->db->createCommand()

, but it looks better.

2) If you have a model and want to get the 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 to replace some_function

by calling some class of methods, because it is OOP.

PS Take your time and read the docs . He was there.

+4


source


$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');
}

      

+1


source


The Yii way is to first create a DB model and then use it. It is very easy to load all data from a table using models. Forget about writing plain SQL - unless absolutely necessary - and use Yii's ORM approach.

"User" is a model associated with a database table. It looks like this (a few more features might be required)

namespace app\models\db;

class User extends \yii\db\ActiveRecord {

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

      

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

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

      

you don't need to match the result with the array, as you can easily use Yii's OOP way:

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

      

It's much easier than converting to arrays and using for

instead foreach

and getting the number of strings ... If for some other reason you need to return the number of objects to return, now you can only usecount($users)

So simple. You may also need to check the Yii2 Implementation Guide for an overview of how to use it.

+1


source







All Articles