We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Model::find()

Hallo to everyone, i have a model with a simple relation

Username: Id,Name,Email,...

UsernameOptions: Id,User_Id,Key,Value

When i am using now $result=Username::findFirst() i get all the Username´s entries. but not the options. There i have to call it like result->usernameOptions. Is there a way do avoid the lazy loading and load already all relations with the "Username::findFirst() and/or Username::find()"?

Thanks Pat



7.1k

You should use the query builder for this: https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Query_Builder.html

Then you can find the results in the row returned and setup the hierarchy. There's an issue filed for the functionality you're looking for: https://github.com/phalcon/cphalcon/issues/1117

thanks for your answers. i am using now afterFetch and it is working. but i have one last question. if i am defining a column set, there is no relation anymore to the UsernameOptions

Username::find(array('columns'=>'id'));

and if i am using something like this, i get an error that the alias is not working. i tried it with the right alias, and the full namespace/model class.

Username::find(array('columns'=>'id,UsernameOptions.value'));

is there another way i have to do it?

hey dschissler, thanks for your answer anyway. i will describe it a litte bit more percise.

I have following code: A Model User which has normal data like username, email,... and 3 other relations.

class User extends Model
{

    /** Initialize method for model.*/
    public function initialize()
    {
        $this->setSource($this->tableName);
        $this->hasMany('id', __NAMESPACE__ . '\UserRole', 'authuserentity_id', array('alias' => 'UserRoles', 'foreignKey' => array('action' => Relation::ACTION_CASCADE)));
        $this->hasMany('id', __NAMESPACE__ . '\Option', 'user_id', array('alias' => 'Options', 'foreignKey' => array('action' => Relation::ACTION_CASCADE)));
        $this->hasMany('id', __NAMESPACE__ . '\Protocol', 'user_id', array('alias' => 'Protocols', 'foreignKey' => array('action' => Relation::ACTION_CASCADE)));

    }
    ....

so if i am using

$model = User::find();
foreach($model as $user){
    $options = $user->getOptions();
}

i will receive the Options.

now i just wanna have the field for example id of the User Model and only the field value of the Option Model. but ther i dont know how to add the columns from the relations. I dont think i need to add a extra join because the relation is already defined!?

$model = User::find(array('columns'=>'id,?value?'));

thanks for your time. pat

edited Dec '15

thanks dschissler with phql i already tried it and it is working. but i was just wondering if its also somehow possible over the normal "find" function. tried already many ways but with everything i got the error that the module or alias could not be found.

but in that case i will switch to phql :)

Well there is no something like partial model in phalcon orm like in doctrine.