Repository to search in the database?

Have a client who makes the selection from the database
Is the object valid Post
Is the class repository that contains methods; getPostById, getPostsByTitle
The call which should return object the Post type or array of Post

repository build Query and passes the database to the client and receives the query result, then what should it be? I need to do something like return new Post($searchResult) ? to pass the raw result set from DB in the constructor of the object?

or how to do it

PS. Requirement that this should work without a doctrine.
July 4th 19 at 22:45
3 answers
July 4th 19 at 22:47
Standard for such cases, you implement the class (model) with complete parameter list for the constructor (with default parameters) and the method that creates the object.
class Post {
 private $id;
 private $title;
 private $description;
 /** ... etc ... */

 public function __construct($id, $title, $description) {
 $this->id = $id;
 $this->title = $title;
 $this->description = $description;

 public function getId() {
 return $this->id;
 public function getTitle() {
 return $this->title;
 public function getDescription() {
 return $this->description;

Class methods:
class PostProvider {
 public function getPostById($id) {
 $response = /** ... Get data ... */;
 if (!$response) {
 throw new Exception('Not found.');
 return $this->createPost($response);
 public function getPostsByTitle($id) {
 $result = [];
 $responses = /** ... Get data ... */;
 foreach ($responses as $response) {
 $result[] = $this->createPost($response);
 return $result;

 * Method returns the Post object by the given array of data
 * @return Post
 private function createPost(array $response){
 return new Post($response['id'], $response['title'], $response['description']);
thank you - erna_Bergstr commented on July 4th 19 at 22:50
July 4th 19 at 22:49
create a factory which raw data will make you the object. delegate her the job
July 4th 19 at 22:51
like return new Post($searchResult) ?

The fact that you have to write is called migratory. They take the state and throw it into the object-passing techniques (direct mapping). The alternative is to make a static method a constructor which will do the same thing but almost without reflections. The third option is proxy classes (as it does the doctrine).
And what these migratory different from

$post = new Post();
foreach($results as $item) {
$post->setDate( new Date ( $item["date"]));
} - erna_Bergstr commented on July 4th 19 at 22:54
those that observed the life cycle of entities. - erna_Bergstr commented on July 4th 19 at 22:57
: you better describe what you datamapper your if you have arrays with types. - Allene_Crona78 commented on July 4th 19 at 23:00
what do you mean "array of types"? - Allene_Crona78 commented on July 4th 19 at 23:03
when you have a "object" for getters and setters. Stupid data structures possible with the type checking. Anemic model. - erna_Bergstr commented on July 4th 19 at 23:06

Find more questions by tags Doctrine ORMPHPSymfony