CodeIgniter and Doctrine models
I´m having problems understanding some basic strategies using doctrine 2 in codeIgniter 2 development.
Background: CI is up and running with doctrine, I can get entities from database and save them.
Example: I have few controllers, where I would like to list latest articles. In my pure CI application I would have 'getLatest' -method in my model. I then would call this in all of my controllers, loading correct view etc.
But now I have doctrine models and not sure how to do this. I just cant add same method to my model. What I have done is moved that getLatest-logic to controller and this does not look right. Now I would need to call other controllers from my actions to get these latest articles. Or should I really duplicate that code in every controller where I need it?
I am still struggling with this. My CI models and doctrine entities have same names and are located in same "application/models/" folder. These together cause several problems. I am trying to change this path, but cant get it work. I have used this library class for loading doctrine: http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/ Any tips?开发者_开发知识库
Your getLatest
functionality belongs in the model, not the controller. Each of your Doctrine 2 models has its own Repository, which you can get with $em->getRepository('Your\Model')
. The default repository (\Doctrine\ORM\EntityRepository) has several methods that might be useful to you:
$repository = $em->getRepository('Your\Model');
$all_entities = $repository->findAll();
$some_entities = $repository->findBy(array(
'some_column' => 'some_condition'
), $order_by, $limit, $offset);
If Doctrine's built-in repository doesn't do what you need, you can create your own repository for any of your models:
class YourRepository extends EntityRepository
{
public function getLatest()
{
// Your getLatest logic here
return $this->_em->createQuery('SELECT m FROM Your\Model LIMIT 10');
}
}
Instead of having a "Model", you can wrap Doctrine components in a Service and keep your controller as simple as possible. Using MVC framework or not, it is important to follow SOLID, especially the Single Responsibility Principle (SRP).
Consider the following simplified example:
class UserService extends AbstractService
{
private $em;
private $repository;
public function __construct(EntityManager $em, UserRepository $repository)
{
$this->em = $em;
$this->repository = $repository;
}
public function create($data)
{
// business logic here
$user = new User();
$user->setUsername($data['username']);
$user->setEmail($data['email']);
$em->persist($this->user);
$em->flush();
}
public function getLatest()
{
// additional business logic could be implemented
return $this->repository->getLatest();
}
}
Then I can call the service from the Controller or from any other service:
class UserController extends Controller
{
/**
* @var UserService $service
*/
private $service;
public function __construct(UserService $service)
{
$this->service = $service;
}
public function create()
{
// some form validation here
$data = $this->input->post('user');
$result = $this->service->create($data);
// do something with the result
}
}
No, you should not neither duplicate nor move that code to controllers. Haven't tried that personally, but I think that doctrine Entities and CI models are completely independent. You may introduce some CI's models that incorporate that getLatest
method, but really operate on Doctrine entities.
So you'll just use Doctrine entities and CI's models as usual. But don't forget to load doctrine
library (if you bootstrapped as told in Doctrine manual) into models that use Doctrine entities.
精彩评论