Skip to content

Instantly share code, notes, and snippets.

diff --git a/workshop/controllers/posts_controller.php b/workshop/controllers/posts_controller.php
index 35c892c..d2324d4 100644
--- a/workshop/controllers/posts_controller.php
+++ b/workshop/controllers/posts_controller.php
@@ -28,7 +28,7 @@ class PostsController extends AppController {
}
}
$tags = $this->Post->Tag->find('list');
- $users = $this->Post->User->find('list');
+ $users = $this->Post->User->find('list', array('fields' => array('id', 'username')));
diff --git a/workshop/views/posts/view.ctp b/workshop/views/posts/view.ctp
index df8a9dc..f5df297 100644
--- a/workshop/views/posts/view.ctp
+++ b/workshop/views/posts/view.ctp
@@ -8,7 +8,7 @@
</dd>
<dt<?php if ($i % 2 == 0) echo $class;?>><?php __('User'); ?></dt>
<dd<?php if ($i++ % 2 == 0) echo $class;?>>
- <?php echo $html->link($post['User']['id'], array('controller' => 'users', 'action' => 'view', $post['User']['id'])); ?>
+ <?php echo $html->link($post['User']['username'], array('controller' => 'users', 'action' => 'view', $post['User']['id'])); ?>
diff --git a/workshop/views/posts/index.ctp b/workshop/views/posts/index.ctp
index 7892034..ed19d04 100644
--- a/workshop/views/posts/index.ctp
+++ b/workshop/views/posts/index.ctp
@@ -29,7 +29,7 @@ foreach ($posts as $post):
<?php echo $post['Post']['id']; ?>
</td>
<td>
- <?php echo $html->link($post['User']['id'], array('controller' => 'users', 'action' => 'view', $post['User']['id'])); ?>
+ <?php echo $html->link($post['User']['username'], array('controller' => 'users', 'action' => 'view', $post['User']['id'])); ?>
diff --git a/workshop/config/routes.php b/workshop/config/routes.php
index 2858312..6c56a03 100755
--- a/workshop/config/routes.php
+++ b/workshop/config/routes.php
@@ -30,7 +30,8 @@
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/views/pages/home.ctp)...
*/
- Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
+ //Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
INSERT INTO `tags` (`id`, `name`) VALUES(NULL, 'CakePHP');
INSERT INTO `tags` (`id`, `name`) VALUES(NULL, '関西');
INSERT INTO `tags` (`id`, `name`) VALUES(NULL, 'これはひどい');
INSERT INTO `tags` (`id`, `name`) VALUES(NULL, 'あとで読む');
<div class="posts form">
<?php echo $form->create('Post');?>
<fieldset>
<legend><?php __('Edit Post');?></legend>
<?php
echo $form->input('id');
echo $form->input('user_id');
echo $form->input('title');
echo $form->input('body');
echo $form->input('Tag');
<?php
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->Post->recursive = 0;
$this->set('posts', $this->paginate());
}
<?php
class Post extends AppModel {
var $name = 'Post';
var $validate = array(
'user_id' => array('numeric'),
'title' => array('notempty')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
<?php
class Tag extends AppModel {
var $name = 'Tag';
var $validate = array(
'name' => array('notempty')
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $hasAndBelongsToMany = array(
--
-- テーブルの構造 `posts_tags`
--
CREATE TABLE `posts_tags` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),