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

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.

This comment has been minimized.

Copy link
@marcj

marcj Dec 10, 2014

Member

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

This comment has been minimized.

Copy link
@mpscholten

mpscholten Dec 10, 2014

Author Member

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.

This comment has been minimized.

Copy link
@marcj

marcj Dec 12, 2014

Member

Ah I see :)

*/
public function requireOneBy($column, $value, ConnectionInterface $con = null)
{
$result = $this->findOneBy($column, $value);

This comment has been minimized.

Copy link
@staabm

staabm Dec 10, 2014

Member

This misses to pass 3rd arg

This comment has been minimized.

Copy link
@mpscholten

mpscholten Dec 10, 2014

Author Member

Thanks for pointing this out. Just fixed this :)

*/
public function requireOne(ConnectionInterface $con = null)
{
$result = $this->findOne();

This comment has been minimized.

Copy link
@staabm

staabm Dec 10, 2014

Member

Pass con

@marcj

This comment has been minimized.

Copy link
Member

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.

@staabm

This comment has been minimized.

Copy link
Member

commented Dec 12, 2014

👍 for EntityNotFoundException

@mpscholten

This comment has been minimized.

Copy link
Member Author

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 :)

@marcj

This comment has been minimized.

Copy link
Member

commented Jan 2, 2015

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

@mpscholten mpscholten force-pushed the mpscholten:require-api branch from e99cff0 to 8c5ec20 Jan 2, 2015

@mpscholten

This comment has been minimized.

Copy link
Member Author

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! :)

Marc Scholten
Added requireOne, requireOneBy, requireOneByArray methods to Models
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

@mpscholten mpscholten force-pushed the mpscholten:require-api branch from 8c5ec20 to fac953c Jan 2, 2015

marcj added a commit that referenced this pull request Jan 7, 2015

Merge pull request #841 from mpscholten/require-api
require-api: requireOne, requireOneByXXX, requireByArray

@marcj marcj merged commit bcde8f3 into propelorm:master Jan 7, 2015

1 check passed

ci/circleci Your tests passed on CircleCI!
Details
@marcj

This comment has been minimized.

Copy link
Member

commented Jan 7, 2015

Thanks, great job :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.