Some time, I just ony need a few column not all of one record, How should i do..
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.
|
May '15 |
4 |
5008 |
0 |
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
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();
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' ));