Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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: http://docs.phalconphp.com/en/latest/reference/phql.html



5.1k
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();


10.6k
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:

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