B
B
Bersonazh2019-07-11 20:21:10
Yii
Bersonazh, 2019-07-11 20:21:10

How to pull the desired strings from the array?

I don't know how to do it right anymore. Everything works out with simple requests, I try to do it through the built-in functions of yii2 and everything is down the drain.
In the console response, I need to pull out the field with id.
5d276f66a1f16187403755.png
controller

class CatalogController extends Controller
{

    public function actionCatalog()
  {
      return $this->render('catalog');
  }
    
  public function actionGetFavourite()
    {
         if(Yii::$app->request->isAjax){

            $model = new user_wishlist();

            $model->good_id = Yii::$app->request->post();

            $model->username = Yii::$app->user->identity->username;

            $user_id = user_wishlist::getUserId( $model->username );

            // user_wishlist::saveUserWish( $model->good_id,  $user_id );

         }
    }
  
}

model
public static function getUserId($username)
 		{
 			if($username)
 			{
 				// $q = User::find(['id'])->where(['username' => $username])->orderBy('id')->one();

 				$q = User::findOne(['username' => $username ]);

        // $res = Yii::$app->db->createCommand($q)->execute();
 				// print_r($q);
 				print_r( $q );

 				// foreach ($q as $key => $value1) {
 				// 	foreach ($value1 as $key => $value) {
 				// 		print_r($key.' : '.$value.'<br>');
 				// 	}
 				// }
 				
 				// $id = ArrayHelper::getValue($q, 'id');

        // $res = Yii::$app->db->createCommand($q)->queryOne();

        // foreach ($res as $key => $value) {
        // 	$id = $value;
        // }
        // " SELECT `id` FROM `user` WHERE  `username` = '$username' ";

 			}

 			

 		}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2019-07-11
@Bersonazh

var_dump($user->id);
https://www.yiiframework.com/doc/api/2.0/yii-db-ac...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question