Cphalcon: When the model uses `columns()`, it should bind the original model instead of `Phalcon\Mvc\Model\Row`

Created on 14 Jun 2019  Â·  6Comments  Â·  Source: phalcon/cphalcon

You need to select full model like without columns option. When you are selecting model with columns then always Phalcon\Mvc\Model\Row is returned. It's not a bug.

_Originally posted by @Jurigag in https://github.com/phalcon/cphalcon/issues/12926#issuecomment-310889268_

5.0 new feature request transfer

Most helpful comment

I'm not saying we shouldn't implement this, im saying that this was a choice which was made to have Row object instead of full model. I guess on data mapper we can more safely implement this, but as im saying - it's not a bug, this current behavior on 3.4.x will stay as it is. Also active record on 4.x most likely too since data mapper will be separate thing as far as i know. @niden

All 6 comments

We just don't have partial models, i guess if we implement Data Mapper then we can consider this @niden

Model query in laravel:

User::select('id', 'name', 'email')->get();

return

Collection {#379 â–¼
  #items: array:2 [â–¼
    0 => User {#380 â–¼
      #fillable: array:3 [â–¶]
      #hidden: array:2 [â–¶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:3 [â–¶]
      #original: array:3 [â–¶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [â–¶]
      #rememberTokenName: "remember_token"
    }
    1 => User {#381 â–¶}
  ]
}

When using DB query:

       DB::table('users')
            ->select('users.id', 'users.name', 'blogs.text')
            ->join('blogs', 'users.id', '=', 'blogs.id')
            ->get()

return

Collection {#339 â–¼
  #items: array:2 [â–¼
    0 => {#344 â–¼
      +"id": 1
      +"name": "sowork"
      +"text": "first article"
    }
    1 => {#338 â–¶}
  ]
}

It would be strange to suddenly return a Phalcon\Mvc\Model\Row

It's not suddenly, it's expected behavior and not a bug.

This is due to all active record events we have - to make sure that they work correctly and do what supposed to do they require full models to be returned.

When using a query, sometimes I only want to query the fields I need, instead of having to query all the fields.

When I use graphql+eagerLoad for preloading, I need to get the association based on the returned model, but if I return Phalcon\Mvc\Model\Row it can't be processed.

I'm not saying we shouldn't implement this, im saying that this was a choice which was made to have Row object instead of full model. I guess on data mapper we can more safely implement this, but as im saying - it's not a bug, this current behavior on 3.4.x will stay as it is. Also active record on 4.x most likely too since data mapper will be separate thing as far as i know. @niden

This can come with the Data Mapper implementation. I don't have any issues with returning the original model or a Row object

Was this page helpful?
0 / 5 - 0 ratings