Setup:
- Drupal 7.x
- Entity API 7.x-1.0
- CER 7.x-1.x-dev
- Entity Connect 7.x-1.0-beta2
- Entity Reference 7.x-1.0
- Multiple content types but 1 acts as a nexus to the others with entity references to the others which make use of CER for back-references. Entity Connect is used to create non-existent nodes, users, etc.
Problem:
I created a nexus node (or page that acts as a central point of all CER / Entity Reference connections) and everything appeared be created okay with the only issue being the following NOTICE and WARNING messages displayed at the top of the final page:
Notice: Undefined property: stdClass::$field_server_associated_products in cer_insert() (line 45 of C:\www\foobar\sites\all\modules\cer\cer.crud.inc).
Warning: array_key_exists() expects parameter 2 to be array, null given in cer_insert() (line 45 of C:\www\foobar\sites\all\modules\cer\cer.crud.inc).
The field you see in the notice blurb is an optional field I didn't provide a value for creating the nexus node.
Here's something else worth considering...
If you have a parent node (the nexus node) and you use Entity Connect to create a child node you tried to reference with Entity Reference, you can do that without problems. You can even use Entity Connect to create grandchildren but I wonder if this somehow caused the above error?
I'm essentially doing this for multi-level, on-the-fly reference-creation of nodes and I'm worried that this might be causing CER some problems somehow. Oddly enough, when I published the nexus page, the CER back-links were created...
Any insight is appreciated and please let me know if you need more information.
Comments
Comment #1
wolf_22 commentedUpdate: I'm still looking into this issue but in the mean time, I noticed something about the PHP used on line 45 that the Notice is complaining about:
if (array_key_exists(LANGUAGE_NONE, $referenced_entity->{$keys['away_field']}) == FALSE || $referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][0]['target_id'] == NULL) {array_key_exists RETURNS a Boolean value, so the comparison Equal operator here isn't necessary. Unless I'm mistaken, it should instead be something like this:
if (!array_key_exists(LANGUAGE_NONE, $referenced_entity->{$keys['away_field']}) || is_null($referenced_entity->{$keys['away_field']}[LANGUAGE_NONE][0]['target_id'])) {Not trying to step on any toes here, just trying to help. I doubt this is causing the issue I experienced above though... I'll post more if I find anything else.
Comment #2
wolf_22 commentedDoes anyone know what caused this issue???
Comment #3
phenaproximaThis is related to http://drupal.org/node/1953660. (Might even be a duplicate?)
I have been busy writing an automated test suite for CER, and I've encountered this issue a few times myself in the course of coding those tests. Every time I got this error, it was caused by me handing the corresponding entities to CER in the wrong order. For example, I was accidentally saving the refererrer first, then the referenced entity. D'oh!
Granted, I'm not sure this is going to be very helpful to your immediate problem, but I think that you're on the right track. cer_insert() needs to be smarter about checking for required data before it starts updating corresponding references. I'm eager to clean up that function and patch it so it won't generate these errors, but I'm nervous about changing the complex CRUD components of CER without having tests in place first.
Comment #4
wolf_22 commentedThat makes sense, phenaproxima, but I'm a little curious if these two are really related... I'm still trying to get my bearings straight with all this but at one point, I think the Notice message indicated a field that didn't even exist. This is what led me to look inside the CER table to manually "adjust" which corresponding references were established.
What I did was this: for the references I did NOT want, I either deleted them entirely from the table or else edited the respective values from 1 to 0. It took some trial and error (and site backups ;.) ), but this eventually narrowed it down to which field references needed to be removed / changed. I still have some testing of my own to do with the content I have, but I think there's a functional discrepancy somewhere that's not updating the CER table correctly... (There's an inconsistency somewhere and once I have more information to shine better light on what I'm experiencing, I'll post about it here.)
I'm praying it's not because of my use of Entity Connect because I really need that...
Are you setting up tests for this or something else?
Comment #5
phenaproximaI submitted a massive patch in http://drupal.org/node/1971250, converting CER's CRUD functionality to an OO architecture. It may be worth applying that patch to see if it fixes this issue; quite honestly, I think this really may just boil down to cer.crud.inc's code being very tangled, and its implicit assumption that $referenced_entity->{$keys['away_field']} exists at all. Since the aforementioned patch refactors cer.crud.inc and (in effect) does an isset() check before looping through the field values, I don't get errors like this when running the automated test suite.
Comment #6
phenaproximaFixed in the 2.x branch, please use git to snag a copy of it for review.
Comment #7
chertzog7.x-1.x is deprecated.