質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Q&A

0回答

658閲覧

Cakephp でテーブルの結合を使って検索できるようにしたい

harukuri

総合スコア21

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

0グッド

1クリップ

投稿2021/07/25 12:51

前提・実現したいこと

CakePHP2でレシピサイトを作っています。
料理名や材料でレシピを検索できるようにしたいです。
RecipesがたくさんのIngredientsを持っている(hasMany)の状態です。

両方検索するために、テーブルを結合したいです。
Recipeテーブルの方に登録してあるキーワードは検索して表示されるのですが、Recipeテーブルにない言葉で検索すると
以下のエラー分が表示されます。

Notice (8): Undefined variable: recipe

勉強不足だと思うのですが、検索してもどう考えたらいいか分からないため、アドバイスいただきたいです。
よろしくお願いいたします。

RecipesController.php

CakePHP

1public function search(){ 2 $conditions=array(); 3 if(isset($this->params['url']['keyword'])){ 4 $keyword=$this->params['url']['keyword']; 5 array_push($conditions,array( 6 'OR'=>array( 7 'Recipe.title LIKE'=>'%'.$keyword.'%', 8 'Recipe.body LIKE'=>'%'.$keyword.'%', 9 ) 10 ) 11 ); 12 } 13 $recipes=$this->Recipe->find('all',array('conditions'=>$conditions)); 14 $this->set('recipes',$recipes); 15 $paginate=array( 16 'limit'=>5, 17 'order'=>array( 18 'Recipe.created'=>'desc' 19 ), 20 // 条件の設定 21 'conditions'=>$conditions, 22 ); 23 $this->Paginator->settings=$paginate; 24 $recipes=$this->Paginator->paginate(); 25 }

Recipe.php

CakePHP

1<?php 2App::uses('AppModel','Model'); 3class Recipe extends AppModel{ 4 public $hasMany=array( 5 'Ingredient'=>array( 6 'className'=>'Ingredient', 7 'foreignkey'=>'recipe_id', 8 'dependent'=>false, 9 ) 10 ); 11} 12$options['joins']= array( 13 array('table'=>'ingredients', 14 'alias'=>'Ingredient', 15 'type'=>'LEFT', 16 'conditions'=>array( 17 'Recipe.id = Ingredient.recipe_id', 18 ) 19 ) 20); 21?>

IngredientsController.php

CakePHP

1<?php 2App::uses('AppController','Controller'); 3class IngredientsController extends AppController{ 4public function search(){ 5 $conditions=array(); 6 if(isset($this->params['url']['keyword'])){ 7 $keyword=$this->params['url']['keyword']; 8 array_push($conditions,array('Ingredient.name LIKE'=>'%'.$keyword.'%')); 9 } 10 $ingredients=$this->Ingredient->find('all',array('conditions'=>$conditions)); 11 $this->set('ingredients',$ingredients); 12 } 13 return $this->redirect(array('controller'=>'recipes','action'=>'search')) 14}

###search.ctp

<?php echo $this->Form->create('Post',array('type'=>'GET')); echo $this->Form->input('keyword', array( 'label'=>'レシピを検索', 'placeholder'=>'料理名・食材名', ) ); echo $this->Form->end('検索'); ?> <table> <?php echo $this->Html->css('search-style'); ?> <!-- $recipesに配列の要素を最初から順に代入し、取得した要素を$recipeに格納する --> <?php foreach ((array)$recipes as $recipe){ ?> <tr> <div class="flex"> <div class="photo"> <?php echo $this->Html->image('/app/webroot/img/recipe_image/'.$recipe['Recipe']['id'].'.'.$recipe['Recipe']['extension'] );?> </div> <?php echo $recipe['Recipe']['title']; ?> &emsp;&emsp; <div class="description"> <?php echo $recipe['Recipe']['body']; ?> </div> <?php }?> <?php $ingredients=$recipe['Ingredient']; foreach ((array)$ingredients as $ingredient){ ?> <td><?php if (isset($ingredient['name'])){ echo '・'.nl2br($ingredient['name']); } ?> </td>&emsp;&emsp; <td> <?php if(isset($ingredient['amount'])){ echo $ingredient['amount']; } ?><br/> </td> </tr> <?php }?> <br/><br/><br/> </table>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問