Avoid Fat Controller
So, I am using the Zend Framework, and I have a question related to preventing thick controllers from being one of my actions. Basically, I normalize the CSV file to my database.
This means that I need to get a feed and then use my model. Capturing feed is just to show how it works, but now it's an action helper.
I am using the Data Mapper template with Zend Framework. I hate that I do this in my controller. All these setProperty () -> setProperty () -> setProperty () look incredibly ugly, and I feel like I'm doing it in the wrong place? Would it be better to just create some kind of service level where I pass all the $ feed , and then in this class I create my models and my Mapper?
Also, I need to normalize, which means I have to use a transaction, but I'm not sure where I should start the transaction. Due to the way I am currently doing, the only place I could consider is in my controller. wow .. that would be a terrible place.
How can I get model behavior and operations from my controller?
ImportController.php
public function indexAction() {
$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$url = "http://www.domain.com/admin/GetBookingData.aspx";
$client = new Zend_Http_Client();
$client->setParameterGet('dateEnteredMin', $start);
$client->setParameterGet('dateEnteredMax', $end);
$client->setParameterGet('login', 'login');
$client->setParameterGet('password', 'password');
$client->setUri( $url );
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30));
$response = $client->request();
$feed = $this->csv_to_array(trim($response->getBody()));
$title = array_shift($feed);
$bookings = new Bookings_Models_Bookings();
$property = new Bookings_Models_Property();
$clients = new Bookings_Models_Clients();
$bookingsMapper = new Bookings_Models_Bookings_Mapper();
$propertyMapper = new Bookings_Models_Property_Mapper();
$clientsMapper = new Bookings_Models_Clients_Mapper();
$bookings->setId($feed[9])
->setPropertyId($feed[1])
->setClientId($feed[2])
->setDate($feed[4]);
$bookingsMapper->save($bookings);
$property->setId($feed[1])
->setPropertyName($feed[23])
$propertyMapper->save($bookings);
$clients->setId($feed[2])
->setFirstName($feed[20])
->setLastName($feed[21])
$clientsMapper->save($clients);
}