Last active
August 18, 2016 21:39
-
-
Save styks1987/066be83cfe77c552cd933a32c175ef74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$controllerMock = $this->_getMock($sessionParams, $controller, [], $requestData); | |
$Listings = TableRegistry::get('Listings'); | |
$controllerMock->Listings = $Listings; | |
$listing = $controllerMock->Listings->newEntity(); | |
$listing = $controllerMock->_updateEntity($requestData, $listing); | |
$listing = $controllerMock->_saveData($listing); | |
$listing = $controllerMock->Listings->get($listing->id, [ | |
'contain' => ['Facilities.Location'] | |
]); | |
$data = $listing->toArray(); | |
$data['facility']['location']['name'] = 'A Newer Name'; | |
$data['facility']['storage_fee'] = 1000; | |
$listing = $controllerMock->_updateEntity($data, $listing); | |
$listing->dirty(true); | |
$listing->facility->dirty(true); | |
$listing->facility->location->dirty(true); | |
$listing = $controllerMock->_saveData($listing); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function _updateEntity($data, $listing) | |
{ | |
$listing = $this->Listings->patchEntity($listing, $data, [ | |
'associated' => $this->_getAssociatedArray() | |
]); | |
return $listing; | |
} | |
public function _saveData($listing) | |
{ | |
$listing->dirty(true); | |
if ($this->Listings->save($listing, [ | |
'associated' => $this->_getAssociatedArray(), | |
'user_id' => $this->Auth->user('id') // This needs to be set for the logging mechanism so I know who saved the listing | |
])) { | |
$error = false; | |
$listing->publishable = $this->isListingPublishable($listing->id); | |
} else { | |
$this->response->statusCode('400', 'Bad request. Are you sure you filled out all the correct fields? Try unpublishing and try again.'); | |
$errors = $listing->errors(); | |
$listing = $this->request->data; | |
$listing['publishable'] = $errors; | |
} | |
return $listing; | |
} | |
private function _getAssociatedArray() | |
{ | |
return [ | |
'Facilities' => [ | |
'accessibleFields' => ['id'], | |
'associated' => [ | |
'FacilityContactMethods', | |
'Location' => [ | |
'accessibleFields' => ['id'] | |
] | |
] | |
], 'TruckDetails', 'TrailerDetails', 'RvDetails', | |
'CraneDetails', 'BoatDetails', 'AutoDetails', 'Transmissions', 'Engines', 'AxleDetails', 'SpecDetails', | |
'TireDetails', 'Photos' | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$requestData = [ | |
'title' => 'My Big Fat Big Truck', | |
'public' => false, | |
'unlisted' => false, | |
'internal' => false, | |
'facility' => [ | |
'storage_fee' => 500, | |
'location' => [ | |
'name' => 'East over West', | |
'state' => 'VA' | |
] | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ListingsTable | |
$this->hasOne('Facilities', [ | |
'foreignKey' => 'listing_id', | |
'dependent' => true, | |
'cascadeCallbacks' => true | |
]); | |
// FacilitiesTable | |
$this->belongsTo('Location', [ | |
'className' => 'Locations' | |
]); | |
// LocationsTable | |
$this->hasMany('Facilities', [ | |
'foreignKey' => 'location_id' | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment