2016-04-27 8 views
0

Ich schreibe diese Abfrage in yii2 -Yii2 Abfrage: Zugriff Array-Elemente in Objekt von einer Abfrage zurück

$senderInfo[] = Customers::find()->where(['cust_id' => $resShipment['ship_shipper_id'], 'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all(); 

und bekommen dies als Folge -

Array ([0] => Array ()) Array ([0] => Array () [1] => Array ()) Array ([0] => Array () [1] => Array () [2] => Array ([0] => common\models\Customers Object ([_attributes:yii\db\BaseActiveRecord:private] => Array ([cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => [email protected] [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 02881 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No) [_oldAttributes:yii\db\BaseActiveRecord:private] => Array ([cust_id] => 79 [cust_user_id] => 2 [cust_designation] => Mr [cust_fname] => abcd [cust_mname] => xyz [cust_lname] => xyz [cust_country_code] => 91 [cust_mobile] => 8888888888 [cust_email] => [email protected] [cust_id_proof] => [cust_country] => [cust_state] => [cust_city] => [cust_location] => [cust_street] => [cust_address] => delhi [cust_pobox] => 413714 [cust_additional_detail] => [cust_picture] => [verified] => Yes [created] => 2016-04-20 14:45:42 [updated] => 0000-00-00 00:00:00 [deleted] => No) [_related:yii\db\BaseActiveRecord:private] => Array () [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array () [_behaviors:yii\base\Component:private] => Array ()))) 

Aber wegen der leeren Arrays am Anfang von Dieses Array kann ich nicht auf die Elemente zugreifen. Ich muss es in einer (strikt) Schleife bevölkern, nachdem es zugänglich wird.

+0

Versuchen Sie dies mit der Verwendung von 'asArray()', '$ senderInfo [] = Kunden :: find() -> wo (['cust_id' => $ resShipment ['ship_shipper_id'], 'cust_country_code' => Yii :: $ app-> benutzer-> identity-> country_code]) -> distinct() -> asArray() - > all(); ' –

+0

es hat auch nicht funktioniert. –

Antwort

0

Wenn Sie das Ergebnis in einem Array möchten Sie Active Funktion toArray verwenden können()

$myModel = Customers::find()-> 
    where(['cust_id' => $resShipment['ship_shipper_id'], 
    'cust_country_code' => Yii::$app->user->identity->country_code])->distinct()->all(); 

$senderInfo = myModel->toArray(); 
+0

Da es bereits ein Array ist, gibt es den folgenden Fehler, wenn es verwendet wird, umArray() - Aufruf einer Memberfunktion toArray() auf einem Nichtobjekt –

+0

das Ergebnis eines Model :: find() ..... - > all() ist kein Array, sondern eine Sammlung von Modellen. Wenn Sie in Array konvertieren möchten, sollten Sie asArray wie oben in commmenr oder toArray() wie in meiner Antwort verwenden. Aber ... warum brauchen Sie ein Modell, das in Array konvertiert wurde? – scaisEdge

+0

Danke Jungs für die Hilfe. Aber ich habe einen anderen Weg gefunden, mein Problem zu lösen. Also, jetzt benutze ich diese Abfrage nicht. Wie auch immer, danke für die Hilfe. –