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

require-api: requireOne, requireOneByXXX, requireByArray #841

Merged
merged 1 commit into from Jan 7, 2015

Conversation

@mpscholten
Copy link
Member

@mpscholten mpscholten commented Dec 10, 2014

Implementation of #803

This adds requireOne(), requireOneBy($column, $value) and requireOneByArray($values) to ModelCriteria. The ModelCriteria also provides magic methods for requireOneByXXX and requireOneByXXXAndXXX. The generated query classes have @method-annotations for these magic methods to support autocompletion.


You can now do something like:

<?php
// ...
try {
  echo 'Hello ' . UserQuery::create()->requireOneById($_GET['id'])->getName();
} catch (\Propel\Runtime\Exception\ModelNotFoundException $e) {
  echo $e->getMessage();
}

Which will output 'Hello Marc' or 'User could not be found' depending on whenever the given user-id exists.


You can customize the exception class in the propel config:

propel
    generator:
        objectModel:
            notFoundExceptionClass: \Vendor\Project\ResourceNotFoundException

or by overwriting the modelNotFoundExceptionClass attribute in your query class (or you can overwrite the createModelNotFoundException method to customize your exception even more, e.g. when you use a custom constructor signature)

*
* Throws an exception when nothing was found.
*
* Does not work with ->with()s containing one-to-many relations.
Copy link
Member

@marcj marcj Dec 10, 2014

Choose a reason for hiding this comment

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

You could check it with $this->isWithOneToMany() and throw a exception for that case.

Loading

Copy link
Member Author

@mpscholten mpscholten Dec 10, 2014

Choose a reason for hiding this comment

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

This should be handled in the $this->findOne method. I just copied the description from the findOne methods because the require methods just wrap the findOne methods.

This should be fixed in it's own pull request.

Loading

Copy link
Member

@marcj marcj Dec 12, 2014

Choose a reason for hiding this comment

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

Ah I see :)

Loading

@marcj
Copy link
Member

@marcj marcj commented Dec 12, 2014

Nice, I like it. I however don't like the term "model" in ModelNotFoundException, the model is actually the class struct and not the data/instance that is defined by its class/schema. I'd rather go with EntityNotFoundException.

Loading

@staabm
Copy link
Member

@staabm staabm commented Dec 12, 2014

👍 for EntityNotFoundException

Loading

@mpscholten
Copy link
Member Author

@mpscholten mpscholten commented Dec 13, 2014

The phpdoc of findOne says it "[...] returns a model object." so I used ModelNotFoundException to keep the identifiers consistent. But I'm fine with changing the exception :)

Loading

@marcj
Copy link
Member

@marcj marcj commented Jan 2, 2015

Excellent @mpscholten! When you are gonna squash those commits I'm going to merge this beauty. 😄

Loading

@mpscholten
Copy link
Member Author

@mpscholten mpscholten commented Jan 2, 2015

When the discussion in #803 is finished :) But I think we will just take the require syntax.

I haven't rebased yet because I already use this feature and the rebase would change the commit-hash my code is requiring in the composer lock file. But here we go! :)

Loading

Changed exception signature to match the default signature for easier reusing of existing exceptions

Added requireOneBy @method annotations for better autocompletion

Renamed NotFoundException to ModelNotFoundException

Added missing $con argument

Changed ModelNotFoundException to EntityNotFoundException
marcj added a commit that referenced this issue Jan 7, 2015
require-api: requireOne, requireOneByXXX, requireByArray
@marcj marcj merged commit bcde8f3 into propelorm:master Jan 7, 2015
1 check passed
Loading
@marcj
Copy link
Member

@marcj marcj commented Jan 7, 2015

Thanks, great job :)

Loading

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

Successfully merging this pull request may close these issues.

None yet

3 participants