Skip to content

Instantly share code, notes, and snippets.

@aerith
Created May 16, 2012 22:58
Show Gist options
  • Save aerith/2714720 to your computer and use it in GitHub Desktop.
Save aerith/2714720 to your computer and use it in GitHub Desktop.
<?php
class PhotoService extends AppModel {
public $name => 'PhotoService';
public $useTable = false;
public $actAs = array(
'Transactable' => array(),
'Streamable' => array(),
);
public $propertyDifinitions = array(
1 => array(
'name' => 'content_type',
'label' => 'コンテントタイプ',
)
);
public function post ($user, $data) {
$this->set($data);
if ($this->validates()) {
$post_data = array(
'Post' => array(
'user_id' => $user['User']['id'],
'title' => $data[$this->name]['title'],
'content' => $data[$this->name]['content'],
),
);
if (!empty($data[$this->name]['id'])) {
$post_data['Post']['id'] = $data[$this->name]['id'];
}
$this->begin();
$post = ClassRegistry::init('Post');
$post->create();
if ($post->save($post_data)) {
$post_id = $post->getLastInsertID();
$property = ClassRegistry::init('PostProperty');
$property_saving_aborted = false;
foreach ($this->propertyDifinitions as $code => $difinition) {
$property_data = array(
'PostProperty' => array(
'user_id' => $user['User']['id'],
'post_id' => $post_id,
'code' => $code,
'name' => $difinition['name'],
'value' => $data[$this->name][$difinition['name']],
),
);
$property->create();
if (!$property->save($property_data)) {
$property_saving_aborted = true;
break;
}
}
if (!$property_saving_aborted) {
if ($this->stream($user, $post_id)) {
$this->commit();
return true;
}
}
}
$this->rollback();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment