Custom model properties are not being set for result set rows when using Model::find()

Hello, In my app I use custom sql query builder and

$result = new MyModel();
$results = new \Phalcon\Mvc\Model\Resultset\Simple(null, $result, $result->getReadConnection()->query($sql));

to get results in one case and

$results = MyModel::find([ some category parameters])

in another case. Columns are the same in both cases and I use the same partial to display results. I have some constant public properties in Model class declaration.

class MyModel extends BaseModel {
public $property_1="MyProp1";
public $property_2="MyProp2";
....;

In first case I can access my properties from each resultset row ( what I need to display my results properlly) , in second case I have only database columns-properties and seems like no model instance has been created. Maybe it's normal behaviour but how do I set my properties and make them accessible in second scenario then?

Well if you selecting some columns then its normal behaviour i think. You have to selecing full objects, maybe some things with hydration in ::find to return full objects even when partial ?



6.7k
edited Nov '15

Hi, Jurigag. Thanks for your response. In first case I also only selecting few columns. I mean if it's normal functioning for resultset shouldn't it be applied for all objects of this class no matter how I've received it? I've tried to set different hydration modes inside ::find() it doesn't seem to affect anything in resultset.



6.7k
edited Nov '15
var_dump($results->getFirst());

returns object of MyModel class in first case and stdClass in case of MyModel::find() resultset.

If I change hydration mode it only changes the name of encapsulating class, for instance:

$results = MyModel::find(['hydration' => Resultset::HYDRATE_OBJECTS,
                                        ...other params here...
                                       ]);

change to HYDRATE_ARRAYS changes StdClass to Array with completely same fields.

object(stdClass)[146]
  public 'id' => string '1932' (length=4)
  public 'part_number' => string '*******' (length=7)
  public 'part_description' => string 'Blah-blah-blah ...' (length=116)
  ...other columns...
array (size=5)
  'id' => string '1932' (length=4)
  'part_number' => string '*******' (length=7)
  'part_description' => string 'Blah-blah-blah ...' (length=116)
  ...other columns...

How can I get down to MyModel objcect from resultset I've got with MyModel::find() without having to select all columns (I have a bunch of them)?

HYDRATE_RECORDS dont work too ? Then i have no idea, you can select all columns, it doesnt cost so much, maybe try add issue on github cuz it seems like a bug.



6.7k
edited Nov '15

Yep, and HYDRATE_RECORDS also returnsobject(Phalcon\Mvc\Model\Row)[139] instead of my model's instance. I've figured that find() and findFirst() methods return instance of Model class only if I haven't set columns parameter inside these methods. Not sure if this is a bug, but I think I will try to figure it on github.



6.7k

Ok. That's my first issue on github, hope it's issued correctly. https://github.com/phalcon/cphalcon/issues/11096

edited Nov '15

No thats a good behaviour cuz it shouldnt return full object, cuz you are not selecting full object and Simple/Row is just simpler objects which you can iterate in loop etc. But it should still return properties of this class.

You can always create new model using ->assign(or constructor) after fetching not full data.



6.7k
edited Nov '15

You are righ, incomplete objects are mentioned in docs. Still I think it would be handy, if I could get public and static properties which I have set for Model from any resultset, maybe even without a need to get down to particular row.

I've replaced model find() with sql query (I had already had custom query builder anyway). But I think assign() will do too.