Why does Doctrine\ORM\Configuration's "DoctrineProxies" Object contain the Universe? - php

In my ORM code I have an Entity with a field fined like so:
//part of entity class Item:
/** #Column(name="product_id", type="integer") */
private $productId;
I then executed this code:
//3 lines ~straight out of Doctrine configuration to get EntityManager
include 'config/doctrine-config.php';
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$em = EntityManager::create($dbParams, $config);
//my own code to retrieve an entity instance:
$instance = $em->find(Item::class, 2);
print_r($instance);
And this is the output I get (skipping few other similar properties):
Application\Entity\Item Object
(
[id:Application\Entity\Item:private] => 2
[description:Application\Entity\Item:private] => Product Kit
[productId:Application\Entity\Item:private] => -1
)
Note how there are 6 (six) lines above that came out of print_r() function.
And everything was fine, Until
Next, I have changed the $productId column to ManyToOne Relationship on my Item Entity class, like so:
/**
* #ManyToOne(targetEntity="Product", inversedBy="id")
* #JoinColumn(name="product_id", referencedColumnName="id")
*/
private $productId;
I ran the same code.
OUT CAME THE UNIVERSE OF 2,392,600 LINES, WHAT?
Two million, three hundred and ninety two thousand, six hundred lines lines of print_r output.
looking at the print-out I see that DoctrineProxies\__CG__\Application\Entity\Product Object contains 2,392,564 lines printed by print_r
Question:
What is exactly in this object and why is it so big as to take up nearly 300Mb of disk space when printed out?
I cannot help but wonder if such complexity is apt to cause performance issues in every-day code. For example, I am not printing out the contents of the $instance variable in my every-day code, but I surely return the humongousness from a method call. Does that mean it is a 300Mb variable that gets passed from i.e. the $em->find(Item::class, 2); call above?
(Very) Partial Listing
Application\Entity\Item Object
(
[id:Application\Entity\Item:private] => 2
[description:Application\Entity\Item:private] => Product Kit
[ProductId:Application\Entity\Item:private] => DoctrineProxies\__CG__\Application\Entity\Product Object
(
[__initializer__] => Closure Object
(
[static] => Array
(
[entityPersister] => Doctrine\ORM\Persisters\Entity\BasicEntityPersister Object
(
[class:protected] => Doctrine\ORM\Mapping\ClassMetadata Object
(
[name] => Application\Entity\Product
[namespace] => Application\Entity
[rootEntityName] => Application\Entity\Product
[inheritanceType] => 1
[generatorType] => 5
[fieldMappings] => Array
(
[id] => Array
(
[fieldName] => id
[type] => integer
[scale] => 0
[length] =>
[unique] =>
[nullable] =>
[precision] => 0
[columnName] => id
[id] => 1
)
[fieldNames] => Array
(
[id] => id
[description] => description
)
[columnNames] => Array
(
[id] => id
[description] => description
)
[idGenerator] => Doctrine\ORM\Id\AssignedGenerator Object
[reflClass] => ReflectionClass Object
(
[name] => Application\Entity\Product
)
[namingStrategy:protected] => Doctrine\ORM\Mapping\DefaultNamingStrategy Object
[instantiator:Doctrine\ORM\Mapping\ClassMetadataInfo:private] => Doctrine\Instantiator\Instantiator Object
)
[conn:protected] => Doctrine\DBAL\Connection Object
(
[_conn:protected] => Doctrine\DBAL\Driver\PDOConnection Object
(
)
[_config:protected] => Doctrine\ORM\Configuration Object
(
[_attributes:protected] => Array
(
[metadataCacheImpl] => Doctrine\Common\Cache\ArrayCache Object
(
[data:Doctrine\Common\Cache\ArrayCache:private] => Array
(
[dc2_b1e855bc8c5c80316087e39e6c34bc26_[Application\Entity\Item$CLASSMETADATA][1]] => Array
(
[0] => Doctrine\ORM\Mapping\ClassMetadata Object
(
[name] => Application\Entity\Item
[namespace] => Application\Entity
[rootEntityName] => Application\Entity\Item
[customGeneratorDefinition] =>
[customRepositoryClassName] =>
[isMappedSuperclass] =>
[isEmbeddedClass] =>
[parentClasses] => Array
[BAZILLION LINES redacted for brevity]

You can't dump a proxy object without XDebug or similar tools (which limit the dumped object size).
The problem is really, really simple:
Proxy -> references EntityManager -> references UnitOfWork -> contains Proxy
This obviously leads to a recursive data-structure dump, which in turn leads to a mess any time you try to dump it without sensible limits.

DoctrineProxies\__CG__\Application\Entity\Product
is a proxy class... which means that doctrine doesn't actually fetch the entity from the database (for performance) unless it is needed (i.e. calling $product->getName() those proxy Classes are in a recursive loop with eachother and are VERY large as you saw... most of the information there you don't really need unless you are diving deep ... you should never use print_r ... in the new symfony 2.7+ i think there is a function called dump() in debug mode ... if you use that to print the entity it has loop protection and it just shows reference numbers ... you can also use the \Doctrine\Common\Util\Debug::dump() that also will print out a smaller list than 2^234234234 lines ...

Related

PHP read first item in object array

I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);

Getting values nested in multiple arrays

I get the response below from a script, and I need to extract board, model, serial number, current and upgrade.
Net\Response Object
(
[unrecognizedWords:protected] => Array
(
)
[_type:Net\Response:private] => !re
[attributes:protected] => Array
(
[board] => true
[model] => CRD
[serialnumber] => XXXXXXXX
[current] => 5.11
[upgrade] => 5.11
)
[_tag:Net\Message:private] =>
)
I've multiple combinations such as this:
echo unrecognizedWords:protected->attributes:protected->board;
However I can not get the values our of the array.
Can someone help me?
You cann't get any protected or private properties of object (only public). Use public methods (getters) of this object (sure, that it's provided).
Read: PHP: Visibility

Json Encode Issues with Objects

array(
[key] => Array
(
[229] => Tuple Object
(
[ID] => 1
[NAME] => abc
)
[233] => Tuple Object
(
[ID] => 2
[NAME] => abcd
)
...
...
...
)
[moreinfo] = 'xyz'
)
issue is when i am using json_encode tuple object is getting lost as all data types like id,name are private.
Following Solution i have found
change private variables to public, still i can access the variables as array instead of objects :-(
write a wrapper which convert object to array...... an extra effort
can any one suggest a solution in which i can use objects.

joomla 1.6 : how $app object print out the entire page contents

I was checking joomla 1.6 index.php and I found the following code at the last line
echo $app;
this prints the entire page contents.
I just printed out the contents in this object using print_r() and I got the following details
JSite Object
(
[template:JSite:private] => stdClass Object
(
[id] => 6
[home] => 1
[template] => beez5
[params] => JRegistry Object
(
[data:protected] => stdClass Object
(
[wrapperSmall] => 53
[wrapperLarge] => 72
[logo] => images/sampledata/fruitshop/fruits.gif
[sitetitle] => Matuna Market
[sitedescription] => Fruit Shop Sample Site
[navposition] => left
[html5] => 0
)
)
)
[_language_filter:JSite:private] =>
[_detect_browser:JSite:private] =>
[_clientId:protected] => 0
[_messageQueue:protected] => Array
(
)
[_name:protected] => site
[scope] =>
[requestTime] => 2011-10-17 17:23
[startTime] => 1318872200.5365
[_errors:protected] => Array
(
)
)
so how echo $app display all the site contents, it doesn't contains any HTML contents in the object.
Thank you very much
It declares the magic method __toString() in the class.
If this function is declared in a class, the return value of it will be used when the object is casted to a string.
Simple example: http://codepad.org/UmZUQA3v
$app is an object, and print_r accesses its values in different ways from echo. When echo is called, it also implicitly calls the magic __toString method. That has been defined such that it returns a string with the page contents, given the values stored inside of the object. print_r will give you those values, but not the __toString representation.

Modifying Model data in beforeSave of Behavior when using saveAll

I'm trying to write a Meta behavior for a project I am working on that will allow me to assign custom variables/attributes to a model, similar to how you can create custom fields in a wordpress post.
I have created a Meta behavior that binds the meta model to the model it is acting on and also has a beforeSave callback that loops through the models data variable and puts the model name into the meta array.
Everything is saving but when I check the database the model field is coming back empty.
The database structure for the Meta is
id - A unique if for the meta
model - The name of the model that this meta entry is associated with
foreign_id - The id of the above model that this meta entry is associated with
key - Name of the meta variable
value - Value of the meta variable
The data that comes to the saveAll function from the form is
Array
(
[Page] => Array
(
[id] => 12
[name] => Test
[slug] => a/b/c/d
[layout] => 0
[body] => Test multilevel
)
[Meta] => Array
(
[0] => Array
(
[id] => 1
[key] => page_title
[value] => About Us
)
[1] => Array
(
[id] => 6
[key] => test4
[value] => test
)
[2] => Array
(
[key] => test3
[value] => lala
)
)
)
and after it has run through the behavior beforeSave it is
Array
(
[Page] => Array
(
[id] => 12
[name] => Test
[slug] => a/b/c/d
[layout] => 0
[body] => Test multilevel
[modified] => 2010-05-04 15:56:54
)
[Meta] => Array
(
[0] => Array
(
[id] => 1
[key] => page_title
[value] => About Us
[model] => Page
)
[1] => Array
(
[id] => 6
[key] => test4
[value] => test
[model] => Page
)
[2] => Array
(
[key] => test3
[value] => lala
[model] => Page
)
)
)
The code from the behavior is
<?php
/**
* Meta Model Behavior
*
* Adds custom variables to models
*
**/
class MetaBehavior extends ModelBehavior {
/**
* Contains configuration settings for use with individual model objects.
* Individual model settings should be stored as an associative array,
* keyed off of the model name.
*
* #var array
* #access public
* #see Model::$alias
*/
var $__settings = array();
var $__defaults = array(
'class' => 'Meta',
'foreign_key' => 'foreign_id',
'dependent' => true,
'auto_bind' => true
);
/**
* Initiate Meta Behavior
*
* #param object $model
* #param array $config
* #return void
* #access public
*/
function setup(&$model, $settings = array()) {
$default = $this->__defaults;
$default['conditions'] = array('Meta.model' => $model->alias);
if (!isset($this->__settings[$model->alias])) {
$this->__settings[$model->alias] = $default;
}
$this->__settings[$model->alias] = array_merge($this->__settings[$model->alias], ife(is_array($settings), $settings, array()));
if ($this->__settings[$model->alias]['auto_bind']) {
$hasManyMeta = array(
'Meta' => array(
'className' => $this->__settings[$model->alias]['class'],
'foreignKey' => $this->__settings[$model->alias]['foreign_key'],
'dependent' => $this->__settings[$model->alias]['dependent'],
'conditions' => $this->__settings[$model->alias]['conditions']
)
);
$metaBelongsTo = array(
$model->alias => array(
'className' => $model->alias,
'foreignKey' => $this->__settings[$model->alias]['foreign_key']
)
);
$model->bindModel(array('hasMany' => $hasManyMeta), false);
$model->Meta->bindModel(array('belongsTo' => $metaBelongsTo), false);
}
}
function beforeSave(&$model) {
foreach($model->data[$this->__settings['class']] as $key => $value) {
$model->data[$this->__settings['class']][$key]['model'] = $model->alias;
}
return true;
}
} // End of MetaBehavior
?>
I have a feeling it may be because of the relationships and the saveall is using the passed data (original) when saving the associations.
The only other way I thought of doing it would be to remove the relationships and put some code into the afterSave function of the behavior to handle the saving and then put some other code into the afterFind to retrieve them.
Any ideas?
Cheers,
Dean
I don't believe you'll have any luck with this as Model::saveAll() doesn't call beforeSave() at all. In fact, it loads associations before then calling $this->save().
Line 1652 of the Model source shows the associations being loaded prior to any real call to Model::__save() that doesn't just validate the data.
What this means is, at first glance, it looks like the magic bindModel() call in your MetaBehavior won't have any affect. To be honest, I'd not really bother with something like that as it's just simpler to set the association in your Page Model class definition.
Of course, you could:
Create an afterSave in your MetaBehavior that facilitates the saving of the Meta Model data or,
Override saveAll() in the model to add the binding and then call parent::saveAll()
Ultimately, I feel it's more sane to utilise Model::saveAll() and set a real association in your Model class definition anyway. Utilising behaviours to promote DRY practices is very noble but it doesn't mean you need to make things less trivial as part of a development methodology that you or another person follows
TLDR; You've already added Meta to your behaviors array, adding an association is only another few lines of code within the same file.

Categories