We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Interesting model row

I have a table :

Table: user_points
Columns:
id int(11) AI PK 
point tinyint(4) 
create_date datetime 
user_id int(11) 
post_id int(11) 
comment_id int(11) 
post_rating_id int(11) 
article_rating_id int(11)

I would like to sum the points and write it out to the screen, with the user_name. The user_name come from the users table... I create 2 different query for this, and i got an interesting result. The 2 query is the following:

return \Baseapp\Models\UserPoints::find(array('columns'=>'*, sum(point) AS sumatory', 'conditions'=>'create_date >= :date_from: AND create_date <= :date_to:', 'bind'=>array('date_from'=>$this->date_from,'date_to'=>$this->date_to), 'group'=>'user_id', 'order'=>'sumatory DESC', 'limit'=>20));
return $this->getDi()->get('modelsManager')->createQuery('SELECT *,sum(point) AS sumatory FROM \Baseapp\Models\UserPoints WHERE create_date >= :date_from: AND create_date <= :date_to: GROUP BY user_id ORDER BY sumatory DESC LIMIT 20')->execute(array('date_from'=>$this->date_from,'date_to'=>$this->date_to));

After this, the resultset is the so annoying for me:

Object Phalcon\Mvc\Model\Row (
  ->article_rating_id = Object Baseapp\Models\UserPoints extends Baseapp\Models\BaseModel (
    ->id = Numeric string (1) "1"
    ->point = Numeric string (1) "2"
    ->create_date = String (19) "2014-09-22 12:15:29"
    ->user_id = Numeric string (1) "1"
    ->post_id = NULL
    ->comment_id = NULL
    ->post_rating_id = Numeric string (2) "17"
    ->article_rating_id = NULL
    Baseapp\Models\UserPoints methods: 65 (
      ->initialize(); 
.....
->sumatory = Numeric string (2) "23"
  Phalcon\Mvc\Model\Row methods: 7 (
    ->setDirtyState();
    ->offsetExists();
    ->offsetGet();
    ->offsetSet();
    ->offsetUnset();
    ->toArray();
    ->count();
  )
)

You can see that, the last table field is got the full Model object, why the last filed, why not the model model name?



98.9k

This calculation:

sum(point) AS sumatory

It doesn't have a related model so it is represented as a Phalcon\Mvc\Model\Row instance



2.0k

Its okey, but why the last field got the real model?

eg: I try in this way its a bit better:

return \Baseapp\Models\UserPoints::find(array('columns'=>'[Baseapp\Models\UserPoints].*, sum(point) AS sumatory', 'conditions'=>'create_date >= :date_from: AND create_date <= :date_to:', 'bind'=>array('date_from'=>$this->date_from,'date_to'=>$this->date_to), 'group'=>'user_id', 'order'=>'sumatory DESC', 'limit'=>20));

The result is:

Object Phalcon\Mvc\Model\Row (
  ->baseapp\Models\UserPoints = Object Baseapp\Models\UserPoints
  ....
->sumatory = Numeric string (2) "23"
  Phalcon\Mvc\Model\Row methods: 7 (

This calculation:

sum(point) AS sumatory

It doesn't have a related model so it is represented as a Phalcon\Mvc\Model\Row instance