How get model data by filed index

i have db table user (user_id, name)

need fetch array [user_id => name]

how fetch this array use phalcon methods?

in yii there is method indexBy

why it does not have?

It doesn't have a method like that because honestly, I think the number of times it would be useful are very low.

I think you best chance at doing this would be to just retrieve all the relevant records using find(), then iterating through the results and build your own array.

edited May '16

Honestly i was looking for such solution also, but could not find any. Actually im needing it 2-3 times per project, so i ended up adding a method in my model (like Dylan's suggestion). Something like that you can reuse:

public function getAllForSelect()
{
    $lang = $this->getDI()->getSession()->language;
    $items = CategoriesI18n::find([
        'columns' => ['foreign_key', 'title'],
        'conditions' => 'lang = :lang:',
        'bind' => [
            'lang' => $lang
        ],
        'order' => 'title ASC',
        'cache' => ['lifetime' => 360000, 'key' => 'categories-all-for-select-'. $lang]
    ]);
    $collection = [];
    foreach ($items as $item) {
        $collection[$item->foreign_key] = $item->title;
    }
    return $collection;
}


10.9k
edited May '16

i use foreach now. but its no good idea when need use many methods with filed index

and its less duplicate code

Why not make a generic method in your Main Model and reuse it, this way you wont have repetitive code... you can make it with minimal effort, no need for extra stuff to bloat the framework :)



10.9k

đťodel may be different. its the task of the framework and not the developer

toArray() too, need generic yourself?

edited May '16
class Users extends BaseModel{}

class BaseModel extends \Phalcon\Mvc\Model {
  public function indexBy($params) {
    // blabla
  }
}

You have the option to overwrite default model methods, also to create new ones, something simple like above..

Thats the good thing about Phalcon it is not stuffed with useless classes that you may never need.. like email sending class, pdf generator, xls reader or even Stripe payment in Larabel.. who the fuk cares about those ? :)



10.9k

toArray() useless method?

edited May '16

Honestly i was looking for such solution also, but could not find any. Actually im needing it 2-3 times per project, so i ended up adding a method in my model (like Dylan's suggestion). Something like that you can reuse:

public function getAllForSelect()
{
    $lang = $this->getDI()->getSession()->language;
    $items = CategoriesI18n::find([
        'columns' => ['foreign_key', 'title'],
        'conditions' => 'lang = :lang:',
        'bind' => [
            'lang' => $lang
        ],
        'order' => 'title ASC',
        'cache' => ['lifetime' => 360000, 'key' => 'categories-all-for-select-'. $lang]
    ]);
    $collection = [];
    foreach ($items as $item) {
        $collection[$item->foreign_key] = $item->title;
    }
    return $collection;
}

Why does this method build $collection? If you're iterating through it in your view files, you can iterate through a collection as returned by find() just as easily.

@Dylan, this was just a mere example. To feed your curiosity i'm using the array for form select values, presentating db values to users (when storing ID in database, but wanna show actual title)... and so on :)



10.9k

I think adding this method to simplify the life of all.