Skip to content

require-api: requireOne, requireOneByXXX, requireByArray#841

Merged
marcj merged 1 commit intopropelorm:masterfrom
mpscholten:require-api
Jan 7, 2015
Merged

require-api: requireOne, requireOneByXXX, requireByArray#841
marcj merged 1 commit intopropelorm:masterfrom
mpscholten:require-api

Conversation

@mpscholten
Copy link
Member

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)

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 check it with $this->isWithOneToMany() and throw a exception for that case.

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see :)

@marcj
Copy link
Member

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.

@staabm
Copy link
Member

staabm commented Dec 12, 2014

👍 for EntityNotFoundException

@mpscholten
Copy link
Member Author

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
Copy link
Member

marcj commented Jan 2, 2015

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

@mpscholten
Copy link
Member Author

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

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 pull request Jan 7, 2015
require-api: requireOne, requireOneByXXX, requireByArray
@marcj marcj merged commit bcde8f3 into propelorm:master Jan 7, 2015
@marcj
Copy link
Member

marcj 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants