Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assoc with null values #457

Merged
merged 1 commit into from
Mar 20, 2014
Merged

Conversation

uwej711
Copy link
Contributor

@uwej711 uwej711 commented Mar 19, 2014

See my corresponding message on symfony-cmf-devs.

Feedback welcome.

@ElectricMaxxx
Copy link
Contributor

👍
super i like that cause just in that moment i need that :-)

$prepared = $this->processAssoc($value);
$node->setProperty($mapping['assoc'], $prepared['keys'], PropertyType::STRING);
$node->setProperty($mapping['assocNulls'], $prepared['nulls'], PropertyType::STRING);
$value = $prepared['values']; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cs issue with the closing parenthesis.

@dbu
Copy link
Member

dbu commented Mar 19, 2014

interesting, i think this will work. i guess it is also BC in the sense that previously stored hashmaps without any null values still work. did we not implement an exception for when one value is null?

@uwej711
Copy link
Contributor Author

uwej711 commented Mar 19, 2014

The exception came from phpcr when trying to convert the null value to a string - which is another issue in Jackalope: it should not store null values in multivalued properties but not throw an exception either.

@@ -509,7 +509,7 @@ public function testAssocProperty()
{
$user = new User();
$user->username = "test";
$assocArray = array('foo' => 'bar', 'ding' => 'dong');
$assocArray = array('foo' => 'bar', 'ding' => 'dong', 'dong' => null, 'dang' => null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my question was what happens when we have 'dang' => 'buzz' - but it will work too so its ok like this.

@dbu
Copy link
Member

dbu commented Mar 19, 2014

thanks, looking good. @lsmith77 can you have a look too if you see a problem?

@uwej711 can you please open an issue on jackalope about the null values in multivalue, so we can discuss the topic there?

protected function processAssoc(NodeInterface $node, array $mapping, array $assoc)
{
$keys = array_keys($assoc);
$values = array_values(array_map(function ($item) { return null === $item ? '<null>' : $item; }, $assoc));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think some whitespace in the two closures would help readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will break the lines ...

@lsmith77
Copy link
Member

hmm why do we even need to store <null> ? with another array we should be able to store the null keys and use this information to add the nulls into the value array before we combine.

@dbu
Copy link
Member

dbu commented Mar 19, 2014

@lsmith77 the point is that the order of the fields needs to be
preserved. so we have to store something non-empty as a placeholder.

@lsmith77
Copy link
Member

i dont think that this is necessary. if have 3 arrays: 1) ordered list of keys 2) ordered list of values without nulls 3) ordered list of keys with a null value.

i can then iterate over the list of keys, create a new array where i check if the key is in the list of null values, if so i use null, otherwise i pop off first value of the list of values.

@dbu
Copy link
Member

dbu commented Mar 19, 2014

true. i think that sounds slightly more elegant. i think querying on those things is a PITA anyways - and when you look for null values you should check if $property . Null field is non-empty, not in the values.

@uwej711
Copy link
Contributor Author

uwej711 commented Mar 19, 2014

Right, I thought about Lukas proposal first but decided to start with a simpler version. Anyway I will give it a try ...

@uwej711
Copy link
Contributor Author

uwej711 commented Mar 19, 2014

Done ...

reset($values);
$result = array();
foreach ($keys as $key) {
if (in_array($key, $nulls)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could flip the $nulls array and then do an isset check instead of in_array. if somebody has a larger array with many nulls, that will be faster.

@dbu
Copy link
Member

dbu commented Mar 20, 2014

thanks uwe. i added one comment, but its not critical to me. can you squash the commets please, then i will merge.

@dbu
Copy link
Member

dbu commented Mar 20, 2014

jackalope/jackalope#210 would tend to remove null keys rather than keep them. but i think its more natural to do it this way for hashmaps. people just need to be aware that when using non-assoc multivalue, null will be removed. (or will they?)

@uwej711
Copy link
Contributor Author

uwej711 commented Mar 20, 2014

squashed ...

dbu added a commit that referenced this pull request Mar 20, 2014
@dbu dbu merged commit 2c0f6be into doctrine:master Mar 20, 2014
@dbu
Copy link
Member

dbu commented Mar 20, 2014

thanks uwe!

@lsmith77
Copy link
Member

added a little fix to deal with older data structures 22d03a8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants