We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to select some column not all use Phalcon\Mvc\Model

Some time, I just ony need a few column not all of one record, How should i do..

edited Mar '14

You can use PHQL::

// Instantiate the Query
$query = new Phalcon\Mvc\Model\Query("SELECT id,name FROM Cars", $di);
// Execute the query returning a result if any
$cars = $query->execute();

//OR

//Executing a simple query
$query = $this->modelsManager->createQuery("SELECT id, name FROM Cars");
$cars = $query->execute();

find complete documentation here: https://docs.phalcon.io/en/latest/reference/phql.html



9.8k
Accepted
answer

I belive you can also do it using model, ie:

$names = Employees::find(array(
            'conditions' => 'active = true',
            'columns' => 'id, name, surname'
            ));

If you are using the query builder you can do this:

$di      = \Phalcon\DI\FactoryDefault::getDefault();
$manager = $di['modelsManager'];
$builder = $manager->createBuilder();

$builder->from('Employees');
$buidler->columns('id,name,surname');

$query = $builder->getQuery();
$data = $query->execute();


25.7k
edited May '15

you're the man (y) Question: the consequence of selecting partial columns may be losing the related models?


update: seem like if the value of "columns" is set to empty string, it is an error.

I belive you can also do it using model, ie:

$names = Employees::find(array(
          'conditions' => 'active = true',
          'columns' => 'id, name, surname'
          ));