patternphpMinor
TodoList in Symfony 3
Viewed 0 times
todolistsymfonystackoverflow
Problem
This little project is about working with doctrine Entity object and everything is working fine.
This is my first project in Symfony 3. Please review my service file and give me some advice to improve it. My main concern is lack of error handling.
service.yml
Service file
```
doctrineManager = $em;
}
public function insert($array){
if(!isset($array['name'])){return false;}
$task = new test();
$task->setName($array['name']);
$task->setDate(new \DateTime());
if(isset($array['description'])){
$task->setDescription($array['description']);
}
$this->doctrineManager->persist($task);
$this->doctrineManager->flush();
return true;
}
public function get(){
$all = $this->doctrineManager->getRepository('AppBundle:test')->findAll();
if($all==null) {return false;}
$json = $this->transformIntoJson($all);
return $json;
}
public function getOne($id){
$one = $this->doctrineManager->getRepository('AppBundle:test')->findOneById($id);
if($one==null) {return false;}
$arr[]=$one;
$json = $this->transformIntoJson($arr);
return $json;
}
public function transformIntoJson($arr){
$table = [];
foreach($arr as $value){
$field = new \stdClass();
$field->id = $value->getId();
$field->name = $value->getName();
$field->description = $value->getDescription();
array_push($table,$field);
}
$table = json_encode($table);
return $table;
}
public function delete($id){
$one = $this->doctrineManager->getRepository('AppBundle:test')->findOneById($id);
if($one==null){return false;}
$this->doctrineManager->remove($one);
$this->doctrine
This is my first project in Symfony 3. Please review my service file and give me some advice to improve it. My main concern is lack of error handling.
service.yml
parameters:
services:
app.ToDoList:
class: AppBundle\Utils\ToDoList
arguments: ['@doctrine.orm.entity_manager']Service file
```
doctrineManager = $em;
}
public function insert($array){
if(!isset($array['name'])){return false;}
$task = new test();
$task->setName($array['name']);
$task->setDate(new \DateTime());
if(isset($array['description'])){
$task->setDescription($array['description']);
}
$this->doctrineManager->persist($task);
$this->doctrineManager->flush();
return true;
}
public function get(){
$all = $this->doctrineManager->getRepository('AppBundle:test')->findAll();
if($all==null) {return false;}
$json = $this->transformIntoJson($all);
return $json;
}
public function getOne($id){
$one = $this->doctrineManager->getRepository('AppBundle:test')->findOneById($id);
if($one==null) {return false;}
$arr[]=$one;
$json = $this->transformIntoJson($arr);
return $json;
}
public function transformIntoJson($arr){
$table = [];
foreach($arr as $value){
$field = new \stdClass();
$field->id = $value->getId();
$field->name = $value->getName();
$field->description = $value->getDescription();
array_push($table,$field);
}
$table = json_encode($table);
return $table;
}
public function delete($id){
$one = $this->doctrineManager->getRepository('AppBundle:test')->findOneById($id);
if($one==null){return false;}
$this->doctrineManager->remove($one);
$this->doctrine
Solution
-
It's better to user
-
I prefer adding
-
Instead of
-
You can create a new service and move the
-
Use
and please follow PSR-* coding styles.
It's better to user
snake_case for service names. so it's better to be app.to_do_list-
I prefer adding
now when using new \Datetime(); it's more readable.-
Instead of
get method you can call it getAll.-
You can create a new service and move the
transformIntoJson method there and add it as a dependency to your class.-
Use
snake_case for action names. like all_tasks instead of AllTasksand please follow PSR-* coding styles.
Context
StackExchange Code Review Q#151689, answer score: 3
Revisions (0)
No revisions yet.