Getting MongoId from PHP object returns NULL - php

I'm a bit stumped here.
I've upgraded an Ubuntu 13.10 dev machine to PHP 5.5.3 with the PECL Mongo driver 1.5.5.
Previously on 5.3.6 with 1.3.2.
All of my objects store their MongoId() on $class->_id;
Randomly, I'm getting that class variable now return as NULL, such as:
$log = new EmailEventLog;
$log->data = $event;
$log->email = $email->_id;
$log->save();
print_r(var_dump($log)); // contains correct ->_id variable
print_r(var_dump($log->_id)); // NULL
Nothing happens in between those two printouts, but I cannot access it!
FYI the class EmailEventLog extends M which has a public $_id; set on it. I've tried bringing that up to the natural class and it's not working still.
Thanks
EDIT - weirdly, closing the object and then getting the _id from the clone returns it as expected!

Related

Declaration of Child method should be compatible to Parent Method

got this error on Yii2. I dont know what exactly the problem is. I just migrate my source code from Windows to Mac OS. I tried cloning my whole project still the error appears.
Declaration of common\models\Product::getAttributes() should be compatible with yii\base\Model::getAttributes($names = NULL, $except = Array)
These are the things i tried:
Clone the whole project to Mac OS. - error above exists.
Clone the whole project to another windows machine.-the project is running well.
I am using yii2 in this project.
As you can see from the error message, you overrided yii\base\Model getAttributes() method. common\models\Product is extended from yii\db\ActiveRecord and ActiveRecord is extended from yii\base\Model.
If you really want to override this method, list all parameters (see here), it's easier to do with help of IDE. And by the way this is PHP feature and has nothing to do with OS or Yii2.
If it is your custom method for another purposes, you need to rename it in order to resolve conflict.
Your method must accept the same parameters ($names = NULL, $except = Array)

Mongo-PHP - MongoCursor exception with MongoDB PHP Driver v1.6

I'm having troubles with PHP MongoCursor since I upgraded to Mongo PHP Driver from 1.5.8 to 1.6.0
The following code works well with version 1.5.8, but crashes with version 1.6
PHP version is 5.5.21., Apache version is Apache/2.4.10 (Ubuntu)
$mongoClient = new \MongoClient($serverUrl, ['readPreference'=>\MongoClient::RP_NEAREST]);
$database = $mongoClient->selectDB($dbName);
$collection = $database->selectCollection($collectionName);
// count() works fine and returns the right nb on documents
echo '<br/>count returned '.$collection->count();
// find() exectues with no error...
$cursor = $collection->find();
$documents = [];
// ...and hasNext() crashes with the Excetion below
while($cursor->hasNext()){$documents[] = $cursor->getNext();}
return $documents;
And so the hasNext() call crashes with this message :
CRITICAL: MongoException: The MongoCursor object has not been correctly initialized by its constructor (uncaught exception)...
Am I doing something wrong ?
Thanks for you help !
This may be related to a bug that was introduced in 1.6.0 regarding iteration with hasNext() and getNext(): PHP-1382. A fix has since been merged to the v1.6 branch and should be released later this week as 1.6.1.
That said, the bug regarding hasNext() was actually that the last document in the result set would be missed while iterating. If I run your original script against 1.6.0, the array contains a null value as its last element. With the fix in place, the array will contain all documents as is expected. I cannot reproduce the exception you're seeing with either version.
That exception is actually thrown from an internal checks on the C data structures, to ensure that the cursor object is properly associated with a MongoClient and socket connection. See the MONGO_CHECK_INITIALIZED() macro calls in this file. Most all of the cursor methods check that a MongoClient is associated, but hasNext() is unique in that it also checks for the socket object (I believe other methods just assume a cursor with a MongoClient also has a socket). If that exception is truly reproducible for you and you're willing to do some debugging with the extension, I'd be very interested to know which of the two checks is throwing the error.
As a side note, you should also be specifying the "replicaSet" option when constructing MongoClient. This should have the replica set name, which ensures that the driver can properly ignore connections to hosts that are not a member of the intended replica set.
I just encountered the same issue; I refactored my code to use the cursor iterator instead, ie:
foreach( $cursor as $doc ) {
$documents[] = $doc;
}
I was looking for a code example of how to implement a tailable cursor and found this question. The following code is a simple example of a tailable cursor (via the $cursor variable) which you provide on a capped mongodb collection.
$cursor->tailable(true);
$cursor->awaitData(true);
while (true) {
if ($cursor->hasNext()) {
var_dump($cursor->getNext());
} else {
if ($cursor->dead()) {
break;
}
}
}

laravel 'class not found' error on production

