Doctrine2 getResult() duplicate objects - php

I have a weird error in Doctrine 2
Repository function
$query = $this->_em->createQuery('SELECT c FROM \Model\Entity\Cluster c
WHERE c.publisherid = :publisherid');
$query->setParameter("publisherid", (int)$publisher_id);
var_dump($query->getResult());
var_dump($query->getArrayResult());
And what we have
Objects:
array (size=8)
0 =>
object(Model\Entity\Cluster)[**396**]
private 'psiteid' => int 215
private 'psiteclusterdate' =>
object(DateTime)[371]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
private 'publisherid' => int 276
private 'views' => int 14657
private 'clicks' => int 6220
...
1 =>
object(Model\Entity\Psitecluster)[**396**]
private 'psiteid' => int 215
private 'psiteclusterdate' =>
object(DateTime)[371]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
private 'publisherid' => int 276
private 'views' => int 14657
private 'clicks' => int 6220
...
Rows with key 0-3 dublicate this object (396) and other rows dublicate second object.
When i dump as array:
array (size=8)
0 =>
array (size=22)
'psiteid' => int 215
'psiteclusterdate' =>
object(DateTime)[367]
public 'date' => string '**2011-12-20** 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
'publisherid' => int 276
'views' => int 14657
'clicks' => int 6220
....
1 =>
array (size=22)
'psiteid' => int 215
'psiteclusterdate' =>
object(DateTime)[396]
public 'date' => string '2011-12-21 00:00:00' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Moscow' (length=13)
'publisherid' => int 276
'views' => int 10166
'clicks' => int 4028
All the rows are different in array. What's wrong with objects?

I solved a problem. It is a bug in Doctrine2. I created bug report here http://doctrine-project.org/jira/browse/DDC-1780
You can fix with in you project (if you can edit Doctrine library files).
Just add
if ($id[$fieldName] instanceof \DateTime) {
$id[$fieldName] = $id[$fieldName]->getTimestamp();
}`
in foreach loop.

Related

Why json_encode returns empty brackets?

var_dump of $array returns this :
array (size=3)
0 =>
object(frontend\models\Notifications)[101]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 1
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
private '_oldAttributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 1
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
private '_related' (yii\db\BaseActiveRecord) =>
array (size=0)
empty
private '_errors' (yii\base\Model) => null
private '_validators' (yii\base\Model) => null
private '_scenario' (yii\base\Model) => string 'default' (length=7)
private '_events' (yii\base\Component) =>
array (size=0)
empty
private '_behaviors' (yii\base\Component) =>
array (size=0)
empty
1 =>
object(frontend\models\Notifications)[108]
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=5)
'id' => int 2
'created_on' => string '2015-11-12 12:12:15' (length=19)
'user_id' => int 1
'text' => string '2severity level is 2guardian is 5,Student_id 2 created a Level 2 discipline issue in school' (length=91)
'is_seen' => int 0
................................
................................
................................
But the json_encode($array) returns [{}, {}, {}].
What I attempted:
Tried changing the character encoding of the whole database to utf8_general_ci.
The character encoding is utf8_general_ci for my table and so is for my 'text' column of the table. What could be the issue?
The array you show has all the properties as private. this mean that this value are not available outside their class's scope.
you can look at this SO for some suggestion
Using json_encode on objects in PHP (regardless of scope)

Grouping records by a value that is within an array in Laravel

I have created a query as follows:
$countVideosWatchedThisMonth = UserVideoView::with('video')->whereBetween('created_at', array($currentMonthStart, $currentMonthEnd))->orderBy('view_count', 'DESC')->get();
This has produced the following output from the database.
array (size=3)
0 =>
array (size=7)
'id' => int 2
'video_id' => int 11
'view_count' => int 42
'created_at' => string '2015-12-01 13:00:00' (length=19)
'updated_at' => string '2015-12-01 13:00:00' (length=19)
'deleted_at' => null
'video' =>
array (size=19)
'id' => int 11
'language_id' => int 1
'gradesubject_category_topic_id' => int 87
'tutor_id' => int 19
'title' => string 'Acceleration' (length=12)
'vimeo_id' => string '117574716' (length=9)
'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/504322261_295x166.jpg' (length=50)
'description' => string 'This video explains what acceleration is. It follows the earlier videos on distance and velocity. The intention is to give the learner a more indepth pictorial view of acceleration. ' (length=182)
'upload_status' => string 'completed' (length=9)
'count_purchased' => int 5
'count_rated' => int 0
'count_watched' => int 15
'rating_average' => null
'status' => string 'enabled' (length=7)
'published_at' => string '2015-01-23 13:10:44' (length=19)
'created_at' => string '2015-01-23 10:17:36' (length=19)
'updated_at' => string '2015-09-21 09:35:43' (length=19)
'deleted_at' => null
'slug' => string '11-acceleration' (length=15)
1 =>
array (size=7)
'id' => int 1
'video_id' => int 10
'view_count' => int 20
'created_at' => string '2015-12-04 13:00:00' (length=19)
'updated_at' => string '2015-12-04 13:00:00' (length=19)
'deleted_at' => null
'video' =>
array (size=19)
'id' => int 10
'language_id' => int 1
'gradesubject_category_topic_id' => int 83
'tutor_id' => int 19
'title' => string 'Introduction to Acids & Bases ' (length=30)
'vimeo_id' => string '117574110' (length=9)
'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/504321450_295x166.jpg' (length=50)
'description' => string 'Introduction to Acids and Bases' (length=31)
'upload_status' => string 'completed' (length=9)
'count_purchased' => int 1
'count_rated' => int 0
'count_watched' => int 1
'rating_average' => null
'status' => string 'enabled' (length=7)
'published_at' => string '2015-01-23 13:45:40' (length=19)
'created_at' => string '2015-01-23 09:43:20' (length=19)
'updated_at' => string '2015-11-07 09:01:09' (length=19)
'deleted_at' => null
'slug' => string '10-introduction-to-acids--bases-' (length=32)
2 =>
array (size=7)
'id' => int 4
'video_id' => int 47
'view_count' => int 10
'created_at' => string '2015-12-01 13:00:00' (length=19)
'updated_at' => string '2015-12-01 13:00:00' (length=19)
'deleted_at' => null
'video' =>
array (size=19)
'id' => int 47
'language_id' => int 1
'gradesubject_category_topic_id' => int 87
'tutor_id' => int 19
'title' => string 'Vectors' (length=7)
'vimeo_id' => string '118220963' (length=9)
'vimeo_thumbnail_large_url' => string 'https://i.vimeocdn.com/video/505197719_295x166.jpg' (length=50)
'description' => string 'Defining vectors' (length=16)
'upload_status' => string 'completed' (length=9)
'count_purchased' => int 0
'count_rated' => int 0
'count_watched' => int 0
'rating_average' => null
'status' => string 'enabled' (length=7)
'published_at' => string '2015-01-30 10:43:44' (length=19)
'created_at' => string '2015-01-30 07:36:34' (length=19)
'updated_at' => string '2015-02-02 07:24:25' (length=19)
'deleted_at' => null
'slug' => string '47-vectors' (length=10)
There are currently 3 records and I need to only retrieve the top values by 'gradesubject_category_topic_id', which is within an array.
I have already done the orderBy() function in order to retrieve only the first unique values.
I am now, however, unable to retrieve only the top values with a unique 'gradesubject_category_topic_id'
The output would, in this case, only retrieve the top two array elements as the last one has the same 'gradesubject_category_topic_id' as the first record in the array.
I hope this makes sense. Much appreciated.
You can do this by joining these two table.
$countVideosWatchedThisMonth = DB::table('user_video_view')
->join('video','user_video_view.video_id','=','video.id')
->whereBetween('created_at', array($currentMonthStart,$currentMonthEnd))
->orderBy('view_count', 'DESC')
->orderBt('gradesubject_category_topic_id','desc')
->first()
Or try this,
$countVideosWatchedThisMonth = DB::table('user_video_view')
->join('video','user_video_view.video_id','='.'video.id')
->whereBetween('created_at', array($currentMonthStart,$currentMonthEnd))
->orderBy('view_count', 'DESC')
->max('gradesubject_category_topic_id')

Combine Eloquent (Laravel 4.2) Results Into Multidimensional Array Based On A Value

I have this array that is returned from Laravel 4.2 Query Builder I am trying to rearrange a bit. I can't seem to figure out how to get the result I am looking for.
array (size=8)
0 =>
object(stdClass)[565]
public 'id' => int 53
public 'vehicle_id' => int 11
public 'date' => string '2015-10-27' (length=10)
1 =>
object(stdClass)[563]
public 'id' => int 55
public 'vehicle_id' => int 11
public 'date' => string '2015-10-29' (length=10)
2 =>
object(stdClass)[567]
public 'id' => int 61
public 'vehicle_id' => int 7
public 'date' => string '2015-10-30' (length=10)
3 =>
object(stdClass)[561]
public 'id' => int 63
public 'vehicle_id' => int 6
public 'date' => string '2015-10-30' (length=10)
4 =>
object(stdClass)[572]
public 'id' => int 64
public 'vehicle_id' => int 8
public 'date' => string '2015-10-30' (length=10)
5 =>
object(stdClass)[580]
public 'id' => int 65
public 'vehicle_id' => int 9
public 'date' => string '2015-10-30' (length=10)
6 =>
object(stdClass)[579]
public 'id' => int 66
public 'vehicle_id' => int 10
public 'date' => string '2015-10-30' (length=10)
7 =>
object(stdClass)[578]
public 'id' => int 57
public 'vehicle_id' => int 11
public 'date' => string '2015-10-31' (length=10)
Example of desired result:
array(
'2015-10-27' => array(11),
'2015-10-30' => array( 6, 7, 8, 9, 10 ),
'2015-10-31' => array(11)
)
I have tried foreach loops nested in for loops etc.. been driving me nuts for a while now. Am I missing something simple here?
$dates = [];
foreach($yourArray as $obj) {
$dates[$obj->date][] = $obj->vehicle_id;
}

PHP - How can I convert a ParseObject to a JSON?

I´m getting a ParseObject from a query and I need to convert it into json.
When I do a print_r($results) I get a lot of stuff but when I do $json = json_encode($results) and print it, the $json is empty.
Here is the var_dump of $results:
array (size=5)
0 =>
object(Parse\ParseObject)[12]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[15]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[15]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'mdHR3ZZo6z' (length=10)
private 'createdAt' =>
object(DateTime)[13]
public 'date' => string '2015-06-10 01:17:13' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[14]
public 'date' => string '2015-06-13 13:34:05' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
1 =>
object(Parse\ParseObject)[21]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[24]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[24]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string '3DK19BWOYY' (length=10)
private 'createdAt' =>
object(DateTime)[22]
public 'date' => string '2015-06-10 01:08:14' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[23]
public 'date' => string '2015-06-13 13:34:09' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
2 =>
object(Parse\ParseObject)[30]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[33]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[33]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'x94VIvFpdq' (length=10)
private 'createdAt' =>
object(DateTime)[31]
public 'date' => string '2015-06-10 01:07:38' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[32]
public 'date' => string '2015-06-13 13:34:11' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
3 =>
object(Parse\ParseObject)[39]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[42]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[42]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'z4vtVb9sdU' (length=10)
private 'createdAt' =>
object(DateTime)[40]
public 'date' => string '2015-06-10 01:13:05' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[41]
public 'date' => string '2015-06-13 13:34:07' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
4 =>
object(Parse\ParseObject)[48]
protected 'serverData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[51]
...
protected 'operationSet' =>
array (size=0)
empty
private 'estimatedData' =>
array (size=1)
'Usuario_FK' =>
object(Parse\ParseUser)[51]
...
private 'dataAvailability' =>
array (size=1)
'Usuario_FK' => boolean true
private 'className' => string 'Asistencia' (length=10)
private 'objectId' => string 'LRsOGB7mjZ' (length=10)
private 'createdAt' =>
object(DateTime)[49]
public 'date' => string '2015-06-10 03:13:36' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'updatedAt' =>
object(DateTime)[50]
public 'date' => string '2015-06-13 14:01:03' (length=19)
public 'timezone_type' => int 2
public 'timezone' => string 'Z' (length=1)
private 'hasBeenFetched' => boolean false
And here is after json_encode($results);
[{},{},{},{},{}]
Ideas?
Thank you for your time!

Obtaining JSON object value from array

Is there a way to get the values of a JSON object member variable from a array without looping the objects? Similar to how call_user_func_array would work but for member variables?
E.g.
array 0 =>
object(stdClass)[402]
public 'id' => int 196
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 1 =>
object(stdClass)[403]
public 'id' => int 197
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 2 =>
object(stdClass)[404]
public 'id' => int 3269
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 3 =>
object(stdClass)[405]
public 'id' => int 3334
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 4 =>
object(stdClass)[406]
public 'id' => int 5831
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 5 =>
object(stdClass)[379]
public 'id' => int 6358
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0 6 =>
object(stdClass)[400]
public 'id' => int 6592
public 'creditLimit' => string '0' (length=1)
public 'state' => int 0
In one single call I'm trying to obtain the summation of creditLimit.
Pass "true" to json_decode to get a regular array.

Categories