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

Allow Closure generators to access generated attributes #311

Closed
wants to merge 2 commits into from

Conversation

pmccarren
Copy link

No description provided.

This allows generators of the 'Closure' type to
access attributes immediately after they are
generated via the object that is passed in as
the first paramater.
@@ -502,7 +498,7 @@ public function isPendingOrSaved($object)
public function deleteSaved()
{
$exceptions = array();
foreach ($this->saved as $object) {
foreach (array_reverse($this->saved) as $object) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be more efficient for us to just add them to the starts of the saved array in the first place rather than reversing the entire array at this point?

Also, this is a change in behaviour, so should really be going into 2.2, rather than 2.1.

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't actually work in the way you expect it to. I'm pretty sure the order here is random since we're storing the objects with their hashes as keys, so php will iterate over them alphabetically by hash (so the order will appear random).

Copy link
Member

Choose a reason for hiding this comment

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

@GrahamCampbell
Copy link
Member

One failing test here:

1) SaveAndDeleteTest::testShouldThrowMultipleDeletionExceptions

Failed asserting that two strings are equal.

--- Expected

+++ Actual

@@ @@

-'OH NOES!'

+'The delete method 'delete' was not found on the model of type: 'ModelWithNoDeleteMethodStub'.'

That confirms what I said inline. This will have to go into 2.2 because of the change in behaviour.

@GrahamCampbell GrahamCampbell self-assigned this Sep 1, 2014
@GrahamCampbell GrahamCampbell modified the milestones: v2.2.0, v3.0.0 Sep 1, 2014
@GrahamCampbell
Copy link
Member

@pmccarren Can you send only your "Set the att value on the model once generated." change to the "experimental" (3.0) branch please.

@pmccarren
Copy link
Author

Sure, absolutely.

@GrahamCampbell
Copy link
Member

Great. :)

@pmccarren
Copy link
Author

Good to go! :)

@GrahamCampbell GrahamCampbell mentioned this pull request Sep 2, 2014
@pmccarren
Copy link
Author

@GrahamCampbell One possible way to remove objects in reverse order is once we create an object, we can append its objectHash (what spl_object_hash generates) to a reference array, and then when needed, simply iterate over said array in reverse order. The one check you'd have to have is to ensure that the object still exists before you attempt to delete it. Thoughts? I'm happy to prototype this too

@GrahamCampbell
Copy link
Member

Yeh, would be nice, but would lead to poor performance of the isSaved function.

I assume you mean something like this under the hood:

$saved = array(
    0 => array('our hash', object),
    1 => array('our hash', object),
    2 => array('our hash', object),
);

@GrahamCampbell
Copy link
Member

Hmmm. What if we were to simply have 2 completely separate arrays. That could be the answer!

@GrahamCampbell
Copy link
Member

$saved = array(
    'hash a' => object3,
    'hash b' => object1,
    'hash c' => object2,
);

$map = array(
    0 => 'hash b',
    1 => 'hash c',
    2 => 'hash a',
);

@GrahamCampbell
Copy link
Member

I'm going to get something to eat. I'll be back in 30 mins or so, and I'll take a look at this. Feel free to tackle this in a pull to 2.2 @pmccarren.

@pmccarren
Copy link
Author

@GrahamCampbell yeah exactly! sorry I wasn't clear there. I believe two separate arrays is the key

@pmccarren
Copy link
Author

Prototyped it over here: #316

@GrahamCampbell
Copy link
Member

Hmmm. I did want it in 2.2, rather than 3.0, but leave the pull open anyway so we can discuss it.

@pmccarren
Copy link
Author

Would you like me to create a PR with this change into 2.2/master?

@GrahamCampbell
Copy link
Member

No, don't bother. When I've got time, I'll review your change against 3.0. Thanks for taking the time to contribute - it's much appreciated. :)

@pmccarren
Copy link
Author

No problem! Glad to help out. using FactoryMuffin has made my testing experience so much better

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

Successfully merging this pull request may close these issues.

2 participants