Slightly odd one here.
I have Persons and Actions. Persons can have many Actions, while each Action belongs to only one Person. I'm using Chumper's Datatables to display a list of people, including a count of their actions.
Since migrating to a production (forge) server, I'm getting
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'action' not found
when calling the datatable. The error shown
/­vendor/­laravel/­framework/­src/­Illuminate/­Database/­Eloquent/­Model.php:721
public function hasOne($related, $foreignKey = null, $localKey = null)
{
$foreignKey = $foreignKey ?: $this->getForeignKey();
$instance = new $related;
$localKey = $localKey ?: $this->getKeyName();
suggests it's a problem with my hasMany relationship:
# /models/Person.php
class Person extends Eloquent {
public function actions()
{
return $this->hasMany('Action');
}
# /models/Action.php
class Action extends Eloquent {
public function person()
{
return $this->belongsTo('Person', 'person_id');
}
I assume these are fine, however, as it all works locally. Datatables also works fine elsewhere, calling through other items and their related actions with no trouble.
I've tried composer dump-autoload and artisan dump-autoload on the production server, to no avail. The deployment script on forge is below:
git pull origin master
composer install
php artisan migrate --env=production
I can't tell if it's a config issue, a database issue, a code issue or something else entirely. I've been back through the many similar questions but nothing's jumped out. Any help much appreciated.
for who may have the same problem, triple check the casing of your model! I had it wrong, that's why locally on mac was working but not on the server
So I think I'd borked this one myself.
I'd been lazy and left function datatablePersons() in PersonsController.php using an old 'count' method, relying on long-defunct relationships (that caused n+1, so had to be binned), hence it wobbling over an actions class whenever that relationship was called upon.
Datatable functions in other controllers (with a cleaner 'count' method) work fine, so I've just rewritten datatablePersons() to use the same method.
I've not quite got the query right (in eloquent, at least) yet - see this question here: mysql join ON and AND to laravel eloquent - but the class not found error has certainly gone away.
I'm (massively) guessing that the classmap on the local machine hadn't been flushed since whatever was removed was removed, while the production machine is rebuilt every push, hence the disparity...?
Either way, it's no longer an issue.

PHP variable loses its value

I have a really serious problem that I have not seen before.
On a website we are using opensource SQC eshop, PHP Version 5.3.3-7+squeeze15 and there is some kind of problem with variable memory I think.
SQC uses notORM and here the problem starts with fatal error "Call to function on non object notORMResult" .
So I dug deeper and found the constructor of NotORM that looks like this:
function __construct(PDO $connection, NotORM_Structure $structure = null,NotORM_Cache $cache = null) {
$this->connection = $connection;
if($_GET['test']){
var_dump($structure);
}
if (!isset($structure)) {
$structure = new NotORM_Structure_Convention;
}
if($_GET['test']){
var_dump($structure);
}
$this->structure = $structure;
if($_GET['test']){
var_dump($this->structure);
exit("1");
}
$this->cache = $cache;
}
And so the output is NULL because the constructor gets no structure param so we create an object. Second output is the object. Then we set the object to attribute and then the THIRD OUTPUT IS NULL
How is this even possible? The site was running for about year and half and no problems till yesterday. I didn't made yet any updates to php and this thing really freaks me out 'cause it's not a constant problem. It just happens sometimes after 2 hours, sometimes after 2 mins and I have really no idea why is this happening.
And btw ... this is just the start it happens across the whole script. Object attributes are set but when you want to read them they give you NULL. There is also second website running on the same server, same php same configuration without problem.
Thanks for any ideas :)

Why can't I declare an instance variable as an array of objects in PHP?

I am writing a customer management system in PHP, for use offline (i.e. on the clients computer). I have considered using Java or C# but have come to the conclusion that it is easier to let the browser do all the layout for me, and just have the company install wamp on their computers.
Through this interface they will also be able to manage Agents (i.e. salesmen that go round their area getting orders for the company, in case any doesn't know). This is the section I will use in this post to demonstrate the problem I am having.
Basically I have 4 classes - AgentPages, AgentList, AgentDetails and AgentForm. AgentForm will have two modes - edit and new. AgentPages has a function called getPages, which returns an array of instances of the other 3 classes. However it does not like the "new" keyword.
My code is as follows (for the AgentPages class only):
<?php
require_once("AgentList.php");
require_once("AgentDetails.php");
require_once("AgentForm.php");
class AgentPages {
public function __construct() {
echo "Constructed";
}
private $pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
function getPages() {
return $this->pages;
}
}
?>
I am using the netbeans 6.9 IDE with PHP enabled, and (as you can probably guess) I have wamp server installed. Under PHP version 5.3 the netbeans debugger is telling me that "Parse error: parse error in C:\wamp\www\CustomerApp_v2\Agents\AgentPages.php on line 20". Under 5.2.11 it says something about unexpected T_NEW on that line. I have cut out a large comment on this, before line 20, but I can tell you that line 20 is the declaration of $pages. I have an empty constructor for each class at the moment.
I have also tried the following line instead of line 20:
$AgentList = new AgentList();
This doesnt work either - I get the same error. According to all the tutorials I have looked at there is nothing wrong with my code - I am probably just overlooking something obvious though.
Does anyone have any idea what I am doing wrong? I have done lots of PHP object oriented stuff before now, but the last time I touched it was 2 years ago.
Thanks in advance.
Regards,
Richard
The problem is that you're trying to initialize an instance variable in a declaration with an expression (a call to new is an expression). That doesn't work. Put the assignment in the constructor and it will work.
Like this:
class AgentPages {
public function __construct() {
$this->pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
echo "Constructed";
}
private $pages;
}

Categories