I'm running into an issue using the idiORM class. (documentation found here: http://idiorm.readthedocs.org/en/latest/)
I'm trying to get the count of a certain query while keeping the original query for further processing, so I simply copied the original query into a second variable but it seems that as soon as I execute the count()
method he copies everything to the other variable.
For example I create a query and save the returned ORM object into a variable $ormResults
and copy it into $copy
:
$ormResults = ORM::for_table('department')->where('company', '4');
$this->_orginalResults = $ormResults;
// Copy the ORM object into $copy
$copy = $ormResults;
$this->resultCount = $copy->count();
Up until here it works fine, the expected count result is correctly stored in $this->resultCount
. However when I now var dump the (up until now) unused variable $this->_orginalResults
it also contains the count()
properties which baffles me as I have not used it at all.
protected '_resultColumns' =>
array
0 => string 'COUNT(*) AS `count`' (length=19)
Which causes trouble when I try to execute $this->_originalResults->findMany();
.
Is this because the count()
method returns the ORM object? As far as I know PHP code doesn't travel upwards.. does it?
So Tl;dr:
$test = ORM::forTable('departments')->where('company', '8');
$test2 = $test;
// Works
var_dump($test->count());
// Fails
var_dump($test2->findMany());
However this works perfectly fine:
$test = ORM::forTable('departments')->where('company', '8');
$test2 = ORM::forTable('departments')->where('company', '8');
var_dump($test->count());
var_dump($test2->findMany());
Any insight / help would be greatly appreciated.
Ok figured it out, apparantely some of the static variables are ruining it.
using clone
to copy the object works perfectly.
$test = ORM::forTable('departments')->where('company', '8');
$test2 = clone $test;
var_dump($test->count());
var_dump($test2->findMany());
Does the trick