How to reference findBySql Array

Hi,

I have this in my Clients model.




    public function getFullName() 

    {

    	return $this->client_firstname.' '.$this->client_lastname;

    }

    

    

    public function clientsOrderByDateCreated()

    {

    	$sql = 'SELECT * FROM clients ORDER BY date_created DESC';

    	$clientdata = Clients::findBySql($sql)->asArray()->all();

    	return $clientdata;

    }



I have this in my view.




<?php

   $newclients = new Clients();

   $arraydata = $newclients->clientsOrderByDateCreated();

?>


<?= $form->field($model, 'clients_id')->dropDownList(ArrayHelper::map($arraydata, 'id', 'fullName')) ?>



In my view you can see that I am calling the clientsOrderByDateCreated function which returns an array of data. I then use that array of data in the ArrayHelper map for the dropDownList. Also in the dropDownList I use fullName which is a function in the Clients model class.

Now from what I can find is that in the getFullName function, I am using $this, which is obviously not working. I have also tried setting the $clientdata as global variable and using $clientdata->client_firstname, but that is also not working.

How do I reference the array correctly in the getFullName function?

Or is there a better way to sort the model? I don’t mind sorting the complete model as I will always want to see the last added client first.

Thanks

Do you want to get the data from db then display it in the dropdownlist in Descending order??? is that what you need??

Yes, descending order on the date_created column.

i suggest to you to use an Primary Id. in that way it’s easy to get the data and Display it in descending order

I have primary id, but still, where or how do I use primary id to sort the model so that where ever I use it, I will have it in descending order?

Thanks,

I got it working using this in my view.




    <?= $form->field($model, 'clients_id')->dropDownList(ArrayHelper::map(Clients::find()

    								->orderBy('id DESC')

    								->all(), 'id', 'fullName')) ?>



Is this a better/good solution?